Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meteor.subscribe onReady callback called before sub marked ready #4614

Closed
alexcorre opened this issue Jun 23, 2015 · 3 comments
Closed

Meteor.subscribe onReady callback called before sub marked ready #4614

alexcorre opened this issue Jun 23, 2015 · 3 comments

Comments

@alexcorre
Copy link

Hi Meteor Team!

I had a question about these lines of code: https://github.com/meteor/meteor/blob/master/packages/ddp/livedata_connection.js#L1377-L1379

When we receive a ready message for a subscription we call the readyCallback before actually marking the subscription as ready. Is there a reason for this? I want to achieve the following but cannot:

Lets say we have a subscription called someSub that publishes some interesting data. This interesting data is used by getSomeData(). I want to make sure that developers on my team dont call getSomeData until the someSub subscription is ready, since it could lead to unpredictable and particularly buggy state.

Now, in the example below, I call getSomeData within the onReady callback function, and it still throws an error.

MySubs.something = Meteor.subscribe('someSub', function() {
    // do some stuff...

   var data = getSomeData();

   // do some more stuff...
});

inside getSomeData

function getSomeData() {
   if (!MySubs.something.ready()) {
     // THIS THROWS when i call getSomeData from the ready callback.
    throw new Error()
  }

  // do some important stuff with the data published by someSub
}

This may just be an edge case, but to me. it seems that ready() should return true from inside the readyCallback. What do you think?

@ccorcos
Copy link

ccorcos commented Jun 24, 2015

Something just to watch out for: MySubs.something.ready() is reactive. So maybe theres an autorun you're not expecting.

@ccorcos
Copy link

ccorcos commented Jun 24, 2015

Not sure why this functionality happens -- maybe due to the flush cycle. In the meantime, Meteor.defer would probably be a suitable workaround.

@glasser
Copy link
Contributor

glasser commented Jun 30, 2015

Good point! I don't think anyone ever thought carefully about this, and it definitely makes sense the way you suggest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants