summaryrefslogtreecommitdiffstats
path: root/lib/fastdom.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fastdom.js')
-rw-r--r--lib/fastdom.js84
1 files changed, 47 insertions, 37 deletions
diff --git a/lib/fastdom.js b/lib/fastdom.js
index a5f98ad..677a215 100644
--- a/lib/fastdom.js
+++ b/lib/fastdom.js
@@ -13,12 +13,23 @@
'use strict';
- // RequestAnimationFrame Polyfill
+ // Normalize rAF
var raf = window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| function(cb) { window.setTimeout(cb, 1000 / 60); };
+ // Normalize cAF
+ var caf = window.cancelAnimationFrame
+ || window.cancelRequestAnimationFrame
+ || window.mozCancelAnimationFrame
+ || window.mozCancelRequestAnimationFrame
+ || window.webkitCancelAnimationFrame
+ || window.webkitCancelRequestAnimationFrame
+ || window.msCancelAnimationFrame
+ || window.msCancelRequestAnimationFrame
+ || function(id) { window.clearTimeout(id); };
+
// Use existing instance if
// one already exists in
// this app, else make one.
@@ -37,8 +48,10 @@
this.jobs = {};
this.mode = null;
this.pending = false;
- this.reads = [];
- this.writes = [];
+ this.queue = {
+ read: [],
+ write: []
+ };
}
/**
@@ -49,9 +62,10 @@
* @api public
*/
FastDom.prototype.read = function(fn, ctx) {
- var id = this._add(this.reads, fn, ctx);
+ var job = this._add('read', fn, ctx);
+ this.queue.read.push(job.id);
this._request('read');
- return id;
+ return job.id;
};
/**
@@ -62,9 +76,10 @@
* @api public
*/
FastDom.prototype.write = function(fn, ctx) {
- var id = this._add(this.writes, fn, ctx);
+ var job = this._add('write', fn, ctx);
+ this.queue.write.push(job.id);
this._request('write');
- return id;
+ return job.id;
};
/**
@@ -74,19 +89,21 @@
* @param {Number} id
* @api public
*/
- FastDom.prototype.clearRead = function(id) {
- this._remove(this.reads, id);
- };
+ FastDom.prototype.clear = function(id) {
+ var job = this.jobs[id];
+ if (!job) return;
+
+ // Defer jobs are cleared differently
+ if (job.type === 'defer') {
+ caf(job.timer);
+ return;
+ }
- /**
- * Removes a job from
- * the 'writes' queue.
- *
- * @param {Number} id
- * @api public
- */
- FastDom.prototype.clearWrite = function(id) {
- this._remove(this.writes, id);
+ var list = this.queue[job.type];
+ var index = list.indexOf(id);
+
+ if (~index) list.splice(index, 1);
+ delete this.jobs[id];
};
/**
@@ -182,12 +199,12 @@
// Set the mode to 'reading',
// then empty all read jobs
this.mode = 'reading';
- this._run(this.reads);
+ this._run(this.queue.read);
// Set the mode to 'writing'
// then empty all write jobs
this.mode = 'writing';
- this._run(this.writes);
+ this._run(this.queue.write);
this.mode = null;
};
@@ -203,16 +220,17 @@
*/
FastDom.prototype.defer = function(frames, fn, ctx) {
if (frames < 0) return;
-
+ var job = this._add('defer', fn);
(function wrapped() {
if (frames-- === 0) {
- try { fn.call(ctx); } catch (e) {
+ try { fn.call(ctx); } catch(e) {
if (this.onError) this.onError(e);
}
} else {
- raf(wrapped);
+ job.timer = raf(wrapped);
}
})();
+ return job.id;
};
/**
@@ -225,22 +243,14 @@
* @returns {Number} id
* @api private
*/
- FastDom.prototype._add = function(list, fn, ctx) {
+ FastDom.prototype._add = function(type, fn, ctx) {
var id = this._uniqueId();
-
- // Store this job
- this.jobs[id] = {
+ return this.jobs[id] = {
+ id: id,
fn: fn,
- ctx: ctx
+ ctx: ctx,
+ type: type
};
-
- // Push the id of
- // this job into
- // the given queue
- list.push(id);
-
- // Return the id
- return id;
};
/**