Skip to content

Commit

Permalink
Fall back to setTimeout when setImmediate is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
dominykas committed Apr 19, 2019
1 parent b23ae1c commit 0650e29
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"scripts": {
"lint": "eslint .",
"test-node": "mocha test/ integration-test/ -R dot --check-leaks",
"test-headless": "mochify",
"test-headless": "mochify --timeout=10000",
"test-cloud": "mochify --wd",
"test": "npm run lint && npm run test-node && npm run test-headless",
"bundle": "browserify -s lolex -o lolex.js src/lolex-src.js",
Expand Down
16 changes: 8 additions & 8 deletions src/lolex-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ function withGlobal(_global) {
};


var globalSetImmediate = global.setImmediate;
var originalSetTimeout = _global.setImmediate || _global.setTimeout;

/**
* @param start {Date|number} the system time - non-integer values are floored
Expand Down Expand Up @@ -722,7 +722,7 @@ function withGlobal(_global) {
// finish up after native setImmediate callback to allow
// all native es6 promises to process their callbacks after
// each timer fires.
globalSetImmediate(nextPromiseTick);
originalSetTimeout(nextPromiseTick);
return;
}

Expand All @@ -742,7 +742,7 @@ function withGlobal(_global) {
}
clock.duringTick = false;

// corner case: during runJobs, new timers were scheduled which could be in the range [clock.now, tickTo]
// corner case: during runJobs new timers were scheduled which could be in the range [clock.now, tickTo]
timer = firstTimerInRange(clock, tickFrom, tickTo);
if (timer) {
try {
Expand Down Expand Up @@ -805,7 +805,7 @@ function withGlobal(_global) {
if (typeof global.Promise !== "undefined") {
clock.tickAsync = function tickAsync(ms) {
return new global.Promise(function (resolve, reject) {
globalSetImmediate(function () {
originalSetTimeout(function () {
try {
doTick(ms, true, resolve, reject);
} catch (e) {
Expand Down Expand Up @@ -837,7 +837,7 @@ function withGlobal(_global) {
if (typeof global.Promise !== "undefined") {
clock.nextAsync = function nextAsync() {
return new global.Promise(function (resolve, reject) {
globalSetImmediate(function () {
originalSetTimeout(function () {
try {
var timer = firstTimer(clock);
if (!timer) {
Expand All @@ -855,7 +855,7 @@ function withGlobal(_global) {
}
clock.duringTick = false;

globalSetImmediate(function () {
originalSetTimeout(function () {
if (err) {
reject(err);
} else {
Expand Down Expand Up @@ -898,7 +898,7 @@ function withGlobal(_global) {
return new global.Promise(function (resolve, reject) {
var i = 0;
function doRun() {
globalSetImmediate(function () {
originalSetTimeout(function () {
try {
var numTimers;
if (i < clock.loopLimit) {
Expand Down Expand Up @@ -946,7 +946,7 @@ function withGlobal(_global) {
if (typeof global.Promise !== "undefined") {
clock.runToLastAsync = function runToLastAsync() {
return new global.Promise(function (resolve, reject) {
globalSetImmediate(function () {
originalSetTimeout(function () {
try {
var timer = lastTimer(clock);
if (!timer) {
Expand Down
6 changes: 3 additions & 3 deletions test/lolex-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ describe("lolex", function () {
describe("tickAsync", function () {

beforeEach(function () {
this.clock = lolex.install(0);
this.clock = lolex.install();
});

afterEach(function () {
Expand Down Expand Up @@ -1846,7 +1846,7 @@ describe("lolex", function () {
describe("nextAsync", function () {

beforeEach(function () {
this.clock = lolex.install(0);
this.clock = lolex.install();
});

afterEach(function () {
Expand Down Expand Up @@ -2400,7 +2400,7 @@ describe("lolex", function () {
});

it("the loop limit can be set when installing a clock", function () {
this.clock = lolex.install(0, null, null, 1);
this.clock = lolex.install({ loopLimit: 1 });
var test = this;
var catchSpy = sinon.spy();

Expand Down

0 comments on commit 0650e29

Please sign in to comment.