Minor refactor

This commit is contained in:
Joe Cheng
2012-06-21 14:03:59 -07:00
parent 615af50a0a
commit 4086392236
2 changed files with 24 additions and 18 deletions

View File

@@ -1,26 +1,15 @@
require './react'
class Observer
def initialize(&proc)
@proc = proc
run
end
include React
def run
ctx = React::Context.new
ctx.on_invalidate do
run
end
ctx.run &@proc
end
end
sess = Session.new
sess.set('user', '')
sess = React::Session.new
user = React::ObservableValue.new { sess.get('user') }
upUser = React::ObservableValue.new { (user.get||'').upcase }
user = ObservableValue.new { sess.get('user') }
upUser = ObservableValue.new { user.get.upcase }
Observer.new { puts upUser.get }
sess.set('user', 'jcheng')
React::Context.flush
Context.flush
sess.set('user', 'jjallaire')
React::Context.flush
Context.flush

View File

@@ -114,6 +114,7 @@ module React
end
end
# Stores (and caches) a single dependent value in a context
class ObservableValue
def initialize(&valueProc)
@valueProc = valueProc
@@ -150,4 +151,20 @@ module React
end
end
# Runs the given proc whenever its dependencies change
class Observer
def initialize(&proc)
@proc = proc
run
end
def run
ctx = React::Context.new
ctx.on_invalidate do
run
end
ctx.run &@proc
end
end
end