Using Flow-router and observeChanges

I’m making the switch from IR & the only problem I can’t figure out is how to wait until the subscriptions are ready before running observeChanges.
First, I need to wait until the subscription is ready because my observeChanges.added callback treats already stored documents differently than documents added since the user has been on the page.

So, in my onRendered callback, I tried writing something like this:

Tracker.autorun(function () {
    if (!FlowRouter.subsReady()) return;
    observeChanges();
  });

However, due to [this] (https://github.com/meteorhacks/flow-router/blob/2ea786c1b098013635c1744393557bd30859df70/client/router.js#L213) dependency in flow-router, subsReady gets rerun every time a parameter is changed, thus rerunning my observation. I also can’t stop the computation because if I did, it’d stop the observation as well.

Any thoughts? The only thing I can think of is rewriting flow-router to only run the .depend() if the path changes.

I think you can use the little var initializing = false trick in your observe changes to create a logic-branchpoint in your observeChanges methods.

So, if initializing is true, the document is treated as if it is a “before user has been on page” document. if it is false, it is treated as a document that “came after the user has been on the page”. I don’t think you need to do anything fancy with a Tracker and the router here.

Edit:

var initializing = true;
Posts.find(query,sort).observeChanges({
    added: onAdded,
    changed: onChanged,
    ...
})
initializing = false;

onAdded = function(id, fields){
    if (initializing){
        //hey, we are coming from the server still, so we wait.
    } else {
        //waiting done! we have arrived fresh off the press, since the user has been on the page
    }
}

I’m already doing that inside the observeChanges function. The problem is that every time the subsReady() gets invalidated, the observeChanges gets stopped & rerun.

actually, how often is your subscription rerun? and why do you need to have the obsrve changes inside a tracker? seems like that is the problem.

observeChanges is inside a tracker because I need to make sure the subscription is loaded before I run it.

Trying to figure out why you need that to be the case. To make sure the subscription is loaded, you’re effectively waiting for its ready callback. You could use that…

the ready callback is nothing more than an autorun with a computation.stop() at the end, which would thus stop my observation…

You can achieve this with the initalizing functionality, right? I’m not sure what else youre trying to achieve, maybe you could give some more information.

The problem doesn’t have anything really to do with the observeChanges, we could replace that with a console.log('foo') and make the goal to only print “foo” once. Rather I want to stop subsReady from invalidating when setParams is called.

For now, I did the following hack, which plays off the fact that equals won’t invalidate if it isn’t changed. It’s super ugly, but I don’t know what else to do:

var mySubsReady = new ReactiveDict();
Tracker.autorun(function () {
    if (!FlowRouter.subsReady()) return;
    mySubsReady.set('all',true);
  });

  Tracker.autorun(function() {
    if (mySubsReady.equals('all',true)) {
      observeChanges();
    }
  });

Maybe subsReady with callback will help you. https://github.com/meteorhacks/flow-router#flowroutersubsready-with-a-callback