1- import type { ContextTimed } from '../types' ;
1+ import type { ContextTimed , ContextTimedInput } from '../types' ;
22import { Timer } from '@matrixai/timer' ;
33import * as errors from '../errors' ;
44import * as utils from '../utils' ;
55
6- type ContextRemaining < C > = Omit < C , keyof ContextTimed > ;
6+ type ContextRemaining < C > = Omit < C , keyof ContextTimedInput > ;
77
88type ContextAndParameters <
99 C ,
1010 P extends Array < any > ,
1111> = 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 ] ;
1414
1515function setupTimedContext (
1616 delay : number ,
1717 errorTimeoutConstructor : new ( ) => Error ,
18- ctx : Partial < ContextTimed > ,
18+ ctx : Partial < ContextTimedInput > ,
1919) : ( ) => void {
2020 // There are 3 properties of timer and signal:
2121 //
@@ -36,11 +36,11 @@ function setupTimedContext(
3636 // In situation 4, there's a caveat for property A: it is assumed that the
3737 // caller has already setup the property A relationship, therefore this
3838 // 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 ) {
4040 const abortController = new AbortController ( ) ;
4141 const e = new errorTimeoutConstructor ( ) ;
4242 // Property A
43- const timer = new Timer ( ( ) => void abortController . abort ( e ) , delay ) ;
43+ const timer = new Timer ( ( ) => void abortController . abort ( e ) , ctx . timer ?? delay ) ;
4444 abortController . signal . addEventListener ( 'abort' , ( ) => {
4545 // Property B
4646 timer . cancel ( ) ;
@@ -51,11 +51,11 @@ function setupTimedContext(
5151 // Property C
5252 timer . cancel ( ) ;
5353 } ;
54- } else if ( ctx . timer === undefined && ctx . signal instanceof AbortSignal ) {
54+ } else if ( ( ctx . timer === undefined || typeof ctx . timer === 'number' ) && ctx . signal instanceof AbortSignal ) {
5555 const abortController = new AbortController ( ) ;
5656 const e = new errorTimeoutConstructor ( ) ;
5757 // Property A
58- const timer = new Timer ( ( ) => void abortController . abort ( e ) , delay ) ;
58+ const timer = new Timer ( ( ) => void abortController . abort ( e ) , ctx . timer ?? delay ) ;
5959 const signalUpstream = ctx . signal ;
6060 const signalHandler = ( ) => {
6161 // Property B
@@ -113,13 +113,13 @@ function setupTimedContext(
113113 * Timed HOF
114114 * This overloaded signature is external signature
115115 */
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 ,
118118 delay ?: number ,
119119 errorTimeoutConstructor ?: new ( ) => Error ,
120120) : ( ...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 ,
123123 delay : number = Infinity ,
124124 errorTimeoutConstructor : new ( ) => Error = errors . ErrorContextsTimedTimeOut ,
125125) : ( ...params : ContextAndParameters < C , P > ) => any {
@@ -133,7 +133,7 @@ function timed<C extends ContextTimed, P extends Array<any>>(
133133 ctx ,
134134 ) ;
135135 try {
136- return await f ( ctx as C , ...args ) ;
136+ return await f ( ctx as C_ , ...args ) ;
137137 } finally {
138138 teardownContext ( ) ;
139139 }
@@ -148,7 +148,7 @@ function timed<C extends ContextTimed, P extends Array<any>>(
148148 ctx ,
149149 ) ;
150150 try {
151- return yield * f ( ctx as C , ...args ) ;
151+ return yield * f ( ctx as C_ , ...args ) ;
152152 } finally {
153153 teardownContext ( ) ;
154154 }
@@ -163,7 +163,7 @@ function timed<C extends ContextTimed, P extends Array<any>>(
163163 ctx ,
164164 ) ;
165165 try {
166- return yield * f ( ctx as C , ...args ) ;
166+ return yield * f ( ctx as C_ , ...args ) ;
167167 } finally {
168168 teardownContext ( ) ;
169169 }
@@ -177,7 +177,7 @@ function timed<C extends ContextTimed, P extends Array<any>>(
177177 errorTimeoutConstructor ,
178178 ctx ,
179179 ) ;
180- const result = f ( ctx as C , ...args ) ;
180+ const result = f ( ctx as C_ , ...args ) ;
181181 if ( utils . isPromiseLike ( result ) ) {
182182 return result . then (
183183 ( r ) => {
0 commit comments