Created
October 4, 2012 04:39
-
-
Save jasonkuhrt/3831511 to your computer and use it in GitHub Desktop.
Rewriting (and playing with) meteor deps docs reactive example into livescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prelude.install-prelude global | |
class Weather | |
(@temperature=30degrees)-> | |
@listeners = {} | |
get-temp: -> | |
@_try-registering-reactive-context! | |
@temperature | |
set-temp: (new-temperature)-> | |
if new-temperature isnt @temperature and typeof new-temperature is \number | |
@temperature = new-temperature | |
each (.invalidate!), @listeners | |
return this | |
# private | |
_try-registering-reactive-context: -> | |
context = Meteor.deps.Context.current | |
# If we're inside a context, | |
# and it's not yet listening to temperature changes.. | |
if context and not @listeners[context.id] | |
# .. add it to our list of contexts that care about the temperature .. | |
@listeners[context.id] = context | |
# .. and remember to take it off our list when it goes away. | |
context.on-invalidate ~> | |
delete @listeners[context.id] | |
weather = new Weather 2degrees | |
reactive -> console.log weather.get-temp! | |
weather.set-temp 100degrees | |
function reactive(fn) | |
ctx = new Meteor.deps.Context! | |
ctx.on-invalidate -> reactive fn | |
ctx.run -> fn.call void, ctx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment