Skip to content

Instantly share code, notes, and snippets.

@jperl
Last active August 29, 2015 14:21
Show Gist options
  • Save jperl/80ac150f733a1b84a803 to your computer and use it in GitHub Desktop.
Save jperl/80ac150f733a1b84a803 to your computer and use it in GitHub Desktop.
Meteor performance optimizations -- inspired by https://github.com/remcoder/computron
Tools.debouncedAutorun = function (f, timeout) {
var debounced = _.debounce(function (c) {
// Store current computation
var prev = Tracker.currentComputation;
// Set the new computation
Tracker.currentComputation = c;//thisComputation;
Tracker.active = !!Tracker.currentComputation;
// Call function
f.call(this, c);
// Switch back
Tracker.currentComputation = prev;
Tracker.active = !!Tracker.currentComputation;
}, timeout);
return Tracker.autorun(function DebouncedComputation(c) {
if (c.firstRun) {
// Let the first run be run normally
f.call(this, c);
} else {
// On reruns we debounce
debounced(c);
}
});
};
Tools.debouncedComputron = function (f, timeout) {
var value = new ReactiveVar();
Tools.debouncedAutorun(function (comp) {
value.set(f(comp));
}, timeout);
// only expose get()
return function () {
return value.get();
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment