diff --git a/src/now.ts b/src/now.ts index 05217b4..bbce10a 100644 --- a/src/now.ts +++ b/src/now.ts @@ -5,13 +5,13 @@ const tickers: { [interval: string]: IResource } = {} -let synchronized = true - -/** - * Turns of the synchronization of {@link now} for unit testing so that tests don't share state between them - */ -export function desynchronizeNowForTests() { - synchronized = false +export function clearTimers() { + for (const key in tickers) { + if (tickers.hasOwnProperty(key)) { + tickers[key].dispose() + delete tickers[key] + } + } } /** @@ -46,21 +46,11 @@ export function now(interval: number | "frame" = 1000) { return Date.now() } - if (!synchronized) { - if (typeof interval === "number") { - return createIntervalTicker(interval).current() - } - - return createAnimationFrameTicker().current() + if (!tickers[interval]) { + if (typeof interval === "number") tickers[interval] = createIntervalTicker(interval) + else tickers[interval] = createAnimationFrameTicker() } - - const timer = tickers[interval] - ? tickers[interval] - : typeof interval === "number" - ? createIntervalTicker(interval) - : createAnimationFrameTicker() - - return timer.current() + return tickers[interval].current() } function createIntervalTicker(interval: number): IResource { diff --git a/test/now.js b/test/now.js index b19ded2..d9cb877 100644 --- a/test/now.js +++ b/test/now.js @@ -62,8 +62,6 @@ describe("given desynchronization is enabled", () => { jest.useFakeTimers("modern") jest.setSystemTime(new Date("2015-10-21T07:28:00Z")) - utils.desynchronizeNowForTests() - const someComputed = mobx.computed(() => { const currentTimestamp = utils.now(1000) @@ -79,6 +77,10 @@ describe("given desynchronization is enabled", () => { ) }) + afterEach(() => { + utils.clearTimers() + }) + it("given time passes, works", () => { jest.advanceTimersByTime(1000)