stubailo / meteor-reactive-method

Call methods synchronously inside Tracker.autorun [deprecated]
https://atmospherejs.com/simple/reactive-method
MIT License
98 stars 11 forks source link

[feature] - trigger method to rerun #6

Closed ecwyne closed 9 years ago

ecwyne commented 9 years ago

It would be useful to trigger ReactiveMethod to invalidate previously returned results and rerun.

Proposed API

Template.myTemplate.helpers({
  helperName: function (){
    return ReactiveMethod.call('methodName', 'a', 'b');
  }
})

//elsewhere
ReactiveMethod.invalidate('methodName', 'a', 'b');
//helperName reruns method and returns new result

Implementation

reactive-method.js

  ReactiveMethod = {
    call: ...
    apply: ...
    invalidate: function (/* arguments */){
      var cc = Tracker.currentComputation;
      //ensure computation existence, serialize args, etc...
      delete cc._reactiveMethodData[serializedArgs];
      cc.invalidate();
    }
  }

I am more than willing to implement this and create a PR. I believe this method could be called "invalidate" or "changed" or "rerun", did't know if you'd have any preferences or any other thoughts on implementing this API @stubailo

stubailo commented 9 years ago

I'd love to accept a PR for this, but unfortunately it's not as simple as your proposed implementation because Tracker.currentComputation will not be the correct object. It might require changing the result caching to be global instead of per-computation.

ecwyne commented 9 years ago

@stubailo

Here's a working implementation. I would love to hear your thoughts https://github.com/stubailo/meteor-reactive-method/compare/master...ecwyne:master

stubailo commented 9 years ago

Wow, looks nice!

stubailo commented 9 years ago

Going to review now.

stubailo commented 9 years ago

Done! Published 1.0.2 with this feature. I decided to split up invalidateCall and invalidateApply, seemed cleaner than supporting two APIs in one.

ecwyne commented 9 years ago

Great! Glad to have this feature! Glad you were able to figure out a way to not store computations forever!

stubailo commented 9 years ago

Haha yeah with these kinds of libraries it's easy to accidentally introduce memory leaks..