@@ -7,10 +7,8 @@ import MicroTaskQueue from './MicroTaskQueue.js';
77export class ListQueue extends MicroTaskQueue {
88 constructor ( paused ) {
99 super ( paused ) ;
10- // AI-NOTE: Using list-toolkit List for O(1) push/pop operations
1110 /** @type {List<MicroTask> } */
1211 this . list = new List ( ) ;
13- // AI-NOTE: stopQueue holds the stop function returned by startQueue(), or null
1412 this . stopQueue = null ;
1513 }
1614
@@ -21,7 +19,6 @@ export class ListQueue extends MicroTaskQueue {
2119 pause ( ) {
2220 if ( ! this . paused ) {
2321 super . pause ( ) ;
24- // AI-NOTE: Pattern: call stop function, then null it
2522 if ( this . stopQueue ) this . stopQueue = ( this . stopQueue ( ) , null ) ;
2623 }
2724 return this ;
@@ -30,7 +27,6 @@ export class ListQueue extends MicroTaskQueue {
3027 resume ( ) {
3128 if ( this . paused ) {
3229 super . resume ( ) ;
33- // AI-NOTE: Auto-start processing if tasks exist and not already running
3430 if ( ! this . list . isEmpty ) {
3531 this . stopQueue = this . startQueue ( ) ;
3632 }
@@ -41,15 +37,13 @@ export class ListQueue extends MicroTaskQueue {
4137 enqueue ( fn ) {
4238 const task = super . enqueue ( fn ) ;
4339 this . list . pushBack ( task ) ;
44- // AI-NOTE: Auto-start queue on first task if not paused and not running
4540 if ( ! this . paused && ! this . stopQueue ) this . stopQueue = this . startQueue ( ) ;
4641 return task ;
4742 }
4843
4944 dequeue ( task ) {
5045 task . cancel ( ) ;
5146 this . list . removeNode ( task ) ;
52- // AI-NOTE: Auto-stop queue when empty (unless paused)
5347 if ( ! this . paused && this . list . isEmpty && this . stopQueue )
5448 this . stopQueue = ( this . stopQueue ( ) , null ) ;
5549 return this ;
@@ -69,6 +63,27 @@ export class ListQueue extends MicroTaskQueue {
6963 startQueue ( ) {
7064 return null ;
7165 }
66+
67+ // Drains pending tasks. If batchMs is a finite number, runs tasks until that
68+ // many milliseconds have elapsed; otherwise swaps in a fresh list and drains
69+ // the captured one entirely (so tasks enqueued during draining run on the
70+ // next tick rather than this one).
71+ _drainBatch ( batchMs , taskContext ) {
72+ if ( ! isNaN ( batchMs ) ) {
73+ const start = Date . now ( ) ;
74+ while ( Date . now ( ) - start < batchMs && ! this . list . isEmpty ) {
75+ const task = this . list . popFront ( ) ;
76+ task . fn ( { ...taskContext , task, queue : this } ) ;
77+ }
78+ } else {
79+ const list = this . list ;
80+ this . list = new List ( ) ;
81+ while ( ! list . isEmpty ) {
82+ const task = list . popFront ( ) ;
83+ task . fn ( { ...taskContext , task, queue : this } ) ;
84+ }
85+ }
86+ }
7287}
7388
7489export default ListQueue ;
0 commit comments