Having a not reactive option

Hey guys,

I think meteor needs a way to not be reactive as well, cause sometimes it’s just a pain in th a**. Do you think its possible to have like an option in the core to not be reactive which kind of defaults to true. I don’t know if this has been discussed early but i would like to hear some opinions from people who have a good understanding of the core.

So we could create like a non reactive publication, a non reactive tracker.autorun function etc, by passing another parameter or an options object?

3 Likes

You can have non-reactive publication if you use low level publish API.
Why would you want non-reactive tracker? Just dont use tracker and it would be automatically non-reactive.
We have reactive:false for Cursor.find on client side, so there you can have also full control when something recompute.

I know we can’t do that stuff right now, this is kind of a discussion on what the community thinks of these options. I know its best to not use tracker.autorun if you don’t want reactivity, but think of this use case, you wanted reactivity for a certain amount of time then you no longer need it. Being able to switch reactivity on/off on demand could be a great addition.

Also regarding publication reactivity, that is exactly what i would want to have. I mean having the option to create non-reactive publications on the lowest level would be very useful in my opinion.

In your use-case here, I would just use a reactive var set up on your template (or wherever you are subscribing):

Template.posts.onCreated(function() {
  this.isReactive = new ReactiveVar(true); // start as true so getPosts evaluates properly
  this.timeoutHandle = Meteor.setTimeout(() => {
    this.isReactive.set(false);
  }, 10000); // disable reactivity after 10 seconds 
});

Template.posts.onDestroyed(function() {
  Meteor.clearTimeout(this.timeoutHandle); // Clears the timeout if the template is destroyed early
});

Template.posts.helpers({
  getPosts: function() {
    let template = Template.instance(); // get template handle
    let reactive = template.isReactive.get(); // get reactive boolean
    return Posts.find({}, {reactive}); // enable/disable reactivity on cursor
  },
});

Note to use this you will need to add the reactive-var package to your application: meteor add reactive-var

1 Like

But won’t it be nice to be able to do that natively?

Given that Pub/Sub is inherently reactive, I’m not sure what you mean by “do that natively”?

I agree that not all data needs to be reactive, but I also find the current methods available for preventing reactivity when necessary are sufficient.

That has existed for a long time. Publish on meteor docs.

Start there and keep reading. It starts by describing the “traditional” reactive publication that meteor is so well known for. It then goes on to describe how you can have full direct control of the publication. On the server, you can either return a cursor, or you can use simple non-reactive updates.

1 Like

I’ve always thought that Meteor couples together three things with the data system that should each be opt-in on their own:

  1. Optimistic UI
  2. Global data store for consistent UI
  3. Reactive data pushing from the server

I hope a future version of Meteor lets you explicitly make tradeoffs between these three. In particular, 1 requires a lot of “magic” complexity on the frontend, and 3 requires a very fancy database driver and rocket scientist scaling; it would be cool if one could opt-out of those.

5 Likes