[Pkg-javascript-commits] [node-setimmediate] 02/04: New upstream version 1.0.5
Thorsten Alteholz
alteholz at moszumanska.debian.org
Sat Nov 5 12:29:43 UTC 2016
This is an automated email from the git hooks/post-receive script.
alteholz pushed a commit to branch master
in repository node-setimmediate.
commit 98415e1e5b9119cf6de32071eb69f1321fcab575
Author: Thorsten Alteholz <debian at alteholz.de>
Date: Sat Nov 5 13:03:56 2016 +0100
New upstream version 1.0.5
---
.gitignore | 2 +
.jshintrc | 22 +++++++++
README.md | 5 +-
package.json | 5 +-
setImmediate.js | 87 ++++++++++++++++++---------------
test/browserOnly/index.html | 16 +++++++
test/browserOnly/selfClose.htm | 14 ++++++
test/browserOnly/tests.js | 45 +++++++++++++++++
test/browserOnly/worker.js | 5 ++
test/mocha.opts | 1 +
test/tests.js | 106 +++++++++++++++++++++++++++++++++++++++++
11 files changed, 266 insertions(+), 42 deletions(-)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0c0aa04
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/node_modules/
+/npm-debug.log
diff --git a/.jshintrc b/.jshintrc
new file mode 100644
index 0000000..264bfdb
--- /dev/null
+++ b/.jshintrc
@@ -0,0 +1,22 @@
+{
+ "camelcase": true,
+ "curly": true,
+ "eqeqeq": true,
+ "evil": true,
+ "globalstrict": true,
+ "immed": true,
+ "indent": 4,
+ "latedef": true,
+ "newcap": true,
+ "noarg": true,
+ "node": true,
+ "quotmark": "double",
+ "trailing": true,
+ "undef": true,
+ "unused": true,
+ "white": true,
+ "globals": {
+ "MessageChannel": false,
+ "self": false
+ }
+}
diff --git a/README.md b/README.md
index ad0f68d..7826339 100644
--- a/README.md
+++ b/README.md
@@ -55,16 +55,15 @@ In the browser, include it with a `<script>` tag; pretty simple.
In Node.js, do
```
-npm install setimmediate
+npm install --save setimmediate
```
then
```js
-require("setimmediate");
+require("setimmediate"); // (somewhere early in your app; it attaches to the global scope.)
```
-somewhere early in your app; it attaches to the global.
## Demo
diff --git a/package.json b/package.json
index e5dd50c..9b211e4 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"name": "setimmediate",
"description": "A shim for the setImmediate efficient script yielding API",
- "version": "1.0.4",
+ "version": "1.0.5",
"author": "YuzuJS",
"contributors": [
"Domenic Denicola <d at domenic.me> (https://domenic.me)",
@@ -11,6 +11,9 @@
"license": "MIT",
"repository": "YuzuJS/setImmediate",
"main": "setImmediate.js",
+ "files": [
+ "setImmediate.js"
+ ],
"scripts": {
"lint": "jshint setImmediate.js",
"test": "mocha test/tests.js",
diff --git a/setImmediate.js b/setImmediate.js
index 5abe55c..3c1b06e 100644
--- a/setImmediate.js
+++ b/setImmediate.js
@@ -9,24 +9,49 @@
var tasksByHandle = {};
var currentlyRunningATask = false;
var doc = global.document;
- var setImmediate;
+ var registerImmediate;
+
+ function setImmediate(callback) {
+ // Callback can either be a function or a string
+ if (typeof callback !== "function") {
+ callback = new Function("" + callback);
+ }
+ // Copy function arguments
+ var args = new Array(arguments.length - 1);
+ for (var i = 0; i < args.length; i++) {
+ args[i] = arguments[i + 1];
+ }
+ // Store and register the task
+ var task = { callback: callback, args: args };
+ tasksByHandle[nextHandle] = task;
+ registerImmediate(nextHandle);
+ return nextHandle++;
+ }
- function addFromSetImmediateArguments(args) {
- tasksByHandle[nextHandle] = partiallyApplied.apply(undefined, args);
- return nextHandle++;
+ function clearImmediate(handle) {
+ delete tasksByHandle[handle];
}
- // This function accepts the same arguments as setImmediate, but
- // returns a function that requires no arguments.
- function partiallyApplied(handler) {
- var args = [].slice.call(arguments, 1);
- return function() {
- if (typeof handler === "function") {
- handler.apply(undefined, args);
- } else {
- (new Function("" + handler))();
- }
- };
+ function run(task) {
+ var callback = task.callback;
+ var args = task.args;
+ switch (args.length) {
+ case 0:
+ callback();
+ break;
+ case 1:
+ callback(args[0]);
+ break;
+ case 2:
+ callback(args[0], args[1]);
+ break;
+ case 3:
+ callback(args[0], args[1], args[2]);
+ break;
+ default:
+ callback.apply(undefined, args);
+ break;
+ }
}
function runIfPresent(handle) {
@@ -35,13 +60,13 @@
if (currentlyRunningATask) {
// Delay by doing a setTimeout. setImmediate was tried instead, but in Firefox 7 it generated a
// "too much recursion" error.
- setTimeout(partiallyApplied(runIfPresent, handle), 0);
+ setTimeout(runIfPresent, 0, handle);
} else {
var task = tasksByHandle[handle];
if (task) {
currentlyRunningATask = true;
try {
- task();
+ run(task);
} finally {
clearImmediate(handle);
currentlyRunningATask = false;
@@ -50,15 +75,9 @@
}
}
- function clearImmediate(handle) {
- delete tasksByHandle[handle];
- }
-
function installNextTickImplementation() {
- setImmediate = function() {
- var handle = addFromSetImmediateArguments(arguments);
- process.nextTick(partiallyApplied(runIfPresent, handle));
- return handle;
+ registerImmediate = function(handle) {
+ process.nextTick(function () { runIfPresent(handle); });
};
}
@@ -97,10 +116,8 @@
global.attachEvent("onmessage", onGlobalMessage);
}
- setImmediate = function() {
- var handle = addFromSetImmediateArguments(arguments);
+ registerImmediate = function(handle) {
global.postMessage(messagePrefix + handle, "*");
- return handle;
};
}
@@ -111,17 +128,14 @@
runIfPresent(handle);
};
- setImmediate = function() {
- var handle = addFromSetImmediateArguments(arguments);
+ registerImmediate = function(handle) {
channel.port2.postMessage(handle);
- return handle;
};
}
function installReadyStateChangeImplementation() {
var html = doc.documentElement;
- setImmediate = function() {
- var handle = addFromSetImmediateArguments(arguments);
+ registerImmediate = function(handle) {
// Create a <script> element; its readystatechange event will be fired asynchronously once it is inserted
// into the document. Do so, thus queuing up the task. Remember to clean up once it's been called.
var script = doc.createElement("script");
@@ -132,15 +146,12 @@
script = null;
};
html.appendChild(script);
- return handle;
};
}
function installSetTimeoutImplementation() {
- setImmediate = function() {
- var handle = addFromSetImmediateArguments(arguments);
- setTimeout(partiallyApplied(runIfPresent, handle), 0);
- return handle;
+ registerImmediate = function(handle) {
+ setTimeout(runIfPresent, 0, handle);
};
}
diff --git a/test/browserOnly/index.html b/test/browserOnly/index.html
new file mode 100644
index 0000000..e04a9bb
--- /dev/null
+++ b/test/browserOnly/index.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="utf-8" />
+ <title>Real-Browser Only setImmediate Tests</title>
+ <link rel="stylesheet" href="../../node_modules/mocha/mocha.css" />
+ </head>
+ <body>
+ <div id="mocha"></div>
+ <script src="../../node_modules/mocha/mocha.js"></script>
+ <script>mocha.setup({ ui: "bdd", slow: Infinity });</script>
+ <script src="../../setImmediate.js"></script>
+ <script src="./tests.js"></script>
+ <script>mocha.run();</script>
+ </body>
+</html>
diff --git a/test/browserOnly/selfClose.htm b/test/browserOnly/selfClose.htm
new file mode 100644
index 0000000..a90e259
--- /dev/null
+++ b/test/browserOnly/selfClose.htm
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Modal Dialog for Testing</title>
+</head>
+<body>
+ <p>This'll be gone in one second; we're just testing the interaction of setImmediate and <code>window.showModalDialog</code>.</p>
+ <script>
+ setTimeout(function () {
+ window.close();
+ }, 1000);
+ </script>
+</body>
+</html>
diff --git a/test/browserOnly/tests.js b/test/browserOnly/tests.js
new file mode 100644
index 0000000..0567fb0
--- /dev/null
+++ b/test/browserOnly/tests.js
@@ -0,0 +1,45 @@
+"use strict";
+/*global setImmediate: false, specify: false, window: false */
+
+function assert(condition) {
+ if (!condition) {
+ throw new Error("Assertion failed");
+ }
+}
+assert.strictEqual = function (x, y) {
+ if (x !== y) {
+ throw new Error(x + " !== " + y);
+ }
+};
+
+specify("Modal dialogs block handlers", function (done) {
+ // Try to launch the less-annoying self-closing-window modal dialog; if that's not an option, fall back to alert.
+ var showTheDialog = window.showModalDialog ?
+ function () {
+ window.showModalDialog("selfClose.htm");
+ }
+ : function () {
+ window.alert("Please press OK to continue the test; we needed a modal dialog.");
+ };
+
+ var dialogClosed = false;
+ setImmediate(function () {
+ showTheDialog();
+ dialogClosed = true;
+ });
+
+ setImmediate(function () {
+ assert(dialogClosed);
+ done();
+ });
+});
+
+if (typeof window.Worker === "function") {
+ specify("When inside a web worker context, setImmediate calls the passed handler", function (done) {
+ var worker = new window.Worker("worker.js");
+ worker.addEventListener("message", function (event) {
+ assert.strictEqual(event.data, "TEST");
+ done();
+ }, false);
+ });
+}
diff --git a/test/browserOnly/worker.js b/test/browserOnly/worker.js
new file mode 100644
index 0000000..68574a5
--- /dev/null
+++ b/test/browserOnly/worker.js
@@ -0,0 +1,5 @@
+importScripts("../../setImmediate.js");
+
+setImmediate(function () {
+ self.postMessage("TEST");
+});
diff --git a/test/mocha.opts b/test/mocha.opts
new file mode 100644
index 0000000..6fb66f7
--- /dev/null
+++ b/test/mocha.opts
@@ -0,0 +1 @@
+--slow Infinity
diff --git a/test/tests.js b/test/tests.js
new file mode 100644
index 0000000..24f0148
--- /dev/null
+++ b/test/tests.js
@@ -0,0 +1,106 @@
+"use strict";
+/*global setImmediate: false, clearImmediate: false, specify: false, window: false */
+
+// The Node version of setImmediate does not support string handlers.
+var global = Function("return this")();
+var originalGlobalSetImmediate = global.setImmediate;
+if (originalGlobalSetImmediate) {
+ global.setImmediate = function(handler) {
+ var args = arguments;
+ if (typeof handler !== "function") {
+ handler = args[0] = eval.bind(null, "" + handler);
+ }
+ return originalGlobalSetImmediate.apply(this, args);
+ };
+}
+
+var assert = require("assert");
+require("../setImmediate");
+
+specify("Handlers do execute", function (done) {
+ setImmediate(function () {
+ done();
+ });
+});
+
+specify("Handlers do not execute in the same event loop turn as the call to `setImmediate`", function (done) {
+ var handlerCalled = false;
+ function handler() {
+ handlerCalled = true;
+ done();
+ }
+
+ setImmediate(handler);
+ assert(!handlerCalled);
+});
+
+specify("Handlers can be strings", function(done) {
+ var property = "handler$" + Math.random().toString(36).slice(2);
+ done.called = false;
+ setImmediate[property] = function () {
+ delete setImmediate[property];
+
+ done();
+ };
+
+ setImmediate("setImmediate." + property + ".called = true; setImmediate." + property + "()");
+ assert.strictEqual(done.called, false);
+});
+
+specify("`setImmediate` passes through an argument to the handler", function (done) {
+ var expectedArg = { expected: true };
+
+ function handler(actualArg) {
+ assert.strictEqual(actualArg, expectedArg);
+ done();
+ }
+
+ setImmediate(handler, expectedArg);
+});
+
+specify("`setImmediate` passes through two arguments to the handler", function (done) {
+ var expectedArg1 = { arg1: true };
+ var expectedArg2 = { arg2: true };
+
+ function handler(actualArg1, actualArg2) {
+ assert.strictEqual(actualArg1, expectedArg1);
+ assert.strictEqual(actualArg2, expectedArg2);
+ done();
+ }
+
+ setImmediate(handler, expectedArg1, expectedArg2);
+});
+
+specify("`clearImmediate` within the same event loop turn prevents the handler from executing", function (done) {
+ var handlerCalled = false;
+ function handler() {
+ handlerCalled = true;
+ }
+
+ var handle = setImmediate(handler);
+ clearImmediate(handle);
+
+ setTimeout(function () {
+ assert(!handlerCalled);
+ done();
+ }, 100);
+});
+
+specify("`clearImmediate` does not interfere with handlers other than the one with ID passed to it", function (done) {
+ var expectedArgs = ["A", "D"];
+ var recordedArgs = [];
+ function handler(arg) {
+ recordedArgs.push(arg);
+ }
+
+ setImmediate(handler, "A");
+ clearImmediate(setImmediate(handler, "B"));
+ var handle = setImmediate(handler, "C");
+ setImmediate(handler, "D");
+ clearImmediate(handle);
+
+ setTimeout(function () {
+ assert.deepEqual(recordedArgs, expectedArgs);
+ done();
+ }, 100);
+});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-setimmediate.git
More information about the Pkg-javascript-commits
mailing list