6
6
import { EventEmitter } from 'events' ;
7
7
import queueMicrotask from 'queue-microtask' ;
8
8
9
+ let taskScheduler : TaskScheduler = queueMicrotask ;
10
+
11
+ /** Schedules the given ask for asynchronous execution. */
12
+ export function scheduleTask ( task : ( ) => void ) : void {
13
+ taskScheduler ( task ) ;
14
+ }
15
+
16
+ /** Returns the asynchronous task scheduler. */
17
+ export function getTaskScheduler ( ) : TaskScheduler {
18
+ return taskScheduler ;
19
+ }
20
+
21
+ /** Sets the asynchronous task scheduler. */
22
+ export function setTaskScheduler ( scheduler : TaskScheduler ) : void {
23
+ taskScheduler = scheduler ;
24
+ }
25
+
9
26
/**
10
27
ID of the INIT state.
11
28
An iterator is initializing if it is preparing main item generation.
@@ -90,7 +107,7 @@ export class AsyncIterator<T> extends EventEmitter {
90
107
if ( ! eventAsync )
91
108
this . emit ( 'end' ) ;
92
109
else
93
- queueMicrotask ( ( ) => this . emit ( 'end' ) ) ;
110
+ taskScheduler ( ( ) => this . emit ( 'end' ) ) ;
94
111
}
95
112
}
96
113
return valid ;
@@ -210,7 +227,7 @@ export class AsyncIterator<T> extends EventEmitter {
210
227
@protected
211
228
*/
212
229
protected _endAsync ( ) {
213
- queueMicrotask ( ( ) => this . _end ( ) ) ;
230
+ taskScheduler ( ( ) => this . _end ( ) ) ;
214
231
}
215
232
216
233
/**
@@ -236,7 +253,7 @@ export class AsyncIterator<T> extends EventEmitter {
236
253
this . _readable = readable ;
237
254
// If the iterator became readable, emit the `readable` event
238
255
if ( readable )
239
- queueMicrotask ( ( ) => this . emit ( 'readable' ) ) ;
256
+ taskScheduler ( ( ) => this . emit ( 'readable' ) ) ;
240
257
}
241
258
}
242
259
@@ -308,7 +325,7 @@ export class AsyncIterator<T> extends EventEmitter {
308
325
return properties && properties [ propertyName ] ;
309
326
// If the value has been set, send it through the callback
310
327
if ( properties && ( propertyName in properties ) ) {
311
- queueMicrotask ( ( ) => callback ( properties [ propertyName ] ) ) ;
328
+ taskScheduler ( ( ) => callback ( properties [ propertyName ] ) ) ;
312
329
}
313
330
// If the value was not set, store the callback for when the value will be set
314
331
else {
@@ -336,7 +353,7 @@ export class AsyncIterator<T> extends EventEmitter {
336
353
const callbacks = propertyCallbacks [ propertyName ] ;
337
354
if ( callbacks ) {
338
355
delete propertyCallbacks [ propertyName ] ;
339
- queueMicrotask ( ( ) => {
356
+ taskScheduler ( ( ) => {
340
357
for ( const callback of callbacks )
341
358
callback ( value ) ;
342
359
} ) ;
@@ -502,7 +519,7 @@ function waitForDataListener(this: AsyncIterator<any>, eventName: string) {
502
519
this . removeListener ( 'newListener' , waitForDataListener ) ;
503
520
addSingleListener ( this , 'readable' , emitData ) ;
504
521
if ( this . readable )
505
- queueMicrotask ( ( ) => emitData . call ( this ) ) ;
522
+ taskScheduler ( ( ) => emitData . call ( this ) ) ;
506
523
}
507
524
}
508
525
// Emits new items though `data` events as long as there are `data` listeners
@@ -710,7 +727,7 @@ export class BufferedIterator<T> extends AsyncIterator<T> {
710
727
constructor ( { maxBufferSize = 4 , autoStart = true } = { } ) {
711
728
super ( INIT ) ;
712
729
this . maxBufferSize = maxBufferSize ;
713
- queueMicrotask ( ( ) => this . _init ( autoStart ) ) ;
730
+ taskScheduler ( ( ) => this . _init ( autoStart ) ) ;
714
731
}
715
732
716
733
/**
@@ -886,7 +903,7 @@ export class BufferedIterator<T> extends AsyncIterator<T> {
886
903
// Acquire reading lock to avoid recursive reads
887
904
if ( ! this . _reading ) {
888
905
this . _reading = true ;
889
- queueMicrotask ( ( ) => {
906
+ taskScheduler ( ( ) => {
890
907
// Release reading lock so _fillBuffer` can take it
891
908
this . _reading = false ;
892
909
this . _fillBuffer ( ) ;
@@ -1072,7 +1089,7 @@ export class TransformIterator<S, D = S> extends BufferedIterator<D> {
1072
1089
const next = ( ) => {
1073
1090
// Continue transforming until at least `count` items have been pushed
1074
1091
if ( this . _pushedCount < count && ! this . closed )
1075
- queueMicrotask ( ( ) => this . _readAndTransform ( next , done ) ) ;
1092
+ taskScheduler ( ( ) => this . _readAndTransform ( next , done ) ) ;
1076
1093
else
1077
1094
done ( ) ;
1078
1095
} ;
@@ -1220,10 +1237,10 @@ export class SimpleTransformIterator<S, D = S> extends TransformIterator<S, D> {
1220
1237
/* Tries to read and transform items */
1221
1238
protected _read ( count : number , done : ( ) => void ) {
1222
1239
const next = ( ) => this . _readAndTransformSimple ( count , nextAsync , done ) ;
1240
+ this . _readAndTransformSimple ( count , nextAsync , done ) ;
1223
1241
function nextAsync ( ) {
1224
- queueMicrotask ( next ) ;
1242
+ taskScheduler ( next ) ;
1225
1243
}
1226
- this . _readAndTransformSimple ( count , nextAsync , done ) ;
1227
1244
}
1228
1245
1229
1246
/* Reads and transform items */
@@ -1858,3 +1875,5 @@ type SourceExpression<T> =
1858
1875
1859
1876
type InternalSource < T > =
1860
1877
AsyncIterator < T > & { _destination : AsyncIterator < any > } ;
1878
+
1879
+ type TaskScheduler = ( task : ( ) => void ) => void ;
0 commit comments