Comparing version 0.4.2 to 0.5.1
{ | ||
"name": "fastdom", | ||
"description": "Eliminates layout thrashing by batching DOM read/write operations", | ||
"version": "0.4.2", | ||
"version": "0.5.1", | ||
"main": "lib/fastdom.js", | ||
@@ -6,0 +6,0 @@ "scripts": [ |
{ | ||
"name": "fastdom", | ||
"description": "Eliminates layout thrashing by batching DOM read/write operations", | ||
"version": "0.4.2", | ||
"version": "0.5.1", | ||
"main": "lib/fastdom.js", | ||
@@ -6,0 +6,0 @@ "scripts": [ |
@@ -36,6 +36,8 @@ | ||
function FastDom() { | ||
this.lastId = 0; | ||
this.jobs = {}; | ||
this.mode = null; | ||
this.pending = false; | ||
this.reads = []; | ||
this.writes = []; | ||
this.mode = null; | ||
this.pending = false; | ||
} | ||
@@ -51,4 +53,5 @@ | ||
FastDom.prototype.read = function(fn, ctx) { | ||
add(this.reads, fn, ctx); | ||
this.request('read'); | ||
var id = this._add(this.reads, fn, ctx); | ||
this._request('read'); | ||
return id; | ||
}; | ||
@@ -64,4 +67,5 @@ | ||
FastDom.prototype.write = function(fn, ctx) { | ||
add(this.writes, fn, ctx); | ||
this.request('write'); | ||
var id = this._add(this.writes, fn, ctx); | ||
this._request('write'); | ||
return id; | ||
}; | ||
@@ -73,7 +77,7 @@ | ||
* | ||
* @param {Function} fn | ||
* @param {Number} id | ||
* @api public | ||
*/ | ||
FastDom.prototype.clearRead = function(fn) { | ||
remove(this.reads, fn); | ||
FastDom.prototype.clearRead = function(id) { | ||
this._remove(this.reads, id); | ||
}; | ||
@@ -85,7 +89,7 @@ | ||
* | ||
* @param {Function} fn | ||
* @param {Number} id | ||
* @api public | ||
*/ | ||
FastDom.prototype.clearWrite = function(fn) { | ||
remove(this.writes, fn); | ||
FastDom.prototype.clearWrite = function(id) { | ||
this._remove(this.writes, id); | ||
}; | ||
@@ -101,3 +105,4 @@ | ||
*/ | ||
FastDom.prototype.request = function(type) { | ||
FastDom.prototype._request = function(type) { | ||
var mode = this.mode; | ||
var self = this; | ||
@@ -108,3 +113,3 @@ | ||
// job will be emptied from the write queue | ||
if (this.mode === 'writing' && type === 'write') return; | ||
if (mode === 'writing' && type === 'write') return; | ||
@@ -114,3 +119,3 @@ // If we are reading we don't need to schedule | ||
// in the currently active read queue | ||
if (this.mode === 'reading' && type === 'read') return; | ||
if (mode === 'reading' && type === 'read') return; | ||
@@ -121,3 +126,3 @@ // If we are reading we don't need to schedule | ||
// currently active frame. | ||
if (this.mode === 'reading' && type === 'write') return; | ||
if (mode === 'reading' && type === 'write') return; | ||
@@ -129,3 +134,3 @@ // If there is already a frame | ||
// Schedule frame (preserving context) | ||
raf(function() { self.frame(); }); | ||
raf(function() { self._frame(); }); | ||
@@ -138,2 +143,12 @@ // Set flag to indicate | ||
/** | ||
* Generates a unique | ||
* id for a job. | ||
* | ||
* @return {Number} | ||
*/ | ||
FastDom.prototype._uniqueId = function() { | ||
return ++this.lastId; | ||
}; | ||
/** | ||
* Calls each job in | ||
@@ -150,13 +165,13 @@ * the list passed. | ||
*/ | ||
FastDom.prototype.run = function(list) { | ||
var fn; | ||
FastDom.prototype._run = function(list) { | ||
var ctx; | ||
var job; | ||
var id; | ||
while (list.length) { | ||
fn = list.shift(); | ||
ctx = fn._dbctx || this; | ||
try { | ||
fn.call(ctx); | ||
} catch (err) { | ||
// TODO: console.error if options.silent === false. | ||
while (id = list.shift()) { | ||
job = this.jobs[id]; | ||
ctx = job.ctx || this; | ||
delete this.jobs[id]; | ||
try { job.fn.call(ctx); } catch (e) { | ||
if (this.onError) this.onError(e); | ||
} | ||
@@ -172,3 +187,3 @@ } | ||
*/ | ||
FastDom.prototype.frame = function() { | ||
FastDom.prototype._frame = function() { | ||
@@ -183,3 +198,3 @@ // Set the pending flag to | ||
this.mode = 'reading'; | ||
this.run(this.reads); | ||
this._run(this.reads); | ||
@@ -189,3 +204,3 @@ // Set the mode to 'writing' | ||
this.mode = 'writing'; | ||
this.run(this.writes); | ||
this._run(this.writes); | ||
@@ -200,4 +215,2 @@ this.mode = null; | ||
* | ||
* TODO: | ||
* | ||
* @param {Function} fn | ||
@@ -210,3 +223,5 @@ * @param {Number} frames | ||
if (frames-- === 0) { | ||
try { fn(); } catch (e){} | ||
try { fn(); } catch (e) { | ||
if (this.onError) this.onError(e); | ||
} | ||
} else { | ||
@@ -219,36 +234,41 @@ raf(wrapped); | ||
/** | ||
* Util | ||
*/ | ||
/** | ||
* Adds a function to | ||
* the given array. | ||
* Adds a new job to | ||
* the given queue. | ||
* | ||
* If a context is given | ||
* it is stored on the | ||
* function object for | ||
* later. | ||
* | ||
* @param {Array} array | ||
* @param {Array} list | ||
* @param {Function} fn | ||
* @param {Object} ctx | ||
* @returns {Number} id | ||
* @api private | ||
*/ | ||
function add(array, fn, ctx) { | ||
if (ctx) fn._dbctx = ctx; | ||
array.push(fn); | ||
} | ||
FastDom.prototype._add = function(list, fn, ctx) { | ||
var id = this._uniqueId(); | ||
// Store this job | ||
this.jobs[id] = { | ||
fn: fn, | ||
ctx: ctx | ||
}; | ||
// Push the id of | ||
// this job into | ||
// the given queue | ||
list.push(id); | ||
// Return the id | ||
return id; | ||
}; | ||
/** | ||
* Removes a function | ||
* from the given array. | ||
* | ||
* @param {Array} array | ||
* @param {Function} item | ||
* Removes a job from | ||
* the given queue. | ||
* @param {Array} list | ||
* @param {Number} id | ||
* @api private | ||
*/ | ||
function remove(array, fn) { | ||
var index = array.indexOf(fn); | ||
if (~index) array.splice(index, 1); | ||
} | ||
FastDom.prototype._remove = function(list, id) { | ||
var index = list.indexOf(id); | ||
if (~index) list.splice(index, 1); | ||
delete this.jobs[id]; | ||
}; | ||
@@ -255,0 +275,0 @@ /** |
{ | ||
"name": "fastdom", | ||
"description": "Eliminates layout thrashing by batching DOM read/write operations", | ||
"version": "0.4.2", | ||
"version": "0.5.1", | ||
"main": "lib/fastdom.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -32,5 +32,9 @@ # fastdom [![Build Status](https://travis-ci.org/wilsonpage/fastdom.png?branch=master)](https://travis-ci.org/wilsonpage/fastdom) | ||
## Examples | ||
- [Aspect ratio example](http://wilsonpage.github.io/fastdom/examples/aspect-ratio.html) | ||
## Installation | ||
FastDom is CommonJS and AMD compatible, you can install it in one of the follwing ways: | ||
FastDom is CommonJS and AMD compatible, you can install it in one of the following ways: | ||
@@ -65,3 +69,3 @@ ``` | ||
Schedules a task for the 'read' queue. | ||
Schedules a job for the 'read' queue. Returns a unique ID that can be used to clear the scheduled job. | ||
@@ -76,3 +80,3 @@ ```js | ||
Schedules a task for the 'write' queue. | ||
Schedules a job for the 'write' queue. Returns a unique ID that can be used to clear the scheduled job. | ||
@@ -85,22 +89,28 @@ ```js | ||
### FastDom#clearRead(callback) | ||
### FastDom#clearRead(id) | ||
Removes a task from the 'read' queue. | ||
Removes a job from the 'read' queue by id. | ||
```js | ||
var fn = function(){}; | ||
var id = fastdom.read(function(){}); | ||
fastdom.clearRead(id); | ||
``` | ||
fastdom.read(fn); | ||
fastdom.clearRead(fn); | ||
### FastDom#clearWrite(id) | ||
Removes a job from the 'write' queue by id. | ||
```js | ||
var id = fastdom.write(function(){}); | ||
fastdom.clearWrite(id); | ||
``` | ||
### FastDom#clearWrite(callback) | ||
### FastDom#defer(callback, frames) | ||
Removes a task from the 'write' queue. | ||
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 | ||
var fn = function(){}; | ||
fastdom.write(fn); | ||
fastdom.clearWrite(fn); | ||
fastdom.defer(expensiveStuff, 3); | ||
``` | ||
@@ -107,0 +117,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10089
262
134