-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate
TransitionAbortedError
to builder + interface.
* Avoid trying to manually extend from a native error, instead use a normal `Error` and add our name/code to it. * introduce structurally Abortable concept
- Loading branch information
1 parent
867f632
commit 38f4ca2
Showing
7 changed files
with
75 additions
and
63 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
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 |
---|---|---|
@@ -1,29 +1,42 @@ | ||
export interface TransitionAbortedErrorConstructor { | ||
new (message?: string): ITransitionAbortedError; | ||
readonly prototype: ITransitionAbortedError; | ||
export interface TransitionAbortedError extends Error { | ||
name: 'TransitionAborted'; | ||
code: 'TRANSITION_ABORTED'; | ||
} | ||
|
||
export interface ITransitionAbortedError extends Error { | ||
constructor: TransitionAbortedErrorConstructor; | ||
export function isTransitionAborted(maybeError: unknown): maybeError is TransitionAbortedError { | ||
return ( | ||
typeof maybeError === 'object' && | ||
maybeError !== null && | ||
(maybeError as TransitionAbortedError).code === 'TRANSITION_ABORTED' | ||
); | ||
} | ||
|
||
const TransitionAbortedError: TransitionAbortedErrorConstructor = (function () { | ||
TransitionAbortedError.prototype = Object.create(Error.prototype); | ||
TransitionAbortedError.prototype.constructor = TransitionAbortedError; | ||
|
||
function TransitionAbortedError(this: ITransitionAbortedError, message?: string) { | ||
let error = Error.call(this, message); | ||
this.name = 'TransitionAborted'; | ||
this.message = message || 'TransitionAborted'; | ||
interface Abortable<T extends boolean> { | ||
isAborted: T; | ||
[key: string]: unknown; | ||
} | ||
|
||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, TransitionAbortedError); | ||
} else { | ||
this.stack = error.stack; | ||
} | ||
} | ||
function isAbortable<T extends boolean>(maybeAbortable: unknown): maybeAbortable is Abortable<T> { | ||
return ( | ||
typeof maybeAbortable === 'object' && | ||
maybeAbortable !== null && | ||
typeof (maybeAbortable as Abortable<T>).isAborted === 'boolean' | ||
); | ||
} | ||
|
||
return TransitionAbortedError as any; | ||
})(); | ||
export function buildTransitionAborted() { | ||
let error = new Error('TransitionAborted') as TransitionAbortedError; | ||
error.name = 'TransitionAborted'; | ||
error.code = 'TRANSITION_ABORTED'; | ||
return error; | ||
} | ||
|
||
export default TransitionAbortedError; | ||
export function throwIfAborted<T extends boolean>( | ||
maybe: Abortable<T> | ||
): T extends true ? never : void; | ||
export function throwIfAborted(maybe: unknown): void; | ||
export function throwIfAborted(maybe: unknown | Abortable<boolean>): never | void { | ||
if (isAbortable(maybe) && maybe.isAborted) { | ||
throw buildTransitionAborted(); | ||
} | ||
} |
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