From 091e292c96027ab3460bfa1803ea943ca73219ec Mon Sep 17 00:00:00 2001 From: Tymoteusz Czech <2625371+Tymek@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:31:09 +0200 Subject: [PATCH] feat: allow for AbortController to be optional --- src/index.test.ts | 21 +++++++++++++++++++++ src/index.ts | 15 +++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index fcb8a00..7eb1ef4 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -642,6 +642,27 @@ test('Should not trigger error on abort', async () => { await client.updateContext({ userId: '789' }); }); +test('Should run without abort controller', async () => { + fetchMock.mockResponse(JSON.stringify(data)); + const abortSpy = jest.spyOn(AbortController.prototype, 'abort'); + + const config: IConfig = { + url: 'http://localhost/test', + clientKey: '12', + appName: 'web', + createAbortController: () => null, + }; + const client = new UnleashClient(config); + + await client.start(); + client.updateContext({ userId: '123' }); + client.updateContext({ userId: '456' }); + await client.updateContext({ userId: '789' }); + + expect(abortSpy).toBeCalledTimes(0); + abortSpy.mockRestore(); +}); + test.each([400, 401, 403, 404, 429, 500, 502, 503])( 'Should publish error when fetch receives a %d error', async (errorCode) => { diff --git a/src/index.ts b/src/index.ts index e326dcc..ae65470 100644 --- a/src/index.ts +++ b/src/index.ts @@ -46,7 +46,7 @@ interface IConfig extends IStaticContext { storageProvider?: IStorageProvider; context?: IMutableContext; fetch?: any; - createAbortController?: () => AbortController; + createAbortController?: () => AbortController | null; bootstrap?: IToggle[]; bootstrapOverride?: boolean; headerName?: string; @@ -100,7 +100,9 @@ export const resolveFetch = () => { try { if (typeof window !== 'undefined' && 'fetch' in window) { return fetch.bind(window); - } else if ('fetch' in globalThis) { + } + + if ('fetch' in globalThis) { return fetch.bind(globalThis); } } catch (e) { @@ -114,7 +116,9 @@ const resolveAbortController = () => { try { if (typeof window !== 'undefined' && 'AbortController' in window) { return () => new window.AbortController(); - } else if ('fetch' in globalThis) { + } + + if ('fetch' in globalThis) { return () => new globalThis.AbortController(); } } catch (e) { @@ -135,7 +139,7 @@ export class UnleashClient extends TinyEmitter { private metrics: Metrics; private ready: Promise; private fetch: any; - private createAbortController?: () => AbortController; + private createAbortController?: () => AbortController | null; private abortController?: AbortController | null; private bootstrap?: IToggle[]; private bootstrapOverride: boolean; @@ -431,8 +435,7 @@ export class UnleashClient extends TinyEmitter { if (this.abortController) { this.abortController.abort(); } - this.abortController = - this.createAbortController && this.createAbortController(); + this.abortController = this.createAbortController?.(); const signal = this.abortController ? this.abortController.signal : undefined;