-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for AbortController. (#606)
- Loading branch information
Showing
6 changed files
with
117 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const SECRET = {}; | ||
|
||
// @ts-ignore | ||
export class _AbortSignal extends EventTarget { | ||
public _aborted: boolean; | ||
|
||
get aborted() { | ||
return this._aborted; | ||
} | ||
|
||
constructor(secret: any) { | ||
super(); | ||
if (secret !== SECRET) { | ||
throw new TypeError("Illegal constructor."); | ||
} | ||
this._aborted = false; | ||
} | ||
|
||
private _onabort: any; | ||
get onabort() { | ||
return this._aborted; | ||
} | ||
set onabort(callback: any) { | ||
const existing = this._onabort; | ||
if (existing) { | ||
this.removeEventListener("abort", existing); | ||
} | ||
this._onabort = callback; | ||
this.addEventListener("abort", callback); | ||
} | ||
|
||
} | ||
|
||
export class _AbortController { | ||
public _signal: _AbortSignal; | ||
constructor() { | ||
this._signal = new _AbortSignal(SECRET); | ||
} | ||
|
||
get signal() { | ||
return this._signal; | ||
} | ||
|
||
abort() { | ||
const signal = this.signal; | ||
if (!signal.aborted) { | ||
signal._aborted = true; | ||
signal.dispatchEvent(new Event("abort")); | ||
} | ||
} | ||
} | ||
|
||
|
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
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
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