Last active
August 29, 2015 14:21
-
-
Save jperl/80ac150f733a1b84a803 to your computer and use it in GitHub Desktop.
Meteor performance optimizations -- inspired by https://github.com/remcoder/computron
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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