Skip to content

Commit

Permalink
Remove setNow from realtime-callbacks.ts (#2509)
Browse files Browse the repository at this point in the history
Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
  • Loading branch information
SimonBrandner committed Jul 10, 2022
1 parent 3935152 commit 9a6dccb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 26 deletions.
10 changes: 1 addition & 9 deletions spec/unit/realtime-callbacks.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import * as callbacks from "../../src/realtime-callbacks";

let wallTime = 1234567890;
jest.useFakeTimers();
jest.useFakeTimers().setSystemTime(wallTime);

describe("realtime-callbacks", function() {
function tick(millis) {
wallTime += millis;
jest.advanceTimersByTime(millis);
}

beforeEach(function() {
callbacks.setNow(() => wallTime);
});

afterEach(function() {
callbacks.setNow();
});

describe("setTimeout", function() {
it("should call the callback after the timeout", function() {
const callback = jest.fn();
Expand Down
20 changes: 3 additions & 17 deletions src/realtime-callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,6 @@ const callbackList: {
// var debuglog = logger.log.bind(logger);
const debuglog = function(...params: any[]) {};

/**
* Replace the function used by this module to get the current time.
*
* Intended for use by the unit tests.
*
* @param {function} [f] function which should return a millisecond counter
*
* @internal
*/
export function setNow(f: () => number): void {
now = f || Date.now;
}
let now = Date.now;

/**
* reimplementation of window.setTimeout, which will call the callback if
* the wallclock time goes past the deadline.
Expand All @@ -78,7 +64,7 @@ export function setTimeout(func: (...params: any[]) => void, delayMs: number, ..
delayMs = 0;
}

const runAt = now() + delayMs;
const runAt = Date.now() + delayMs;
const key = count++;
debuglog("setTimeout: scheduling cb", key, "at", runAt,
"(delay", delayMs, ")");
Expand Down Expand Up @@ -141,7 +127,7 @@ function scheduleRealCallback(): void {
return;
}

const timestamp = now();
const timestamp = Date.now();
const delayMs = Math.min(first.runAt - timestamp, TIMER_CHECK_PERIOD_MS);

debuglog("scheduleRealCallback: now:", timestamp, "delay:", delayMs);
Expand All @@ -150,7 +136,7 @@ function scheduleRealCallback(): void {

function runCallbacks(): void {
let cb;
const timestamp = now();
const timestamp = Date.now();
debuglog("runCallbacks: now:", timestamp);

// get the list of things to call
Expand Down

0 comments on commit 9a6dccb

Please sign in to comment.