7
7
8
8
import searchTimer from './backburner/binary-search' ;
9
9
import DeferredActionQueues from './backburner/deferred-action-queues' ;
10
- import iteratorDrain from './backburner/iterator-drain' ;
10
+ import iteratorDrain , { Iteratable } from './backburner/iterator-drain' ;
11
11
12
12
import Queue , { QUEUE_STATE } from './backburner/queue' ;
13
13
@@ -29,7 +29,7 @@ function parseArgs() {
29
29
target = arguments [ 0 ] ;
30
30
method = arguments [ 1 ] ;
31
31
if ( typeof method === 'string' ) {
32
- method = target [ method ] as ( ) => any ;
32
+ method = target [ method ] ;
33
33
}
34
34
35
35
if ( length > 2 ) {
@@ -52,24 +52,27 @@ export default class Backburner {
52
52
53
53
public options : any ;
54
54
55
- private _onBegin : ( ) => void ;
56
- private _onEnd : ( ) => void ;
55
+ private _onBegin : ( currentInstance : DeferredActionQueues , previousInstance : DeferredActionQueues | null ) => void ;
56
+ private _onEnd : ( currentInstance : DeferredActionQueues , nextInstance : DeferredActionQueues | null ) => void ;
57
57
private queueNames : string [ ] ;
58
- private instanceStack : DeferredActionQueues [ ] ;
59
- private _debouncees : any [ ] ;
60
- private _throttlers : any [ ] ;
58
+ private instanceStack : DeferredActionQueues [ ] = [ ] ;
59
+ private _debouncees : any [ ] = [ ] ;
60
+ private _throttlers : any [ ] = [ ] ;
61
61
private _eventCallbacks : {
62
- end : ( ) => [ ] ;
63
- begin : ( ) => [ ] ;
62
+ end : Function [ ] ;
63
+ begin : Function [ ] ;
64
+ } = {
65
+ end : [ ] ,
66
+ begin : [ ]
64
67
} ;
65
68
66
69
private _timerTimeoutId : number | null = null ;
67
- private _timers : any [ ] ;
70
+ private _timers : any [ ] = [ ] ;
68
71
private _platform : {
69
- setTimeout ( fn : ( ) => void , ms : number ) : number ;
72
+ setTimeout ( fn : Function , ms : number ) : number ;
70
73
clearTimeout ( id : number ) : void ;
71
- next ( fn : ( ) => void ) : number ;
72
- clearNext ( fn ) : void ;
74
+ next ( fn : Function ) : number ;
75
+ clearNext ( id : any ) : void ;
73
76
now ( ) : number ;
74
77
} ;
75
78
@@ -85,20 +88,12 @@ export default class Backburner {
85
88
this . options . defaultQueue = queueNames [ 0 ] ;
86
89
}
87
90
88
- this . instanceStack = [ ] ;
89
- this . _timers = [ ] ;
90
- this . _debouncees = [ ] ;
91
- this . _throttlers = [ ] ;
92
- this . _eventCallbacks = {
93
- end : [ ] ,
94
- begin : [ ]
95
- } ;
96
-
97
91
this . _onBegin = this . options . onBegin || noop ;
98
92
this . _onEnd = this . options . onEnd || noop ;
99
93
100
94
let _platform = this . options . _platform || { } ;
101
95
let platform = Object . create ( null ) ;
96
+
102
97
platform . setTimeout = _platform . setTimeout || ( ( fn , ms ) => setTimeout ( fn , ms ) ) ;
103
98
platform . clearTimeout = _platform . clearTimeout || ( ( id ) => clearTimeout ( id ) ) ;
104
99
platform . next = _platform . next || ( ( fn ) => SET_TIMEOUT ( fn , 0 ) ) ;
@@ -209,9 +204,9 @@ export default class Backburner {
209
204
}
210
205
}
211
206
212
- public run ( method : ( ) => any ) ;
213
- public run ( target : ( ) => any | any | null , method ?: ( ) => any | string , ...args ) ;
214
- public run ( target : any | null | undefined , method ?: any , ...args : any [ ] ) ;
207
+ public run ( target : Function ) ;
208
+ public run ( target : Function | any | null , method ?: Function | string , ...args ) ;
209
+ public run ( target : any | null | undefined , method ?: Function , ...args : any [ ] ) ;
215
210
public run ( ) {
216
211
let [ target , method , args ] = parseArgs ( ...arguments ) ;
217
212
return this . _run ( target , method , args ) ;
@@ -231,6 +226,9 @@ export default class Backburner {
231
226
@param {any } args The method arguments
232
227
@return method result
233
228
*/
229
+ public join ( target : Function ) ;
230
+ public join ( target : Function | any | null , method ?: Function | string , ...args ) ;
231
+ public join ( target : any | null | undefined , method ?: Function , ...args : any [ ] ) ;
234
232
public join ( ) {
235
233
let [ target , method , args ] = parseArgs ( ...arguments ) ;
236
234
return this . _join ( target , method , args ) ;
@@ -239,17 +237,16 @@ export default class Backburner {
239
237
/**
240
238
* @deprecated please use schedule instead.
241
239
*/
242
- public defer ( queueName : string , ...args ) ;
243
- public defer ( ) {
244
- return this . schedule ( ...arguments ) ;
240
+ public defer ( queueName , targetOrMethod , ..._args ) {
241
+ return this . schedule ( queueName , targetOrMethod , ..._args ) ;
245
242
}
246
243
247
244
/**
248
245
* Schedule the passed function to run inside the specified queue.
249
246
*/
250
- public schedule ( queueName : string , method : ( ) => any ) ;
247
+ public schedule ( queueName : string , method : Function ) ;
251
248
public schedule < T , U extends keyof T > ( queueName : string , target : T , method : U , ...args ) ;
252
- public schedule ( queueName : string , target : any | null , method : any | ( ( ) => any ) , ...args ) ;
249
+ public schedule ( queueName : string , target : any , method : any | Function , ...args ) ;
253
250
public schedule ( queueName , ..._args ) {
254
251
let [ target , method , args ] = parseArgs ( ..._args ) ;
255
252
let stack = this . DEBUG ? new Error ( ) : undefined ;
@@ -264,25 +261,24 @@ export default class Backburner {
264
261
@param {Iterable } an iterable of functions to execute
265
262
@return method result
266
263
*/
267
- public scheduleIterable ( queueName : string , iterable : ( ) => any ) {
264
+ public scheduleIterable ( queueName : string , iterable : ( ) => Iteratable ) {
268
265
let stack = this . DEBUG ? new Error ( ) : undefined ;
269
266
return this . _ensureInstance ( ) . schedule ( queueName , null , iteratorDrain , [ iterable ] , false , stack ) ;
270
267
}
271
268
272
269
/**
273
270
* @deprecated please use scheduleOnce instead.
274
271
*/
275
- public deferOnce ( queueName : string , ...args ) ;
276
- public deferOnce ( ) {
277
- return this . scheduleOnce ( ...arguments ) ;
272
+ public deferOnce ( queueName , targetOrMethod , ...args ) {
273
+ return this . scheduleOnce ( queueName , targetOrMethod , ...args ) ;
278
274
}
279
275
280
276
/**
281
277
* Schedule the passed function to run once inside the specified queue.
282
278
*/
283
- public scheduleOnce ( queueName : string , method : ( ) => any ) ;
279
+ public scheduleOnce ( queueName : string , method : Function ) ;
284
280
public scheduleOnce < T , U extends keyof T > ( queueName : string , target : T , method : U , ...args ) ;
285
- public scheduleOnce ( queueName : string , target : any | null , method : any | ( ( ) => any ) , ...args ) ;
281
+ public scheduleOnce ( queueName : string , target : any | null , method : any | Function , ...args ) ;
286
282
public scheduleOnce ( queueName , ..._args ) {
287
283
let [ target , method , args ] = parseArgs ( ..._args ) ;
288
284
let stack = this . DEBUG ? new Error ( ) : undefined ;
@@ -321,7 +317,7 @@ export default class Backburner {
321
317
method = args . shift ( ) ;
322
318
} else if ( methodOrTarget !== null && type === 'string' && methodOrWait in methodOrTarget ) {
323
319
target = args . shift ( ) ;
324
- method = target [ args . shift ( ) ] as ( ) => any ;
320
+ method = target [ args . shift ( ) ] ;
325
321
} else if ( isCoercableNumber ( methodOrWait ) ) {
326
322
method = args . shift ( ) ;
327
323
wait = parseInt ( args . shift ( ) , 10 ) ;
@@ -370,23 +366,23 @@ export default class Backburner {
370
366
return this . _setTimeout ( fn , executeAt ) ;
371
367
}
372
368
373
- public throttle < T > ( target : T , methodName : keyof T , wait : number , immediate ?: boolean ) : Timer ;
374
- public throttle < T > ( target : T , methodName : keyof T , arg1 : any , wait : number , immediate ?: boolean ) : Timer ;
375
- public throttle < T > ( target : T , methodName : keyof T , arg1 : any , arg2 : any , wait : number , immediate ?: boolean ) : Timer ;
376
- public throttle < T > ( target : T , methodName : keyof T , arg1 : any , arg2 : any , arg3 : any , wait : number , immediate ?: boolean ) : Timer ;
369
+ public throttle < T > ( target : T , methodName : keyof T , wait ? : number | string , immediate ?: boolean ) : Timer ;
370
+ public throttle < T > ( target : T , methodName : keyof T , arg1 : any , wait ? : number | string , immediate ?: boolean ) : Timer ;
371
+ public throttle < T > ( target : T , methodName : keyof T , arg1 : any , arg2 : any , wait ? : number | string , immediate ?: boolean ) : Timer ;
372
+ public throttle < T > ( target : T , methodName : keyof T , arg1 : any , arg2 : any , arg3 : any , wait ? : number | string , immediate ?: boolean ) : Timer ;
377
373
378
374
// with target, with immediate
379
- public throttle ( thisArg : any , method : ( ) => void , wait : number , immediate ?: boolean ) : Timer ;
380
- public throttle < A > ( thisArg : any , method : ( arg1 : A ) => void , arg1 : A , wait : number , immediate ?: boolean ) : Timer ;
381
- public throttle < A , B > ( thisArg : any , method : ( arg1 : A , arg2 : B ) => void , arg1 : A , arg2 : B , wait : number , immediate ?: boolean ) : Timer ;
382
- public throttle < A , B , C > ( thisArg : any , method : ( arg1 : A , arg2 : B , arg3 : C ) => void , arg1 : A , arg2 : B , arg3 : C , wait : number , immediate ?: boolean ) : Timer ;
375
+ public throttle ( thisArg : any | null , method : ( ) => void , wait ? : number | string , immediate ?: boolean ) : Timer ;
376
+ public throttle < A > ( thisArg : any | null , method : ( arg1 : A ) => void , arg1 : A , wait ? : number | string , immediate ?: boolean ) : Timer ;
377
+ public throttle < A , B > ( thisArg : any | null , method : ( arg1 : A , arg2 : B ) => void , arg1 : A , arg2 : B , wait ? : number | string , immediate ?: boolean ) : Timer ;
378
+ public throttle < A , B , C > ( thisArg : any | null , method : ( arg1 : A , arg2 : B , arg3 : C ) => void , arg1 : A , arg2 : B , arg3 : C , wait ? : number | string , immediate ?: boolean ) : Timer ;
383
379
384
380
// without target, with immediate
385
- public throttle ( method : ( ) => void , wait : number , immediate ?: boolean ) : Timer ;
386
- public throttle < A > ( method : ( arg1 : A ) => void , arg1 : A , wait : number , immediate ?: boolean ) : Timer ;
387
- public throttle < A , B > ( method : ( arg1 : A , arg2 : B ) => void , arg1 : A , arg2 : B , wait : number , immediate ?: boolean ) : Timer ;
388
- public throttle < A , B , C > ( method : ( arg1 : A , arg2 : B , arg3 : C ) => void , arg1 : A , arg2 : B , arg3 : C , wait : number , immediate ?: boolean ) : Timer ;
389
- public throttle ( targetOrThisArgOrMethod : object | ( ( ) => any ) , ...args ) : Timer {
381
+ public throttle ( method : ( ) => void , wait ? : number | string , immediate ?: boolean ) : Timer ;
382
+ public throttle < A > ( method : ( arg1 : A ) => void , arg1 : A , wait ? : number | string , immediate ?: boolean ) : Timer ;
383
+ public throttle < A , B > ( method : ( arg1 : A , arg2 : B ) => void , arg1 : A , arg2 : B , wait ? : number | string , immediate ?: boolean ) : Timer ;
384
+ public throttle < A , B , C > ( method : ( arg1 : A , arg2 : B , arg3 : C ) => void , arg1 : A , arg2 : B , arg3 : C , wait ? : number | string , immediate ?: boolean ) : Timer ;
385
+ public throttle ( targetOrThisArgOrMethod : object | Function , ...args ) : Timer {
390
386
let target ;
391
387
let method ;
392
388
let immediate ;
@@ -404,7 +400,7 @@ export default class Backburner {
404
400
immediate = args . pop ( ) ;
405
401
let type = typeof method ;
406
402
if ( type === 'string' ) {
407
- method = target [ method ] as ( ) => any ;
403
+ method = target [ method ] ;
408
404
} else if ( type !== 'function' ) {
409
405
args . unshift ( method ) ;
410
406
method = target ;
@@ -446,23 +442,23 @@ export default class Backburner {
446
442
}
447
443
448
444
// with target, with method name, with optional immediate
449
- public debounce < T > ( target : T , methodName : keyof T , wait : number , immediate ?: boolean ) : Timer ;
450
- public debounce < T > ( target : T , methodName : keyof T , arg1 : any , wait : number , immediate ?: boolean ) : Timer ;
451
- public debounce < T > ( target : T , methodName : keyof T , arg1 : any , arg2 : any , wait : number , immediate ?: boolean ) : Timer ;
452
- public debounce < T > ( target : T , methodName : keyof T , arg1 : any , arg2 : any , arg3 : any , wait : number , immediate ?: boolean ) : Timer ;
445
+ public debounce < T > ( target : T , methodName : keyof T , wait : number | string , immediate ?: boolean ) : Timer ;
446
+ public debounce < T > ( target : T , methodName : keyof T , arg1 : any , wait : number | string , immediate ?: boolean ) : Timer ;
447
+ public debounce < T > ( target : T , methodName : keyof T , arg1 : any , arg2 : any , wait : number | string , immediate ?: boolean ) : Timer ;
448
+ public debounce < T > ( target : T , methodName : keyof T , arg1 : any , arg2 : any , arg3 : any , wait : number | string , immediate ?: boolean ) : Timer ;
453
449
454
450
// with target, with optional immediate
455
- public debounce ( thisArg : any , method : ( ) => void , wait : number , immediate ?: boolean ) : Timer ;
456
- public debounce < A > ( thisArg : any , method : ( arg1 : A ) => void , arg1 : A , wait : number , immediate ?: boolean ) : Timer ;
457
- public debounce < A , B > ( thisArg : any , method : ( arg1 : A , arg2 : B ) => void , arg1 : A , arg2 : B , wait : number , immediate ?: boolean ) : Timer ;
458
- public debounce < A , B , C > ( thisArg : any , method : ( arg1 : A , arg2 : B , arg3 : C ) => void , arg1 : A , arg2 : B , arg3 : C , wait : number , immediate ?: boolean ) : Timer ;
451
+ public debounce ( thisArg : any | null , method : ( ) => void , wait : number | string , immediate ?: boolean ) : Timer ;
452
+ public debounce < A > ( thisArg : any | null , method : ( arg1 : A ) => void , arg1 : A , wait : number | string , immediate ?: boolean ) : Timer ;
453
+ public debounce < A , B > ( thisArg : any | null , method : ( arg1 : A , arg2 : B ) => void , arg1 : A , arg2 : B , wait : number | string , immediate ?: boolean ) : Timer ;
454
+ public debounce < A , B , C > ( thisArg : any | null , method : ( arg1 : A , arg2 : B , arg3 : C ) => void , arg1 : A , arg2 : B , arg3 : C , wait : number | string , immediate ?: boolean ) : Timer ;
459
455
460
456
// without target, with optional immediate
461
- public debounce ( method : ( ) => void , wait : number , immediate ?: boolean ) : Timer ;
462
- public debounce < A > ( method : ( arg1 : A ) => void , arg1 : A , wait : number , immediate ?: boolean ) : Timer ;
463
- public debounce < A , B > ( method : ( arg1 : A , arg2 : B ) => void , arg1 : A , arg2 : B , wait : number , immediate ?: boolean ) : Timer ;
464
- public debounce < A , B , C > ( method : ( arg1 : A , arg2 : B , arg3 : C ) => void , arg1 : A , arg2 : B , arg3 : C , wait : number , immediate ?: boolean ) : Timer ;
465
- public debounce ( targetOrThisArgOrMethod : object | ( ( ) => any ) , ...args ) : Timer {
457
+ public debounce ( method : ( ) => void , wait : number | string , immediate ?: boolean ) : Timer ;
458
+ public debounce < A > ( method : ( arg1 : A ) => void , arg1 : A , wait : number | string , immediate ?: boolean ) : Timer ;
459
+ public debounce < A , B > ( method : ( arg1 : A , arg2 : B ) => void , arg1 : A , arg2 : B , wait : number | string , immediate ?: boolean ) : Timer ;
460
+ public debounce < A , B , C > ( method : ( arg1 : A , arg2 : B , arg3 : C ) => void , arg1 : A , arg2 : B , arg3 : C , wait : number | string , immediate ?: boolean ) : Timer ;
461
+ public debounce ( targetOrThisArgOrMethod : object | Function , ...args ) : Timer {
466
462
let target ;
467
463
let method ;
468
464
let immediate ;
@@ -481,7 +477,7 @@ export default class Backburner {
481
477
482
478
let type = typeof method ;
483
479
if ( type === 'string' ) {
484
- method = target [ method ] as ( ) => any ;
480
+ method = target [ method ] ;
485
481
} else if ( type !== 'function' ) {
486
482
args . unshift ( method ) ;
487
483
method = target ;
@@ -542,7 +538,10 @@ export default class Backburner {
542
538
}
543
539
544
540
public hasTimers ( ) {
545
- return this . _timers . length > 0 || this . _debouncees . length > 0 || this . _throttlers . length > 0 || this . _autorun !== null ;
541
+ return this . _timers . length > 0 ||
542
+ this . _debouncees . length > 0 ||
543
+ this . _throttlers . length > 0 ||
544
+ this . _autorun !== null ;
546
545
}
547
546
548
547
public cancel ( timer ?) {
0 commit comments