Skip to content

Commit

Permalink
feat: allow for AbortController to be optional (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tymek authored Jun 12, 2024
1 parent f9a7c85 commit 7d972e3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
21 changes: 21 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -135,7 +139,7 @@ export class UnleashClient extends TinyEmitter {
private metrics: Metrics;
private ready: Promise<void>;
private fetch: any;
private createAbortController?: () => AbortController;
private createAbortController?: () => AbortController | null;
private abortController?: AbortController | null;
private bootstrap?: IToggle[];
private bootstrapOverride: boolean;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7d972e3

Please sign in to comment.