Meteor: Tracker.autorun and dep.changed causing infinite loop -


i using new tracker.dependency tracking several things, causes autorun in code below run infinitely. wrong? code below okay once separate getsong , getsongid depend on dep , dep2, instead of dep.

songmanager = {   dep: new tracker.dependency,   dep2: new tracker.dependency,   init: function(songid) {     var self = this;     this.setsongid(songid);     meteor.subscribe('song', songid);     tracker.autorun(function(){       var songs = self.getsongcursor().fetch();       if (songs.length > 0) {         self.song = songs[0];         self.dep.changed();       }     })   },   getsongcursor: function(){     return songs.find({_id: this.getsongid()});   },   getsong: function(){     this.dep.depend();     return this.song;   },   getsongid: function(){     this.dep2.depend();     return this.songid;   },   setsongid: function(arg){     this.songid = arg;     this.dep2.changed();   }, }; 

the problem you're creating circular dependency. recommend using reactivevar rather working lower-level dependency api.

meteor add reactive-var 

then can this:

songmanager = {    song: new reactivevar(),    songid: new reactivevar(),    init: function(songid) {     this.songid.set(songid);     this.computation = tracker.autorun(_.bind(this.update, this));   },    update: function() {     var songid = this.songid.get();     meteor.subscribe('song', songid);     this.song.set(songs.findone(songid));   },    stop: function() {     this.computation.stop();   } };  songmanager.init(oldsongid); songmanager.songid.set(newsongid);  // after enough time has passed subscription update , tracker flush: var currentsong = songmanager.song.get(); console.log(currentsong._id === newsongid); // true 

i added way stop autorun computation doesn't keep running in background when it's no longer necessary. note since subscription run within autorun, automatically stopped , restarted when songid changes. update function run twice, meteor knows not send 2 identical subscription requests.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -