-
Notifications
You must be signed in to change notification settings - Fork 51
feat: abort controller #183
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ interface IConfig extends IStaticContext { | |
storageProvider?: IStorageProvider; | ||
context?: IMutableContext; | ||
fetch?: any; | ||
createAbortController?: () => AbortController; | ||
bootstrap?: IToggle[]; | ||
bootstrapOverride?: boolean; | ||
headerName?: string; | ||
|
@@ -94,6 +95,18 @@ export const resolveFetch = () => { | |
return undefined; | ||
}; | ||
|
||
const resolveAbortController = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar pattern to how we handle fetch |
||
try { | ||
if (typeof window !== 'undefined' && 'AbortController' in window) { | ||
return () => new window.AbortController(); | ||
} else if ('fetch' in globalThis) { | ||
return () => new globalThis.AbortController(); | ||
} | ||
} catch (e) { | ||
console.error('Unleash failed to resolve "AbortController" factory', e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should basically never happen, right? This is if someone is doing something unusual like swapping out fetch? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct - someone would need to take deliberate effort to end up in this situation |
||
} | ||
}; | ||
|
||
export class UnleashClient extends TinyEmitter { | ||
private toggles: IToggle[] = []; | ||
private impressionDataAll: boolean; | ||
|
@@ -107,6 +120,8 @@ export class UnleashClient extends TinyEmitter { | |
private metrics: Metrics; | ||
private ready: Promise<void>; | ||
private fetch: any; | ||
private createAbortController?: () => AbortController; | ||
private abortController?: AbortController | null; | ||
private bootstrap?: IToggle[]; | ||
private bootstrapOverride: boolean; | ||
private headerName: string; | ||
|
@@ -128,6 +143,7 @@ export class UnleashClient extends TinyEmitter { | |
environment = 'default', | ||
context, | ||
fetch = resolveFetch(), | ||
createAbortController = resolveAbortController(), | ||
bootstrap, | ||
bootstrapOverride = true, | ||
headerName = 'Authorization', | ||
|
@@ -176,8 +192,14 @@ export class UnleashClient extends TinyEmitter { | |
'Unleash: You must either provide your own "fetch" implementation or run in an environment where "fetch" is available.' | ||
); | ||
} | ||
if (!createAbortController) { | ||
console.error( | ||
'Unleash: You must either provide your own "AbortController" implementation or run in an environment where "AbortController" is available.' | ||
); | ||
} | ||
|
||
this.fetch = fetch; | ||
this.createAbortController = createAbortController; | ||
this.bootstrap = | ||
bootstrap && bootstrap.length > 0 ? bootstrap : undefined; | ||
this.bootstrapOverride = bootstrapOverride; | ||
|
@@ -366,6 +388,15 @@ export class UnleashClient extends TinyEmitter { | |
|
||
private async fetchToggles() { | ||
if (this.fetch) { | ||
if (this.abortController) { | ||
this.abortController.abort(); | ||
} | ||
this.abortController = | ||
this.createAbortController && this.createAbortController(); | ||
const signal = this.abortController | ||
? this.abortController.signal | ||
: undefined; | ||
|
||
try { | ||
const isPOST = this.usePOSTrequests; | ||
|
||
|
@@ -382,6 +413,7 @@ export class UnleashClient extends TinyEmitter { | |
cache: 'no-cache', | ||
headers: this.getHeaders(), | ||
body, | ||
signal, | ||
}); | ||
if (response.ok && response.status !== 304) { | ||
this.etag = response.headers.get('ETag') || ''; | ||
|
@@ -404,6 +436,8 @@ export class UnleashClient extends TinyEmitter { | |
} catch (e) { | ||
console.error('Unleash: unable to fetch feature toggles', e); | ||
this.emit(EVENTS.ERROR, e); | ||
} finally { | ||
this.abortController = null; | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also wanted to test error emitter on abort but the mocking fetch library does not support AbortController