-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db2226b
commit 5b46247
Showing
17 changed files
with
369 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
interface AbortSignalEventTargetAddOptions { | ||
once: boolean; | ||
} | ||
|
||
export interface AbortSignalEventTarget { | ||
addEventListener: ( | ||
name: 'abort', | ||
listener: () => void, | ||
options?: AbortSignalEventTargetAddOptions | ||
) => void; | ||
removeEventListener: (name: 'abort', listener: () => void) => void; | ||
aborted?: boolean; | ||
reason?: unknown; | ||
} | ||
|
||
export interface AbortSignalEventEmitter { | ||
off: (name: 'abort', listener: () => void) => void; | ||
once: (name: 'abort', listener: () => void) => void; | ||
} | ||
|
||
export type AbortSignalAny = AbortSignalEventTarget | AbortSignalEventEmitter; | ||
|
||
export class AbortError extends Error { | ||
constructor (reason?: AbortSignalEventTarget['reason']) { | ||
// TS does not recognizes the cause clause | ||
// @ts-expect-error | ||
super('The task has been aborted', { cause: reason }); | ||
} | ||
|
||
get name () { | ||
return 'AbortError'; | ||
} | ||
} | ||
|
||
export function onabort (abortSignal: AbortSignalAny, listener: () => void) { | ||
if ('addEventListener' in abortSignal) { | ||
abortSignal.addEventListener('abort', listener, { once: true }); | ||
} else { | ||
abortSignal.once('abort', listener); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.