-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor TypeScript definition to CommonJS compatible export (#44)
- Loading branch information
1 parent
3217a33
commit fa2d83c
Showing
4 changed files
with
98 additions
and
68 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 |
---|---|---|
@@ -1,64 +1,86 @@ | ||
export interface ClearablePromise<T> extends Promise<T> { | ||
/** | ||
* Clears the delay and settles the promise. | ||
*/ | ||
clear(): void; | ||
} | ||
|
||
/** | ||
* Minimal subset of `AbortSignal` that delay will use if passed. | ||
* This avoids a dependency on dom.d.ts. | ||
* The dom.d.ts `AbortSignal` is compatible with this one. | ||
*/ | ||
interface AbortSignal { | ||
readonly aborted: boolean; | ||
addEventListener(type: 'abort', listener: () => void, options?: { once?: boolean }): void; | ||
removeEventListener(type: 'abort', listener: () => void): void; | ||
} | ||
declare namespace delay { | ||
interface ClearablePromise<T> extends Promise<T> { | ||
/** | ||
Clears the delay and settles the promise. | ||
*/ | ||
clear(): void; | ||
} | ||
|
||
export interface Options { | ||
/** | ||
* An optional AbortSignal to abort the delay. | ||
* If aborted, the Promise will be rejected with an AbortError. | ||
*/ | ||
signal?: AbortSignal | ||
Minimal subset of `AbortSignal` that delay will use if passed. | ||
This avoids a dependency on dom.d.ts. | ||
The dom.d.ts `AbortSignal` is compatible with this one. | ||
*/ | ||
interface AbortSignal { | ||
readonly aborted: boolean; | ||
addEventListener( | ||
type: 'abort', | ||
listener: () => void, | ||
options?: {once?: boolean} | ||
): void; | ||
removeEventListener(type: 'abort', listener: () => void): void; | ||
} | ||
|
||
interface Options { | ||
/** | ||
An optional AbortSignal to abort the delay. | ||
If aborted, the Promise will be rejected with an AbortError. | ||
*/ | ||
signal?: AbortSignal; | ||
} | ||
} | ||
|
||
type Delay = { | ||
/** | ||
* Create a promise which resolves after the specified `milliseconds`. | ||
* | ||
* @param milliseconds - Milliseconds to delay the promise. | ||
* @returns A promise which resolves after the specified `milliseconds`. | ||
*/ | ||
(milliseconds: number, options?: Options): ClearablePromise<void>; | ||
Create a promise which resolves after the specified `milliseconds`. | ||
@param milliseconds - Milliseconds to delay the promise. | ||
@returns A promise which resolves after the specified `milliseconds`. | ||
*/ | ||
(milliseconds: number, options?: delay.Options): delay.ClearablePromise<void>; | ||
|
||
/** | ||
* Create a promise which resolves after the specified `milliseconds`. | ||
* | ||
* @param milliseconds - Milliseconds to delay the promise. | ||
* @returns A promise which resolves after the specified `milliseconds`. | ||
*/ | ||
<T>(milliseconds: number, options?: Options & { | ||
/** Value to resolve in the returned promise. */ | ||
value: T | ||
}): ClearablePromise<T>; | ||
Create a promise which resolves after the specified `milliseconds`. | ||
@param milliseconds - Milliseconds to delay the promise. | ||
@returns A promise which resolves after the specified `milliseconds`. | ||
*/ | ||
<T>( | ||
milliseconds: number, | ||
options?: delay.Options & { | ||
/** | ||
Value to resolve in the returned promise. | ||
*/ | ||
value: T; | ||
} | ||
): delay.ClearablePromise<T>; | ||
|
||
/** | ||
* Create a promise which rejects after the specified `milliseconds`. | ||
* | ||
* @param milliseconds - Milliseconds to delay the promise. | ||
* @returns A promise which rejects after the specified `milliseconds`. | ||
*/ | ||
Create a promise which rejects after the specified `milliseconds`. | ||
@param milliseconds - Milliseconds to delay the promise. | ||
@returns A promise which rejects after the specified `milliseconds`. | ||
*/ | ||
// TODO: Allow providing value type after https://github.com/Microsoft/TypeScript/issues/5413 will be resolved. | ||
reject(milliseconds: number, options?: Options & { | ||
/** Value to reject in the returned promise. */ | ||
value?: any | ||
}): ClearablePromise<never>; | ||
} | ||
reject( | ||
milliseconds: number, | ||
options?: delay.Options & { | ||
/** | ||
Value to reject in the returned promise. | ||
*/ | ||
value?: unknown; | ||
} | ||
): delay.ClearablePromise<never>; | ||
}; | ||
|
||
declare const delay: Delay & { | ||
createWithTimers(timers: {clearTimeout: typeof clearTimeout, setTimeout: typeof setTimeout}): Delay | ||
createWithTimers(timers: { | ||
clearTimeout: typeof clearTimeout; | ||
setTimeout: typeof setTimeout; | ||
}): Delay; | ||
|
||
// TODO: Remove this for the next major release | ||
default: typeof delay; | ||
}; | ||
|
||
export default delay; | ||
export = delay; |
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,19 +1,24 @@ | ||
import {expectType} from 'tsd-check'; | ||
import delay from '.'; | ||
/// <reference lib="dom"/> | ||
import {expectType} from 'tsd'; | ||
import delay = require('.'); | ||
import {ClearablePromise} from '.'; | ||
|
||
expectType<void>(await delay(200)); | ||
expectType<ClearablePromise<void>>(delay(200)); | ||
|
||
expectType<string>(await delay(200, {value: '🦄'})); | ||
expectType<number>(await delay(200, {value: 0})); | ||
expectType<ClearablePromise<string>>(delay(200, {value: '🦄'})); | ||
expectType<ClearablePromise<number>>(delay(200, {value: 0})); | ||
expectType<ClearablePromise<void>>( | ||
delay(200, {signal: new AbortController().signal}) | ||
); | ||
|
||
expectType<never>(await delay.reject(200, {value: '🦄'})); | ||
expectType<never>(await delay.reject(200, {value: 0})); | ||
expectType<ClearablePromise<never>>(delay.reject(200, {value: '🦄'})); | ||
expectType<ClearablePromise<never>>(delay.reject(200, {value: 0})); | ||
|
||
const customDelay = delay.createWithTimers({clearTimeout, setTimeout}) | ||
expectType<void>(await customDelay(200)); | ||
const customDelay = delay.createWithTimers({clearTimeout, setTimeout}); | ||
expectType<ClearablePromise<void>>(customDelay(200)); | ||
|
||
expectType<string>(await customDelay(200, {value: '🦄'})); | ||
expectType<number>(await customDelay(200, {value: 0})); | ||
expectType<ClearablePromise<string>>(customDelay(200, {value: '🦄'})); | ||
expectType<ClearablePromise<number>>(customDelay(200, {value: 0})); | ||
|
||
expectType<never>(await customDelay.reject(200, {value: '🦄'})); | ||
expectType<never>(await customDelay.reject(200, {value: 0})); | ||
expectType<ClearablePromise<never>>(customDelay.reject(200, {value: '🦄'})); | ||
expectType<ClearablePromise<never>>(customDelay.reject(200, {value: 0})); |
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