Skip to content

Commit

Permalink
Add nodei.co badge to REAME.md; fix indent.
Browse files Browse the repository at this point in the history
  • Loading branch information
uid11 committed Sep 19, 2016
1 parent 17b0147 commit 3dd6c4d
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 103 deletions.
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# next-task #

[![NPM version][npm-image]][npm-url] ![dependencies][dependencies-image] [![License MIT][license-image]](LICENSE)
[![NPM version][npm-image]][npm-url] ![dependencies][dependencies-image] [![License MIT][license-image]](LICENSE)

Fast microtask queue for all platforms, equivalent rawAsap (based on the ideas and source of rawAsap), but a little faster.
[![NPM](https://nodei.co/npm/next-task.png)](https://nodei.co/npm/next-task/)

Fast microtask queue for all platforms, equivalent rawAsap (based on the ideas and source of rawAsap), but a little faster.

## Usage ##
```js
Expand Down Expand Up @@ -31,11 +33,12 @@ nextTask(task);
About rawAsap and microtasks: [rawAsap](https://github.com/kriskowal/asap#raw-asap).
If you need queue of animation tasks, use [raf](https://github.com/chrisdickinson/raf) instead, for synchronize with rendering loop.
If you need to perform a long (macrotask) queue of heavy tasks, use [setImmediate](https://github.com/YuzuJS/setImmediate) to give the browser the ability to handle current events.
Note that, like rawAsap, nextTask does not catch the errors (to work as soon as possible).
Note that, like rawAsap, **next-task** does not catch the errors (to work as soon as possible).


## Differences from rawAsap ##
- **Errors**:

### Errors ###
```js
/**
* If a task does throw an error, with rawAsap you need
Expand All @@ -50,21 +53,25 @@ rawAsap.requestFlush();
nextTask();
```

- **Domains**: nextTask does not support domains for Node.js (rasAsap does).
### Domains ###
**next-task** does not support domains for Node.js (rasAsap does).

- **Promise**: nextTask uses native Promise, if it is available (only native, and ignores any polyfills). More information: [Consider using Promise.prototype.then](https://github.com/kriskowal/asap/issues/54).
### Promise ###
**next-task** uses native Promise, if it is available (only native, and ignores any polyfills). More information: [Consider using Promise.prototype.then](https://github.com/kriskowal/asap/issues/54).

- **Property 'use'** points to the technology used:
### Property 'use' ###
Property 'use' points to the technology used:
```js
/** In the order of attempts to use: */
nextTask.use === 'setImmediate' || /* only Node.js */
'Promise' || /* ES6 native promise, if available */
'MutationObserver' || /* modern browsers */
'setTimeout' /* all other platforms */
```
- **Method 'setCapacity'** to limit the memory usage (more information: [function to change rawAsap.capacity value must be added](https://github.com/kriskowal/asap/issues/53)):

### Method 'setCapacity' ###
Method 'setCapacity' limited the memory usage (more information: [function to change rawAsap.capacity value must be added](https://github.com/kriskowal/asap/issues/53)):
```js
/* return new actual value */
nextTask.setCapacity(1024); /* return 1024 */

nextTask.setCapacity(); /* return 1024 */
Expand Down Expand Up @@ -124,7 +131,7 @@ $ npm run test:browser
```

## License ##
[MIT](LICENSE)
[MIT](LICENSE)

[license-image]: https://img.shields.io/badge/license-MIT-blue.svg "license-image"
[dependencies-image]: https://img.shields.io/gemnasium/mathiasbynens/he.svg?maxAge=2592000 "dependencies-image"
Expand Down
24 changes: 13 additions & 11 deletions benchmark/browser-scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,27 @@ suite.on('complete', function() {
});

function addTimer(s, f) {
suite.add(s, function (deferred) {
f(function () {
deferred.resolve();
});
}, {
defer: true
suite.add(s, function (deferred) {
f(function () {
deferred.resolve();
});
}, {
defer: true
});
}

function time(s, f) {
// This is to make sure that the function doesn't
// have any errors before benchmarking it
f(function () {});
/**
* This is to make sure that the function doesn't
* have any errors before benchmarking it.
*/
f(function () {});

addTimer(s, f);
addTimer(s, f);
}

function run() {
suite.run();
suite.run();
}

module.exports = benchmark = {
Expand Down
2 changes: 1 addition & 1 deletion benchmark/drain.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var benchmark = require("./scaffold");
var asap = require("asap");
var rawAsap = require("asap/raw");

/* asap default capacity */
/** asap default capacity */
var CAPACITY = 1024;

var nextTask = require("../src/next-task");
Expand Down
26 changes: 14 additions & 12 deletions benchmark/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var Benchmark = require("benchmark");

var results = [], benchmark;

var suite = new Benchmark.Suite;
var suite = new Benchmark.Suite();

suite.on('cycle', function (event) {
var res = String(event.target);
Expand All @@ -18,25 +18,27 @@ suite.on('complete', function() {
});

function addTimer(s, f) {
suite.add(s, function (deferred) {
f(function () {
deferred.resolve();
});
}, {
defer: true
suite.add(s, function (deferred) {
f(function () {
deferred.resolve();
});
}, {
defer: true
});
}

function time(s, f) {
// This is to make sure that the function doesn't
// have any errors before benchmarking it
f(function () {});
/**
* This is to make sure that the function doesn't
* have any errors before benchmarking it.
*/
f(function () {});

addTimer(s, f);
addTimer(s, f);
}

function run() {
suite.run();
suite.run();
}

module.exports = benchmark = {
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-task",
"version": "1.0.8",
"version": "1.0.9",
"description": "Implementation of nextTick (microtask queue) for all platforms (like asap.js).",
"main": "src/next-task.js",
"scripts": {
Expand All @@ -24,11 +24,11 @@
],
"dependencies": {},
"devDependencies": {
"asap": "^2.0.0",
"benchmark": "^2.0.0",
"webpack": ">=1.0.0",
"mocha": "^3.0.0",
"opener": "*",
"asap": "^2.0.4",
"benchmark": "^2.1.1",
"webpack": "^1.13.2",
"mocha": "^3.0.2",
"opener": "^1.4.2",
"lodash": "^4.15.0",
"platform": "^1.3.1"
},
Expand Down
5 changes: 5 additions & 0 deletions src/next-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ if (!nextTask) {
};
}

/**
* Set capacity value.
* @param {number} value
* @return {number} Setted value.
*/
nextTask.setCapacity = function setCapacity(value) {
return capacity = Number(value) || capacity;
};
Expand Down
123 changes: 61 additions & 62 deletions test/scaffold-for-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,98 +7,97 @@ var failed = 0;
var global = Function('return this')();

module.exports = {
it: it,
expect: expect,
run: run
it: it,
expect: expect,
run: run
};

function run() {
var index = 0;
next();
function next(error) {
if (error) {
done(error);
} else if (index === tests.length) {
done(null);
} else {
tests[index++].run(next);
}
var index = 0;
next();
function next(error) {
if (error) {
done(error);
} else if (index === tests.length) {
done(null);
} else {
tests[index++].run(next);
}
function done(error) {
if (error) {
throw error;
}
global.global_test_results = {
passed: !failed
};
}
function done(error) {
if (error) {
throw error;
}
global.global_test_results = {
passed: !failed
};
}
}

function it(name, callback) {
tests.push(new Test(name, callback));
tests.push(new Test(name, callback));
}

function expect(value) {
return new Expectation(value, currentTest);
return new Expectation(value, currentTest);
}

function Test(name, callback) {
this.name = name;
this.callback = callback;
this.failed = false;
this.name = name;
this.callback = callback;
this.failed = false;
}

Test.prototype.run = function (done) {
var self = this;
currentTest = this;
this.callback(function (error) {
if (error) {
done(error);
}
if (self.failed) {
failed++;
} else {
passed++;
}
done();
});
var self = this;
currentTest = this;
this.callback(function (error) {
if (error) {
done(error);
}
if (self.failed) {
failed++;
} else {
passed++;
}
done();
});
};

function Expectation(value, test) {
this.value = value;
this.test = test;
this.value = value;
this.test = test;
}

Expectation.prototype.toBe = function (value) {
var ok = this.value === value;
if (!ok) {
this.test.failed = true;
}
var ok = this.value === value;
if (!ok) {
this.test.failed = true;
}
};

Expectation.prototype.toEqual = function (value) {
var ok = equals(this.value, value);
if (!ok) {
this.test.failed = true;
}
var ok = equals(this.value, value);
if (!ok) {
this.test.failed = true;
}
};

function equals(a, b) {
if (isArray(a)) {
if (!isArray(b)) return false;
if (a.length !== b.length) return false;
for (var index = 0; index < a.length; index++) {
if (!equals(a[index], b[index])) {
return false;
}
}
return true;
} else {
return a === b;
if (isArray(a)) {
if (!isArray(b)) return false;
if (a.length !== b.length) return false;
for (var index = 0; index < a.length; index++) {
if (!equals(a[index], b[index])) {
return false;
}
}
return true;
} else {
return a === b;
}
}

function isArray(object) {
return typeof object === "object" && Object.prototype.toString.call(object) === "[object Array]";
}

return typeof object === "object" && Object.prototype.toString.call(object) === "[object Array]";
}
8 changes: 7 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var global = Function('return this')(),
window = global.window,
document = global.document;

/* Use global nextTask for browser tests with different "use". */
/** Use global nextTask for browser tests with different "use". */
if (global.nextTask) nextTask = global.nextTask;

var isNode = ({}).toString.call(global.process) === '[object process]';
Expand All @@ -26,6 +26,12 @@ if (isNode) {
document.body.insertBefore(p, document.body.firstChild);
}

/**
* Throw error, if value in not true.
* @param {*} value
* @param {string} msg
* @throws {Error}
*/
function assert(value, msg) {
if (value !== true) throw Error('Assert ' + (msg || ''));
}
Expand Down

0 comments on commit 3dd6c4d

Please sign in to comment.