-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.json
50 lines (50 loc) · 6.47 KB
/
package.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{
"name": "setimmediate",
"description": "A shim for the setImmediate efficient script yielding API",
"version": "1.0.2",
"author": {
"name": "YuzuJS"
},
"contributors": [
{
"name": "Domenic Denicola",
"email": "domenic@domenicdenicola.com",
"url": "http://domenic.me"
},
{
"name": "Donavon West",
"email": "github@donavon.com",
"url": "http://donavon.com"
},
{
"name": "Yaffle"
}
],
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/YuzuJS/setImmediate.git"
},
"bugs": {
"url": "https://github.com/YuzuJS/setImmediate/issues"
},
"main": "setImmediate.js",
"scripts": {
"lint": "jshint setImmediate.js",
"test": "mocha test/tests.js",
"test-browser": "opener http://localhost:9008/__zuul && zuul test/tests.js --ui mocha-bdd --local 9008",
"test-browser-only": "opener http://localhost:9007/test/browserOnly/index.html && http-server . -p 9007"
},
"devDependencies": {
"jshint": "^2.5.0",
"mocha": "~1.18.2",
"http-server": "~0.6.1",
"opener": "^1.3",
"zuul": "^1.6.4"
},
"readme": "# setImmediate.js\n**A YuzuJS production**\n\n## Introduction\n\n**setImmediate.js** is a highly cross-browser implementation of the `setImmediate` and `clearImmediate` APIs, [proposed][spec] by Microsoft to the Web Performance Working Group. `setImmediate` allows scripts to yield to the browser, executing a given operation asynchronously, in a manner that is typically more efficient and consumes less power than the usual `setTimeout(..., 0)` pattern.\n\nsetImmediate.js runs at “full speed” in the following browsers and environments, using various clever tricks:\n\n * Internet Explorer 6+\n * Firefox 3+\n * WebKit\n * Opera 9.5+\n * Node.js\n * Web workers in browsers that support `MessageChannel`, which I can't find solid info on.\n\nIn all other browsers we fall back to using `setTimeout`, so it's always safe to use.\n\n## Macrotasks and Microtasks\n\nThe `setImmediate` API, as specified, gives you access to the environment's [task queue][], sometimes known as its \"macrotask\" queue. This is crucially different from the [microtask queue][] used by web features such as `MutationObserver`, language features such as promises and `Object.observe`, and Node.js features such as `process.nextTick`. Each go-around of the macrotask queue yields back to the event loop once all queued tasks have been processed, even if the macrotask itself queued more macrotasks. Whereas, the microtask queue will continue executing any queued microtasks until it is exhausted.\n\nIn practice, what this means is that if you call `setImmediate` inside of another task queued with `setImmediate`, you will yield back to the event loop and any I/O or rendering tasks that need to take place between those calls, instead of executing the queued task as soon as possible.\n\nIf you are looking specifically to yield as part of a render loop, consider using [`requestAnimationFrame`][raf]; if you are looking solely for the control-flow ordering effects, use a microtask solution such as [asap][].\n\n## The Tricks\n\n### `process.nextTick`\n\nIn Node.js versions below 0.9, `setImmediate` is not available, but [`process.nextTick`][nextTIck] is, so we use it to shim support for a global `setImmediate`. In Node.js 0.9 and above, `setImmediate` is available.\n\nNote that we check for *actual* Node.js environments, not emulated ones like those produced by browserify or similar. Such emulated environments often already include a `process.nextTick` shim that's not as browser-compatible as setImmediate.js.\n\n### `postMessage`\n\nIn Firefox 3+, Internet Explorer 9+, all modern WebKit browsers, and Opera 9.5+, [`postMessage`][postMessage] is available and provides a good way to queue tasks on the event loop. It's quite the abuse, using a cross-document messaging protocol within the same document simply to get access to the event loop task queue, but until there are native implementations, this is the best option.\n\nNote that Internet Explorer 8 includes a synchronous version of `postMessage`. We detect this, or any other such synchronous implementation, and fall back to another trick.\n\n### `MessageChannel`\n\nUnfortunately, `postMessage` has completely different semantics inside web workers, and so cannot be used there. So we turn to [`MessageChannel`][MessageChannel], which has worse browser support, but does work inside a web worker.\n\n### `<script> onreadystatechange`\n\nFor our last trick, we pull something out to make things fast in Internet Explorer versions 6 through 8: namely, creating a `<script>` element and firing our calls in its `onreadystatechange` event. This does execute in a future turn of the event loop, and is also faster than `setTimeout(…, 0)`, so hey, why not?\n\n## Usage\n\nIn the browser, include it with a `<script>` tag; pretty simple.\n\nIn Node.js, do\n\n```\nnpm install setimmediate\n```\n\nthen\n\n```js\nrequire(\"setimmediate\");\n```\n\nsomewhere early in your app; it attaches to the global.\n\n## Demo\n\n* [Quick sort demo][cross-browser-demo]\n\n## Reference and Reading\n\n * [Efficient Script Yielding W3C Editor's Draft][spec]\n * [W3C mailing list post introducing the specification][list-post]\n * [IE Test Drive demo][ie-demo]\n * [Introductory blog post by Nicholas C. Zakas][ncz]\n\n\n[spec]: https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/setImmediate/Overview.html\n[task queue]: http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#task-queue\n[microtask queue]: http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#perform-a-microtask-checkpoint\n[raf]: www.w3.org/TR/animation-timing/\n[asap]: https://github.com/kriskowal/asap\n[list-post]: http://lists.w3.org/Archives/Public/public-web-perf/2011Jun/0100.html\n[ie-demo]: http://ie.microsoft.com/testdrive/Performance/setImmediateSorting/Default.html\n[ncz]: http://www.nczonline.net/blog/2011/09/19/script-yielding-with-setimmediate/\n[nextTick]: http://nodejs.org/docs/v0.8.16/api/process.html#process_process_nexttick_callback\n[postMessage]: http://www.whatwg.org/specs/web-apps/current-work/multipage/web-messaging.html#posting-messages\n[MessageChannel]: http://www.whatwg.org/specs/web-apps/current-work/multipage/web-messaging.html#channel-messaging\n[cross-browser-demo]: http://jphpsf.github.com/setImmediate-shim-demo\n",
"readmeFilename": "README.md",
"homepage": "https://github.com/YuzuJS/setImmediate",
"_id": "setimmediate@1.0.2",
"_from": "setimmediate@~1.0.1"
}