Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make now configurable so time can freeze during backburner run loop #263

Merged
merged 1 commit into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import iteratorDrain from './backburner/iterator-drain';

import Queue, { QUEUE_STATE } from './backburner/queue';

const now = Date.now;
const noop = function() {};

export default class Backburner {
Expand Down Expand Up @@ -43,6 +42,7 @@ export default class Backburner {
clearTimeout(id: number): void;
next(fn: () => void): number;
clearNext(fn): void;
now(): number;
};

private _boundRunExpiredTimers: () => void;
Expand Down Expand Up @@ -75,6 +75,7 @@ export default class Backburner {
platform.clearTimeout = _platform.clearTimeout || ((id) => clearTimeout(id));
platform.next = _platform.next || ((fn) => platform.setTimeout(fn, 0));
platform.clearNext = _platform.clearNext || platform.clearTimeout;
platform.now = _platform.now || Date.now;

this._platform = platform;

Expand Down Expand Up @@ -436,7 +437,7 @@ export default class Backburner {
}

let onError = getOnError(this.options);
let executeAt = now() + wait;
let executeAt = this._platform.now() + wait;

let fn;
if (onError) {
Expand Down Expand Up @@ -679,7 +680,7 @@ export default class Backburner {
let l = timers.length;
let i = 0;
let defaultQueue = this.options.defaultQueue;
let n = now();
let n = this._platform.now();
for (; i < l; i += 2) {
let executeAt = timers[i];
if (executeAt <= n) {
Expand Down Expand Up @@ -708,7 +709,7 @@ export default class Backburner {
private _installTimerTimeout() {
if (this._timers.length === 0) { return; }
let minExpiresAt = this._timers[0];
let n = now();
let n = this._platform.now();
let wait = Math.max(0, minExpiresAt - n);
this._timerTimeoutId = this._platform.setTimeout(this._boundRunExpiredTimers, wait);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/configurable-timeout-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,26 @@ QUnit.test('We can use a custom clearTimeout', function(assert) {
});
});
});

QUnit.test('We can use a custom now', function(assert) {
assert.expect(2);
let done = assert.async();

let currentTime = 10;
let customNowWasUsed = false;
let bb = new Backburner(['one'], {
_platform: {
now() {
customNowWasUsed = true;
return currentTime += 10;
},
isFakePlatform: true
}
});

bb.later(() => {
assert.ok(bb.options._platform.isFakePlatform, 'we are using the fake platform');
assert.ok(customNowWasUsed , 'custom now was used');
done();
}, 10);
});