Skip to content

Commit 9d1ebcf

Browse files
committed
feat(utilities): queue utility
1 parent 5f1ac6b commit 9d1ebcf

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

packages/utilities/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"**/*.{js,mjs,cjs,map,d.ts,html,md}"
4040
],
4141
"dependencies": {
42-
"@gecut/logger": "workspace:^",
4342
"@gecut/types": "workspace:^"
4443
},
4544
"devDependencies": {

packages/utilities/src/queue.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import {GecutLogger} from '@gecut/logger';
2-
31
import {uid} from './uid';
42
import {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
/**

packages/utilities/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99

1010
"include": ["src/**/*.ts"],
1111
"exclude": [],
12-
"references": [{"path": "../types"}, {"path": "../logger"}]
12+
"references": [{"path": "../types"}]
1313
}

0 commit comments

Comments
 (0)