1- import { GecutLogger } from '@gecut/logger' ;
2-
31import { uid } from './uid' ;
42import { untilIdle , untilMS , untilNextFrame } from './wait/wait' ;
53
@@ -11,21 +9,14 @@ export class GecutQueue {
119 * Creates a new GecutQueue instance
1210 * @param {string } name - The name of the queue
1311 * @param {('animationFrame' | 'idleCallback' | number) } delayPeriod - The delay period for the queue
14- * @param {GecutLogger } [logger] - The logger instance (optional)
1512 */
16- constructor ( name : string , delayPeriod : 'animationFrame' | 'idleCallback' | number , logger ?: GecutLogger ) {
13+ constructor ( name : string , delayPeriod : 'animationFrame' | 'idleCallback' | number ) {
1714 /**
1815 * The name of the queue
1916 * @type {string }
2017 */
2118 this . name = name ;
2219
23- /**
24- * The logger instance
25- * @type {GecutLogger }
26- */
27- this . logger = logger ?? new GecutLogger ( `queue/${ name } ` ) ;
28-
2920 /**
3021 * The delay period function
3122 * @type {() => Promise<unknown> }
@@ -34,7 +25,6 @@ export class GecutQueue {
3425 }
3526
3627 name : string ;
37- private logger : GecutLogger ;
3828 private delayPeriod : ( ) => Promise < unknown > ;
3929
4030 /**
@@ -49,23 +39,28 @@ export class GecutQueue {
4939 */
5040 private values = new Map < string , unknown > ( ) ;
5141
42+ private runningId ?: string ;
43+
5244 /**
5345 * Pushes a new promise to the queue
54- * @param {Promise<unknown> } func - The promise to push
46+ * @param {Promise<unknown> } promise - The promise to push
5547 * @param {string } [id] - The optional id for the promise
5648 * @returns {{ id: string } } - The id of the pushed promise
5749 */
58- push ( func : Promise < unknown > , id ?: string ) : { id : string } {
50+ push ( promise : Promise < unknown > , id ?: string ) : { id : string } {
5951 id ??= uid ( ) ;
6052
6153 this . queue . set (
6254 id ,
6355 this . waitForAllFinish ( )
6456 . then ( ( ) => this . delayPeriod ( ) )
65- . then ( ( ) => func )
66- . then ( ( value ) => {
67- this . logger . methodFull ?.( id ! , { } , value ) ;
57+ . then ( ( ) => {
58+ this . runningId = id ;
6859
60+ return promise ;
61+ } )
62+ . then ( ( value ) => {
63+ this . runningId = undefined ;
6964 this . queue . delete ( id ! ) ;
7065 this . values . set ( id ! , value ) ;
7166
@@ -81,12 +76,12 @@ export class GecutQueue {
8176 * @param {string } id - The id of the promise
8277 * @returns {Promise<unknown> } - The promise value
8378 */
84- getValue ( id : string ) : Promise < unknown > {
79+ getValue < T > ( id : string ) : T | Promise < T > {
8580 if ( this . values . has ( id ) ) {
86- return Promise . resolve ( this . values . get ( id ) ) ;
81+ return this . values . get ( id ) as T ;
8782 }
8883
89- return this . queue . get ( id ) ?? Promise . resolve ( undefined ) ;
84+ return ( this . queue . get ( id ) ?? Promise . resolve ( undefined ) ) as Promise < T > ;
9085 }
9186
9287 /**
@@ -95,7 +90,11 @@ export class GecutQueue {
9590 * @returns {boolean } - Whether the promise is running
9691 */
9792 isRunning ( id : string ) : boolean {
98- return this . queue . has ( id ) ;
93+ return this . runningId === id ;
94+ }
95+
96+ isFinished ( id : string ) : boolean {
97+ return this . values . has ( id ) ;
9998 }
10099
101100 /**
0 commit comments