Skip to content

Commit 75fcad3

Browse files
authored
Merge pull request #649 from share/next-tick-check
♻️ Check `nextTick` polyfill once at module level
2 parents 8ba28c5 + 22ce801 commit 75fcad3

File tree

4 files changed

+60
-108
lines changed

4 files changed

+60
-108
lines changed

lib/next-tick.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
exports.messageChannel = function() {
2+
var triggerCallback = createNextTickTrigger(arguments);
3+
var channel = new MessageChannel();
4+
channel.port1.onmessage = function() {
5+
triggerCallback();
6+
channel.port1.close();
7+
};
8+
channel.port2.postMessage('');
9+
};
10+
11+
exports.setTimeout = function() {
12+
var triggerCallback = createNextTickTrigger(arguments);
13+
setTimeout(triggerCallback);
14+
};
15+
16+
function createNextTickTrigger(args) {
17+
var callback = args[0];
18+
var _args = [];
19+
for (var i = 1; i < args.length; i++) {
20+
_args[i - 1] = args[i];
21+
}
22+
23+
return function triggerCallback() {
24+
callback.apply(null, _args);
25+
};
26+
}

lib/util.js

+8-25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var nextTickImpl = require('./next-tick');
12

23
exports.doNothing = doNothing;
34
function doNothing() {}
@@ -85,31 +86,13 @@ exports.truthy = function(arg) {
8586
return !!arg;
8687
};
8788

88-
exports.nextTick = function(callback) {
89-
if (typeof process !== 'undefined' && process.nextTick) {
90-
return process.nextTick.apply(null, arguments);
91-
}
92-
93-
var args = [];
94-
for (var i = 1; i < arguments.length; i++) {
95-
args[i - 1] = arguments[i];
96-
}
97-
98-
if (typeof MessageChannel === 'undefined') {
99-
return setTimeout(triggerCallback);
100-
}
101-
102-
var channel = new MessageChannel();
103-
channel.port1.onmessage = function() {
104-
triggerCallback();
105-
channel.port1.close();
106-
};
107-
channel.port2.postMessage('');
108-
109-
function triggerCallback() {
110-
callback.apply(null, args);
111-
}
112-
};
89+
if (typeof process !== 'undefined' && typeof process.nextTick === 'function') {
90+
exports.nextTick = process.nextTick;
91+
} else if (typeof MessageChannel !== 'undefined') {
92+
exports.nextTick = nextTickImpl.messageChannel;
93+
} else {
94+
exports.nextTick = nextTickImpl.setTimeout;
95+
}
11396

11497
exports.clone = function(obj) {
11598
return (obj === undefined) ? undefined : JSON.parse(JSON.stringify(obj));

test/next-tick.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var nextTickImpl = require('../lib/next-tick');
2+
var expect = require('chai').expect;
3+
4+
describe('nextTick', function() {
5+
['messageChannel', 'setTimeout'].forEach(function(name) {
6+
var tick = nextTickImpl[name];
7+
8+
it('passes args', function(done) {
9+
tick(function(arg1, arg2, arg3) {
10+
expect(arg1).to.equal('foo');
11+
expect(arg2).to.equal(123);
12+
expect(arg3).to.be.undefined;
13+
done();
14+
}, 'foo', 123);
15+
});
16+
17+
it('calls asynchronously', function(done) {
18+
var called = false;
19+
tick(function() {
20+
called = true;
21+
done();
22+
});
23+
expect(called).to.be.false;
24+
});
25+
});
26+
});

test/util-test.js

-83
This file was deleted.

0 commit comments

Comments
 (0)