Sunday, 15 August 2010

meteor - how does Tracker.autorun pick out its computation? -



meteor - how does Tracker.autorun pick out its computation? -

looking @ tracker.autorun, works magically...but i'd know how decides variables going form dependency computation. picks out "reactive" vars, illustration following:

window.bar = 1 tracker.autorun (c) => bar = window.bar foo = session.get('foo') console.log('autorun', foo, bar)

if alter value of session.set('foo') cause computation run again. whereas changing window.bar doesn't cause rerun. if utilize subscribe result (not collection) works, guess reactive too.

are there guides understanding behavior bit better?

edit:

thanks comments below clarify computation able inferred because accessors used reactive vars, meteor can track deps.

however need bit more clarity understand when var flagged. instance in illustration below, subscribe phone call outside autorun, puts results array. means tracker not tracking calls (reactive var) accessor methods, variables referenced within block - if calls setup methods outside autorun() block.

sublist = [ meteor.subscribe("players"), meteor.subscribe("stuff" ) ] tracker.autorun (c) => subready = _.filter sublist, (item) -> homecoming item.ready() alldone = (sublist.length == subready.length) # code rerun when subs ready() true

maybe should add together new question... it's related this question .

i'm not expert , haven't read much it, can seek explain briefly.

all reactive variables have thing called dependency. example, when 1 creates new reactivevar, new dependency created. see here.

to retrieve value reactive variable, 1 must phone call function. in "getter", dependency instructed remember has dependency. example, see here reactivevar.get.

to alter value reactive variable, 1 must phone call function. in "setter", dependency notified has changed, , signals functions depending on dependency must rerun. example, see here reactivevar.set.

not complicated, right? well, easy part, remains building infrastructure makes work :) that's harder , more complicated explain.

reactive variables aren't reactive themselves; must evaluated in reactive environment in order reactive. reactive environment created calling tracker.autorun. see here.

when phone call tracker.autorun, function passed executed in new reactive environment, , dependencies reactive variables notifies of depend method tracked environment. when phone call adependency.depend, this function executed, , kind of adds dependency environments list on dependencies depends on.

when reactive variable changes value, this function executed. tells environment 1 of reactive variables depends on has changed, , invalidates dependencies in environment. after has happened, entire function passed tracker.autorun re-run, , new dependencies tracked.

do big picture? it's implementation bit more complicated i've explained, think that's kind of how works.

meteor

No comments:

Post a Comment