Skip to content

Commit

Permalink
Merge pull request #649 from share/next-tick-check
Browse files Browse the repository at this point in the history
♻️ Check `nextTick` polyfill once at module level
  • Loading branch information
alecgibson authored Apr 2, 2024
2 parents 8ba28c5 + 22ce801 commit 75fcad3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 108 deletions.
26 changes: 26 additions & 0 deletions lib/next-tick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
exports.messageChannel = function() {
var triggerCallback = createNextTickTrigger(arguments);
var channel = new MessageChannel();
channel.port1.onmessage = function() {
triggerCallback();
channel.port1.close();
};
channel.port2.postMessage('');
};

exports.setTimeout = function() {
var triggerCallback = createNextTickTrigger(arguments);
setTimeout(triggerCallback);
};

function createNextTickTrigger(args) {
var callback = args[0];
var _args = [];
for (var i = 1; i < args.length; i++) {
_args[i - 1] = args[i];
}

return function triggerCallback() {
callback.apply(null, _args);
};
}
33 changes: 8 additions & 25 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var nextTickImpl = require('./next-tick');

exports.doNothing = doNothing;
function doNothing() {}
Expand Down Expand Up @@ -85,31 +86,13 @@ exports.truthy = function(arg) {
return !!arg;
};

exports.nextTick = function(callback) {
if (typeof process !== 'undefined' && process.nextTick) {
return process.nextTick.apply(null, arguments);
}

var args = [];
for (var i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}

if (typeof MessageChannel === 'undefined') {
return setTimeout(triggerCallback);
}

var channel = new MessageChannel();
channel.port1.onmessage = function() {
triggerCallback();
channel.port1.close();
};
channel.port2.postMessage('');

function triggerCallback() {
callback.apply(null, args);
}
};
if (typeof process !== 'undefined' && typeof process.nextTick === 'function') {
exports.nextTick = process.nextTick;
} else if (typeof MessageChannel !== 'undefined') {
exports.nextTick = nextTickImpl.messageChannel;
} else {
exports.nextTick = nextTickImpl.setTimeout;
}

exports.clone = function(obj) {
return (obj === undefined) ? undefined : JSON.parse(JSON.stringify(obj));
Expand Down
26 changes: 26 additions & 0 deletions test/next-tick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var nextTickImpl = require('../lib/next-tick');
var expect = require('chai').expect;

describe('nextTick', function() {
['messageChannel', 'setTimeout'].forEach(function(name) {
var tick = nextTickImpl[name];

it('passes args', function(done) {
tick(function(arg1, arg2, arg3) {
expect(arg1).to.equal('foo');
expect(arg2).to.equal(123);
expect(arg3).to.be.undefined;
done();
}, 'foo', 123);
});

it('calls asynchronously', function(done) {
var called = false;
tick(function() {
called = true;
done();
});
expect(called).to.be.false;
});
});
});
83 changes: 0 additions & 83 deletions test/util-test.js

This file was deleted.

0 comments on commit 75fcad3

Please sign in to comment.