diff options
author | Wilson Page <wilsonpage@me.com> | 2013-09-25 11:12:57 -0400 |
---|---|---|
committer | Wilson Page <wilsonpage@me.com> | 2013-09-25 11:12:57 -0400 |
commit | 1ff7af35a4acf8218b210f67da2a5f5598805129 (patch) | |
tree | 171fb861db796b422ceb3a57be95b6ebb9ee8f49 | |
parent | 9e126bde7d26bc43fbe81ec8b23e51e2187ea572 (diff) | |
parent | 3c369e994af94cbb9f805d5a35cc4b21dd53e721 (diff) | |
download | fastdom-1ff7af35a4acf8218b210f67da2a5f5598805129.zip fastdom-1ff7af35a4acf8218b210f67da2a5f5598805129.tar.gz fastdom-1ff7af35a4acf8218b210f67da2a5f5598805129.tar.bz2 |
Fix merge conflicts
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | lib/fastdom.js | 10 | ||||
-rw-r--r-- | test/test.defer.js | 15 |
3 files changed, 21 insertions, 8 deletions
@@ -101,14 +101,14 @@ var id = fastdom.write(function(){}); fastdom.clearWrite(id); ``` -### FastDom#defer(callback, frames) +### FastDom#defer(frames, callback[, context]) Defers a job for the number of frames specified. This is useful is you have a particualrly expensive piece of work to do, and don't want it to be done with all the other work. For example; you are using third party library that doesn't expose an API that allows you split DOM read/write work, `fastdom.defer()` will push this work futher into the future and prevent it from disrupting other carefully batched work. ```js -fastdom.defer(expensiveStuff, 3); +fastdom.defer(3, expensiveStuff); ``` ## Tests diff --git a/lib/fastdom.js b/lib/fastdom.js index 036a2dc..a5f98ad 100644 --- a/lib/fastdom.js +++ b/lib/fastdom.js @@ -197,14 +197,16 @@ * by the number of frames * specified. * - * @param {Function} fn * @param {Number} frames + * @param {Function} fn * @api public */ - FastDom.prototype.defer = function(fn, frames) { + FastDom.prototype.defer = function(frames, fn, ctx) { + if (frames < 0) return; + (function wrapped() { if (frames-- === 0) { - try { fn(); } catch (e) { + try { fn.call(ctx); } catch (e) { if (this.onError) this.onError(e); } } else { @@ -266,4 +268,4 @@ window['fastdom'] = fastdom; } -})(window.fastdom);
\ No newline at end of file +})(window.fastdom); diff --git a/test/test.defer.js b/test/test.defer.js index 18b83cf..a1045e1 100644 --- a/test/test.defer.js +++ b/test/test.defer.js @@ -5,7 +5,7 @@ suite('defer', function(){ var fastdom = new FastDom(); var job = sinon.spy(); - fastdom.defer(job, 4); + fastdom.defer(4, job); raf(function() { assert(!job.called); @@ -21,4 +21,15 @@ suite('defer', function(){ }); }); }); -});
\ No newline at end of file + + test("Should call a deferred callback with the given context", function(done) { + var fastdom = new FastDom(); + var cb = sinon.spy(); + var ctx = { foo: 'bar' }; + + fastdom.defer(2, function() { + assert.equal(this.foo, 'bar'); + done(); + }, ctx); + }); +}); |