1
- import type { ContextTimed } from '../types' ;
1
+ import type { ContextTimed , ContextTimedInput } from '../types' ;
2
2
import { Timer } from '@matrixai/timer' ;
3
3
import * as errors from '../errors' ;
4
4
import * as utils from '../utils' ;
5
5
6
- type ContextRemaining < C > = Omit < C , keyof ContextTimed > ;
6
+ type ContextRemaining < C > = Omit < C , keyof ContextTimedInput > ;
7
7
8
8
type ContextAndParameters <
9
9
C ,
10
10
P extends Array < any > ,
11
11
> = keyof ContextRemaining < C > extends never
12
- ? [ Partial < ContextTimed > ?, ...P ]
13
- : [ Partial < ContextTimed > & ContextRemaining < C > , ...P ] ;
12
+ ? [ Partial < ContextTimedInput > ?, ...P ]
13
+ : [ Partial < ContextTimedInput > & ContextRemaining < C > , ...P ] ;
14
14
15
15
function setupTimedContext (
16
16
delay : number ,
17
17
errorTimeoutConstructor : new ( ) => Error ,
18
- ctx : Partial < ContextTimed > ,
18
+ ctx : Partial < ContextTimedInput > ,
19
19
) : ( ) => void {
20
20
// There are 3 properties of timer and signal:
21
21
//
@@ -36,11 +36,11 @@ function setupTimedContext(
36
36
// In situation 4, there's a caveat for property A: it is assumed that the
37
37
// caller has already setup the property A relationship, therefore this
38
38
// wrapper will not re-setup this property A relationship.
39
- if ( ctx . timer === undefined && ctx . signal === undefined ) {
39
+ if ( ( ctx . timer === undefined || typeof ctx . timer === 'number' ) && ctx . signal === undefined ) {
40
40
const abortController = new AbortController ( ) ;
41
41
const e = new errorTimeoutConstructor ( ) ;
42
42
// Property A
43
- const timer = new Timer ( ( ) => void abortController . abort ( e ) , delay ) ;
43
+ const timer = new Timer ( ( ) => void abortController . abort ( e ) , ctx . timer ?? delay ) ;
44
44
abortController . signal . addEventListener ( 'abort' , ( ) => {
45
45
// Property B
46
46
timer . cancel ( ) ;
@@ -51,11 +51,11 @@ function setupTimedContext(
51
51
// Property C
52
52
timer . cancel ( ) ;
53
53
} ;
54
- } else if ( ctx . timer === undefined && ctx . signal instanceof AbortSignal ) {
54
+ } else if ( ( ctx . timer === undefined || typeof ctx . timer === 'number' ) && ctx . signal instanceof AbortSignal ) {
55
55
const abortController = new AbortController ( ) ;
56
56
const e = new errorTimeoutConstructor ( ) ;
57
57
// Property A
58
- const timer = new Timer ( ( ) => void abortController . abort ( e ) , delay ) ;
58
+ const timer = new Timer ( ( ) => void abortController . abort ( e ) , ctx . timer ?? delay ) ;
59
59
const signalUpstream = ctx . signal ;
60
60
const signalHandler = ( ) => {
61
61
// Property B
@@ -113,13 +113,13 @@ function setupTimedContext(
113
113
* Timed HOF
114
114
* This overloaded signature is external signature
115
115
*/
116
- function timed < C extends ContextTimed , P extends Array < any > , R > (
117
- f : ( ctx : C , ...params : P ) => R ,
116
+ function timed < C extends ContextTimedInput , C_ extends ContextTimed , P extends Array < any > , R > (
117
+ f : ( ctx : C_ , ...params : P ) => R ,
118
118
delay ?: number ,
119
119
errorTimeoutConstructor ?: new ( ) => Error ,
120
120
) : ( ...params : ContextAndParameters < C , P > ) => R ;
121
- function timed < C extends ContextTimed , P extends Array < any > > (
122
- f : ( ctx : C , ...params : P ) => any ,
121
+ function timed < C extends ContextTimedInput , C_ extends ContextTimed , P extends Array < any > > (
122
+ f : ( ctx : C_ , ...params : P ) => any ,
123
123
delay : number = Infinity ,
124
124
errorTimeoutConstructor : new ( ) => Error = errors . ErrorContextsTimedTimeOut ,
125
125
) : ( ...params : ContextAndParameters < C , P > ) => any {
@@ -133,7 +133,7 @@ function timed<C extends ContextTimed, P extends Array<any>>(
133
133
ctx ,
134
134
) ;
135
135
try {
136
- return await f ( ctx as C , ...args ) ;
136
+ return await f ( ctx as C_ , ...args ) ;
137
137
} finally {
138
138
teardownContext ( ) ;
139
139
}
@@ -148,7 +148,7 @@ function timed<C extends ContextTimed, P extends Array<any>>(
148
148
ctx ,
149
149
) ;
150
150
try {
151
- return yield * f ( ctx as C , ...args ) ;
151
+ return yield * f ( ctx as C_ , ...args ) ;
152
152
} finally {
153
153
teardownContext ( ) ;
154
154
}
@@ -163,7 +163,7 @@ function timed<C extends ContextTimed, P extends Array<any>>(
163
163
ctx ,
164
164
) ;
165
165
try {
166
- return yield * f ( ctx as C , ...args ) ;
166
+ return yield * f ( ctx as C_ , ...args ) ;
167
167
} finally {
168
168
teardownContext ( ) ;
169
169
}
@@ -177,7 +177,7 @@ function timed<C extends ContextTimed, P extends Array<any>>(
177
177
errorTimeoutConstructor ,
178
178
ctx ,
179
179
) ;
180
- const result = f ( ctx as C , ...args ) ;
180
+ const result = f ( ctx as C_ , ...args ) ;
181
181
if ( utils . isPromiseLike ( result ) ) {
182
182
return result . then (
183
183
( r ) => {
0 commit comments