From c1e75ee087f0b91687ce32610959617e8a3a2d27 Mon Sep 17 00:00:00 2001 From: Jeffrey Chupp Date: Mon, 10 Jul 2023 15:21:17 -0400 Subject: [PATCH 1/2] Reset polling on `init` This is most relevant to react tests since there's a single global `prefab` object, but if you `init` after starting polling, the previous polling shouldn't continue. --- src/prefab.test.ts | 41 ++++++++++++++++++++++++++++++++++++++++- src/prefab.ts | 20 +++++++++++--------- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/prefab.test.ts b/src/prefab.test.ts index e0c2408..e23c3df 100644 --- a/src/prefab.test.ts +++ b/src/prefab.test.ts @@ -12,6 +12,11 @@ beforeEach(() => { prefab.configs = {}; }); +afterEach(() => { + prefab.stopPolling(); + prefab.pollCount = 0; +}); + describe('init', () => { it('works when the request is successful', async () => { const data = {values: {turbo: {double: 2.5}}}; @@ -91,7 +96,6 @@ describe('poll', () => { await prefab.poll({frequencyInMs}); expect(prefab.loader.context).toStrictEqual(prefab.context); - await wait(1); if (prefab.pollStatus.status !== 'running') { throw new Error('Expected pollStatus to be running'); @@ -117,6 +121,41 @@ describe('poll', () => { await wait(frequencyInMs * 2); expect(prefab.pollCount).toEqual(2); }); + + it('is reset on init', async () => { + jest.spyOn(globalThis, 'clearTimeout'); + + const data = {values: {}}; + const frequencyInMs = 25; + fetchMock.mockResponse(JSON.stringify(data)); + + const config: InitParams = { + apiKey: '1234', + context: new Context({user: {device: 'desktop'}}), + }; + + await prefab.init(config); + + if (!prefab.loader) { + throw new Error('Expected loader to be set'); + } + + await prefab.poll({frequencyInMs}); + expect(prefab.loader.context).toStrictEqual(prefab.context); + + if (prefab.pollStatus.status !== 'running') { + throw new Error('Expected pollStatus to be running'); + } + expect(prefab.pollCount).toEqual(0); + expect(prefab.loader.context).toStrictEqual(prefab.context); + + const timeoutId = prefab.pollTimeoutId; + + await prefab.init(config); + expect(prefab.pollStatus).toEqual({status: 'stopped'}); + expect(clearTimeout).toHaveBeenCalledWith(timeoutId); + expect(prefab.pollTimeoutId).toBeUndefined(); + }); }); describe('setConfig', () => { diff --git a/src/prefab.ts b/src/prefab.ts index 29efceb..c75a963 100644 --- a/src/prefab.ts +++ b/src/prefab.ts @@ -17,11 +17,7 @@ type PollStatus = | {status: 'not-started'} | {status: 'pending'} | {status: 'stopped'} - | { - status: 'running'; - frequencyInMs: number; - timeoutId: ReturnType; - }; + | {status: 'running'; frequencyInMs: number}; export const prefab = { configs: {} as {[key: string]: Config}, @@ -38,6 +34,8 @@ export const prefab = { pollCount: 0, + pollTimeoutId: undefined as ReturnType | undefined, + async init({ apiKey, context: providedContext, @@ -51,6 +49,10 @@ export const prefab = { throw new Error('Context must be provided'); } + if (this.pollTimeoutId) { + this.stopPolling(); + } + this.context = context; this.loader = new Loader({ @@ -82,8 +84,9 @@ export const prefab = { }, stopPolling() { - if (this.pollStatus.status === 'running') { - clearInterval(this.pollStatus.timeoutId); + if (this.pollTimeoutId) { + clearTimeout(this.pollTimeoutId); + this.pollTimeoutId = undefined; } this.pollStatus = {status: 'stopped'}; @@ -128,7 +131,7 @@ async function load() { } async function doPolling({frequencyInMs}: {frequencyInMs: number}) { - const pollTimeoutId = setTimeout(() => { + prefab.pollTimeoutId = setTimeout(() => { load().finally(() => { if (prefab.pollStatus.status === 'running') { doPolling({frequencyInMs}); @@ -139,6 +142,5 @@ async function doPolling({frequencyInMs}: {frequencyInMs: number}) { prefab.pollStatus = { status: 'running', frequencyInMs, - timeoutId: pollTimeoutId, }; } From 1da020b230225358f2cab073fa6b3fcbc0a6a983 Mon Sep 17 00:00:00 2001 From: Jeffrey Chupp Date: Tue, 11 Jul 2023 08:24:35 -0400 Subject: [PATCH 2/2] Fix duplicate prettier --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 283feac..008ff98 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,6 @@ "jest": "^28.1.3", "jest-fetch-mock": "^3.0.3", "lint-staged": "^13.2.3", - "prettier": "^2.8.8", "prettier": "^3.0.0", "ts-node": "^10.9.1", "typescript": "^4.7.4"