Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow for AbortController to be optional #211

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading