@plugola/queue 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/DiscriminatedQueue.d.ts +23 -0
- package/dist/DiscriminatedQueue.js +25 -0
- package/dist/DiscriminatedQueue.js.map +1 -0
- package/dist/MethodQueue.d.ts +6 -0
- package/dist/MethodQueue.js +22 -0
- package/dist/MethodQueue.js.map +1 -0
- package/dist/Queuable.d.ts +10 -0
- package/dist/Queuable.js +34 -0
- package/dist/Queuable.js.map +1 -0
- package/dist/Queue.d.ts +10 -0
- package/dist/Queue.js +32 -0
- package/dist/Queue.js.map +1 -0
- package/dist/Runnable.d.ts +5 -0
- package/dist/Runnable.js +2 -0
- package/dist/Runnable.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/package.json +6 -6
- package/CHANGELOG.md +0 -13
- package/src/DiscriminatedQueue.ts +0 -39
- package/src/MethodQueue.ts +0 -25
- package/src/Queuable.ts +0 -37
- package/src/Queue.ts +0 -32
- package/src/Runnable.ts +0 -5
- package/src/index.ts +0 -5
- package/test/queue.test.ts +0 -97
- package/test/tsconfig.json +0 -9
- package/tsconfig.json +0 -12
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Queue } from './Queue.ts';
|
|
2
|
+
export declare class DiscriminatedQueue<T extends Record<PropertyKey, unknown>> extends Queue<DiscriminatedQueueItem<T>> {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(execRecord: DiscriminatedQueueExecRecord<T>, items?: DiscriminatedQueueItem<T>[]);
|
|
5
|
+
/**
|
|
6
|
+
* Registers an executor for a new discriminant, mutating this queue in place
|
|
7
|
+
* and returning it (widened) rather than allocating a replacement. Mutating is
|
|
8
|
+
* deliberate: a fresh instance would start out stopped and silently drop the
|
|
9
|
+
* running state, so a discriminant added after start() would never run.
|
|
10
|
+
*/
|
|
11
|
+
addExec<K extends PropertyKey, U>(type: K, exec: (item: U) => void): DiscriminatedQueue<T & {
|
|
12
|
+
[P in K]: U;
|
|
13
|
+
}>;
|
|
14
|
+
}
|
|
15
|
+
export type DiscriminatedQueueItem<T extends Record<PropertyKey, unknown>> = {
|
|
16
|
+
[K in keyof T]: {
|
|
17
|
+
type: K;
|
|
18
|
+
} & T[K];
|
|
19
|
+
}[keyof T];
|
|
20
|
+
type DiscriminatedQueueExecRecord<T extends Record<PropertyKey, unknown>> = {
|
|
21
|
+
[K in keyof T]: (item: T[K]) => void;
|
|
22
|
+
};
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
var _DiscriminatedQueue_execRecord;
|
|
2
|
+
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
|
+
import { Queue } from './Queue.js';
|
|
4
|
+
export class DiscriminatedQueue extends Queue {
|
|
5
|
+
constructor(execRecord, items) {
|
|
6
|
+
super((item) => {
|
|
7
|
+
execRecord[item.type](item);
|
|
8
|
+
}, items);
|
|
9
|
+
_DiscriminatedQueue_execRecord.set(this, void 0);
|
|
10
|
+
__classPrivateFieldSet(this, _DiscriminatedQueue_execRecord, execRecord, "f");
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Registers an executor for a new discriminant, mutating this queue in place
|
|
14
|
+
* and returning it (widened) rather than allocating a replacement. Mutating is
|
|
15
|
+
* deliberate: a fresh instance would start out stopped and silently drop the
|
|
16
|
+
* running state, so a discriminant added after start() would never run.
|
|
17
|
+
*/
|
|
18
|
+
addExec(type, exec) {
|
|
19
|
+
;
|
|
20
|
+
__classPrivateFieldGet(this, _DiscriminatedQueue_execRecord, "f")[type] = exec;
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
_DiscriminatedQueue_execRecord = new WeakMap();
|
|
25
|
+
//# sourceMappingURL=DiscriminatedQueue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DiscriminatedQueue.js","sourceRoot":"","sources":["../src/DiscriminatedQueue.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAElC,MAAM,OAAO,kBAEX,SAAQ,KAAgC;IAGxC,YACE,UAA2C,EAC3C,KAAmC;QAEnC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE;YACb,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAA;QAC7B,CAAC,EAAE,KAAK,CAAC,CAAA;QARX,iDAA4C;QAS1C,uBAAA,IAAI,kCAAe,UAAU,MAAA,CAAA;IAC/B,CAAC;IAED;;;;;OAKG;IACH,OAAO,CACL,IAAO,EACP,IAAuB;QAEvB,CAAC;QAAC,uBAAA,IAAI,sCAAyD,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;QAC5E,OAAO,IAA0D,CAAA;IACnE,CAAC;CACF"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Queue } from './Queue.ts';
|
|
2
|
+
export declare class MethodQueue extends Queue<() => any> {
|
|
3
|
+
constructor();
|
|
4
|
+
queueMethod<R, Args extends unknown[]>(method: (...args: Args) => R): (...args: Args) => Promise<Awaited<R>>;
|
|
5
|
+
push<R>(fn: () => R): Promise<Awaited<R>>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Queue } from './Queue.js';
|
|
2
|
+
export class MethodQueue extends Queue {
|
|
3
|
+
constructor() {
|
|
4
|
+
super((fn) => fn());
|
|
5
|
+
}
|
|
6
|
+
queueMethod(method) {
|
|
7
|
+
return (...args) => this.push(() => method(...args));
|
|
8
|
+
}
|
|
9
|
+
push(fn) {
|
|
10
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
11
|
+
super.push(() => {
|
|
12
|
+
try {
|
|
13
|
+
resolve(fn());
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
reject(error);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
return promise;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=MethodQueue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MethodQueue.js","sourceRoot":"","sources":["../src/MethodQueue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAElC,MAAM,OAAO,WAAY,SAAQ,KAAgB;IAC/C;QACE,KAAK,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAA;IACrB,CAAC;IAED,WAAW,CACT,MAA4B;QAE5B,OAAO,CAAC,GAAG,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC,CAAA;IACtD,CAAC;IAEQ,IAAI,CAAI,EAAW;QAC1B,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,aAAa,EAAc,CAAA;QACxE,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE;YACd,IAAI,CAAC;gBACH,OAAO,CAAC,EAAE,EAAgB,CAAC,CAAA;YAC7B,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,CAAC,KAAK,CAAC,CAAA;YACf,CAAC;QACH,CAAC,CAAC,CAAA;QACF,OAAO,OAAO,CAAA;IAChB,CAAC;CACF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { MethodQueue } from './MethodQueue.ts';
|
|
2
|
+
import { Runnable } from './Runnable.ts';
|
|
3
|
+
export declare function QueuableMixin<C extends new (...args: any) => any>(Class: C): new (...args: ConstructorParameters<C>) => InstanceType<C> & Queuable;
|
|
4
|
+
export declare class Queuable implements Runnable {
|
|
5
|
+
readonly queue: MethodQueue;
|
|
6
|
+
constructor(queue?: MethodQueue);
|
|
7
|
+
get running(): boolean;
|
|
8
|
+
resume(): void;
|
|
9
|
+
pause(): void;
|
|
10
|
+
}
|
package/dist/Queuable.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { MethodQueue } from './MethodQueue.js';
|
|
2
|
+
export function QueuableMixin(Class) {
|
|
3
|
+
class QueuableClass extends Class {
|
|
4
|
+
constructor() {
|
|
5
|
+
super(...arguments);
|
|
6
|
+
this.queue = new MethodQueue();
|
|
7
|
+
}
|
|
8
|
+
get running() {
|
|
9
|
+
return this.queue.running;
|
|
10
|
+
}
|
|
11
|
+
resume() {
|
|
12
|
+
this.queue.resume();
|
|
13
|
+
}
|
|
14
|
+
pause() {
|
|
15
|
+
this.queue.pause();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return QueuableClass;
|
|
19
|
+
}
|
|
20
|
+
export class Queuable {
|
|
21
|
+
constructor(queue = new MethodQueue()) {
|
|
22
|
+
this.queue = queue;
|
|
23
|
+
}
|
|
24
|
+
get running() {
|
|
25
|
+
return this.queue.running;
|
|
26
|
+
}
|
|
27
|
+
resume() {
|
|
28
|
+
this.queue.resume();
|
|
29
|
+
}
|
|
30
|
+
pause() {
|
|
31
|
+
this.queue.pause();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=Queuable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Queuable.js","sourceRoot":"","sources":["../src/Queuable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAG9C,MAAM,UAAU,aAAa,CAC3B,KAAQ;IAER,MAAM,aAAc,SAAQ,KAAK;QAAjC;;YACE,UAAK,GAAG,IAAI,WAAW,EAAE,CAAA;QAU3B,CAAC;QATC,IAAI,OAAO;YACT,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAA;QAC3B,CAAC;QACD,MAAM;YACJ,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAA;QACrB,CAAC;QACD,KAAK;YACH,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;QACpB,CAAC;KACF;IAED,OAAO,aAAa,CAAA;AACtB,CAAC;AAED,MAAM,OAAO,QAAQ;IAEnB,YAAY,KAAK,GAAG,IAAI,WAAW,EAAE;QACnC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;IACpB,CAAC;IACD,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAA;IAC3B,CAAC;IACD,MAAM;QACJ,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAA;IACrB,CAAC;IACD,KAAK;QACH,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;IACpB,CAAC;CACF"}
|
package/dist/Queue.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Runnable } from './Runnable.ts';
|
|
2
|
+
export declare class Queue<T> implements Runnable {
|
|
3
|
+
#private;
|
|
4
|
+
protected readonly items: T[];
|
|
5
|
+
constructor(exec: (item: T) => void, items?: T[]);
|
|
6
|
+
get running(): boolean;
|
|
7
|
+
push(item: T): void;
|
|
8
|
+
resume(): void;
|
|
9
|
+
pause(): void;
|
|
10
|
+
}
|
package/dist/Queue.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
var _Queue_exec, _Queue_running;
|
|
2
|
+
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
|
+
export class Queue {
|
|
4
|
+
constructor(exec, items = []) {
|
|
5
|
+
_Queue_exec.set(this, void 0);
|
|
6
|
+
_Queue_running.set(this, false);
|
|
7
|
+
__classPrivateFieldSet(this, _Queue_exec, exec, "f");
|
|
8
|
+
this.items = items;
|
|
9
|
+
}
|
|
10
|
+
get running() {
|
|
11
|
+
return __classPrivateFieldGet(this, _Queue_running, "f");
|
|
12
|
+
}
|
|
13
|
+
push(item) {
|
|
14
|
+
if (__classPrivateFieldGet(this, _Queue_running, "f"))
|
|
15
|
+
__classPrivateFieldGet(this, _Queue_exec, "f").call(this, item);
|
|
16
|
+
else
|
|
17
|
+
this.items.push(item);
|
|
18
|
+
}
|
|
19
|
+
resume() {
|
|
20
|
+
if (__classPrivateFieldGet(this, _Queue_running, "f"))
|
|
21
|
+
return;
|
|
22
|
+
__classPrivateFieldSet(this, _Queue_running, true, "f");
|
|
23
|
+
let item;
|
|
24
|
+
while (__classPrivateFieldGet(this, _Queue_running, "f") && (item = this.items.shift()))
|
|
25
|
+
this.push(item);
|
|
26
|
+
}
|
|
27
|
+
pause() {
|
|
28
|
+
__classPrivateFieldSet(this, _Queue_running, false, "f");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
_Queue_exec = new WeakMap(), _Queue_running = new WeakMap();
|
|
32
|
+
//# sourceMappingURL=Queue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Queue.js","sourceRoot":"","sources":["../src/Queue.ts"],"names":[],"mappings":";;AAEA,MAAM,OAAO,KAAK;IAIhB,YAAY,IAAuB,EAAE,KAAK,GAAQ,EAAE;QAHpD,8BAAwB;QAQxB,yBAAW,KAAK,EAAA;QAJd,uBAAA,IAAI,eAAS,IAAI,MAAA,CAAA;QACjB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;IACpB,CAAC;IAGD,IAAI,OAAO;QACT,OAAO,uBAAA,IAAI,sBAAS,CAAA;IACtB,CAAC;IAED,IAAI,CAAC,IAAO;QACV,IAAI,uBAAA,IAAI,sBAAS;YAAE,uBAAA,IAAI,mBAAM,MAAV,IAAI,EAAO,IAAI,CAAC,CAAA;;YAC9B,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC5B,CAAC;IAED,MAAM;QACJ,IAAI,uBAAA,IAAI,sBAAS;YAAE,OAAM;QACzB,uBAAA,IAAI,kBAAY,IAAI,MAAA,CAAA;QACpB,IAAI,IAAmB,CAAA;QACvB,OAAO,uBAAA,IAAI,sBAAS,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;YAAE,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACtE,CAAC;IAED,KAAK;QACH,uBAAA,IAAI,kBAAY,KAAK,MAAA,CAAA;IACvB,CAAC;CACF"}
|
package/dist/Runnable.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Runnable.js","sourceRoot":"","sources":["../src/Runnable.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAC5D,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAE9C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plugola/queue",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Runnable queues that buffer while stopped and drain on start",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -24,14 +24,14 @@
|
|
|
24
24
|
},
|
|
25
25
|
"homepage": "https://github.com/jg-wright/plugola#readme",
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@types/node": "
|
|
28
|
-
"@vitest/runner": "4.1.
|
|
27
|
+
"@types/node": "26.6.6",
|
|
28
|
+
"@vitest/runner": "4.1.11",
|
|
29
29
|
"rimraf": "6.1.3",
|
|
30
30
|
"ts-node": "10.9.2",
|
|
31
|
-
"typescript": "
|
|
32
|
-
"vitest": "
|
|
31
|
+
"typescript": "7.0.2",
|
|
32
|
+
"vitest": "5.1.0"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"tslib": "^2.
|
|
35
|
+
"tslib": "^2.9.1"
|
|
36
36
|
}
|
|
37
37
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
## 1.0.0 (2026-11-02)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
### Features
|
|
7
|
-
|
|
8
|
-
* **queue:** add queuable class ([0954c61](https://github.com/jg-wright/plugola/commit/0954c6178b158390539466348c0d6276512c2835))
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
### Bug Fixes
|
|
12
|
-
|
|
13
|
-
* **message-bus:** auto resume partipants ([b872449](https://github.com/jg-wright/plugola/commit/b8724493924d4238c32bc3d736ebf09ead5924ea))
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { Queue } from './Queue.ts'
|
|
2
|
-
|
|
3
|
-
export class DiscriminatedQueue<
|
|
4
|
-
T extends Record<PropertyKey, unknown>,
|
|
5
|
-
> extends Queue<DiscriminatedQueueItem<T>> {
|
|
6
|
-
#execRecord: DiscriminatedQueueExecRecord<T>
|
|
7
|
-
|
|
8
|
-
constructor(
|
|
9
|
-
execRecord: DiscriminatedQueueExecRecord<T>,
|
|
10
|
-
items?: DiscriminatedQueueItem<T>[],
|
|
11
|
-
) {
|
|
12
|
-
super((item) => {
|
|
13
|
-
execRecord[item.type](item)
|
|
14
|
-
}, items)
|
|
15
|
-
this.#execRecord = execRecord
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Registers an executor for a new discriminant, mutating this queue in place
|
|
20
|
-
* and returning it (widened) rather than allocating a replacement. Mutating is
|
|
21
|
-
* deliberate: a fresh instance would start out stopped and silently drop the
|
|
22
|
-
* running state, so a discriminant added after start() would never run.
|
|
23
|
-
*/
|
|
24
|
-
addExec<K extends PropertyKey, U>(
|
|
25
|
-
type: K,
|
|
26
|
-
exec: (item: U) => void,
|
|
27
|
-
): DiscriminatedQueue<T & { [P in K]: U }> {
|
|
28
|
-
;(this.#execRecord as Record<PropertyKey, (item: any) => void>)[type] = exec
|
|
29
|
-
return this as unknown as DiscriminatedQueue<T & { [P in K]: U }>
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export type DiscriminatedQueueItem<T extends Record<PropertyKey, unknown>> = {
|
|
34
|
-
[K in keyof T]: { type: K } & T[K]
|
|
35
|
-
}[keyof T]
|
|
36
|
-
|
|
37
|
-
type DiscriminatedQueueExecRecord<T extends Record<PropertyKey, unknown>> = {
|
|
38
|
-
[K in keyof T]: (item: T[K]) => void
|
|
39
|
-
}
|
package/src/MethodQueue.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { Queue } from './Queue.ts'
|
|
2
|
-
|
|
3
|
-
export class MethodQueue extends Queue<() => any> {
|
|
4
|
-
constructor() {
|
|
5
|
-
super((fn) => fn())
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
queueMethod<R, Args extends unknown[]>(
|
|
9
|
-
method: (...args: Args) => R,
|
|
10
|
-
): (...args: Args) => Promise<Awaited<R>> {
|
|
11
|
-
return (...args) => this.push(() => method(...args))
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
override push<R>(fn: () => R): Promise<Awaited<R>> {
|
|
15
|
-
const { promise, resolve, reject } = Promise.withResolvers<Awaited<R>>()
|
|
16
|
-
super.push(() => {
|
|
17
|
-
try {
|
|
18
|
-
resolve(fn() as Awaited<R>)
|
|
19
|
-
} catch (error) {
|
|
20
|
-
reject(error)
|
|
21
|
-
}
|
|
22
|
-
})
|
|
23
|
-
return promise
|
|
24
|
-
}
|
|
25
|
-
}
|
package/src/Queuable.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { MethodQueue } from './MethodQueue.ts'
|
|
2
|
-
import { Runnable } from './Runnable.ts'
|
|
3
|
-
|
|
4
|
-
export function QueuableMixin<C extends new (...args: any) => any>(
|
|
5
|
-
Class: C,
|
|
6
|
-
): new (...args: ConstructorParameters<C>) => InstanceType<C> & Queuable {
|
|
7
|
-
class QueuableClass extends Class implements Runnable {
|
|
8
|
-
queue = new MethodQueue()
|
|
9
|
-
get running() {
|
|
10
|
-
return this.queue.running
|
|
11
|
-
}
|
|
12
|
-
resume() {
|
|
13
|
-
this.queue.resume()
|
|
14
|
-
}
|
|
15
|
-
pause() {
|
|
16
|
-
this.queue.pause()
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return QueuableClass
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class Queuable implements Runnable {
|
|
24
|
-
readonly queue: MethodQueue
|
|
25
|
-
constructor(queue = new MethodQueue()) {
|
|
26
|
-
this.queue = queue
|
|
27
|
-
}
|
|
28
|
-
get running() {
|
|
29
|
-
return this.queue.running
|
|
30
|
-
}
|
|
31
|
-
resume() {
|
|
32
|
-
this.queue.resume()
|
|
33
|
-
}
|
|
34
|
-
pause() {
|
|
35
|
-
this.queue.pause()
|
|
36
|
-
}
|
|
37
|
-
}
|
package/src/Queue.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { Runnable } from './Runnable.ts'
|
|
2
|
-
|
|
3
|
-
export class Queue<T> implements Runnable {
|
|
4
|
-
#exec: (item: T) => void
|
|
5
|
-
protected readonly items: T[]
|
|
6
|
-
|
|
7
|
-
constructor(exec: (item: T) => void, items: T[] = []) {
|
|
8
|
-
this.#exec = exec
|
|
9
|
-
this.items = items
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
#running = false
|
|
13
|
-
get running() {
|
|
14
|
-
return this.#running
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
push(item: T) {
|
|
18
|
-
if (this.#running) this.#exec(item)
|
|
19
|
-
else this.items.push(item)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
resume() {
|
|
23
|
-
if (this.#running) return
|
|
24
|
-
this.#running = true
|
|
25
|
-
let item: T | undefined
|
|
26
|
-
while (this.#running && (item = this.items.shift())) this.push(item)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
pause() {
|
|
30
|
-
this.#running = false
|
|
31
|
-
}
|
|
32
|
-
}
|
package/src/Runnable.ts
DELETED
package/src/index.ts
DELETED
package/test/queue.test.ts
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { expect, test, vi } from 'vitest'
|
|
2
|
-
import { DiscriminatedQueue } from '../src/DiscriminatedQueue.ts'
|
|
3
|
-
import { MethodQueue } from '../src/MethodQueue.ts'
|
|
4
|
-
import { Queue } from '../src/Queue.ts'
|
|
5
|
-
|
|
6
|
-
test('discriminated queue', () => {
|
|
7
|
-
const fooSpy = vi.fn((_item: { name: string }) => {})
|
|
8
|
-
const barSpy = vi.fn((_item: { num: number }) => {})
|
|
9
|
-
|
|
10
|
-
const queue = new DiscriminatedQueue({
|
|
11
|
-
foo: fooSpy,
|
|
12
|
-
bar: barSpy,
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
queue.push({ type: 'foo', name: 'My name' })
|
|
16
|
-
queue.push({ type: 'bar', num: 123 })
|
|
17
|
-
|
|
18
|
-
expect(fooSpy).not.toHaveBeenCalled()
|
|
19
|
-
expect(barSpy).not.toHaveBeenCalled()
|
|
20
|
-
|
|
21
|
-
queue.resume()
|
|
22
|
-
|
|
23
|
-
expect(fooSpy).toHaveBeenCalledWith({ type: 'foo', name: 'My name' })
|
|
24
|
-
expect(barSpy).toHaveBeenCalledWith({ type: 'bar', num: 123 })
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
test('Queue buffers while paused and drains in FIFO order on resume', () => {
|
|
28
|
-
const seen: number[] = []
|
|
29
|
-
const queue = new Queue<number>((n) => seen.push(n))
|
|
30
|
-
queue.push(1)
|
|
31
|
-
queue.push(2)
|
|
32
|
-
queue.push(3)
|
|
33
|
-
expect(seen).toEqual([])
|
|
34
|
-
queue.resume()
|
|
35
|
-
expect(seen).toEqual([1, 2, 3])
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
test('Queue executes immediately once running', () => {
|
|
39
|
-
const seen: number[] = []
|
|
40
|
-
const queue = new Queue<number>((n) => seen.push(n))
|
|
41
|
-
queue.resume()
|
|
42
|
-
queue.push(1)
|
|
43
|
-
expect(seen).toEqual([1])
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
test('Queue stops draining when paused mid-flight', () => {
|
|
47
|
-
const seen: number[] = []
|
|
48
|
-
const queue = new Queue<number>((n) => {
|
|
49
|
-
seen.push(n)
|
|
50
|
-
if (n === 2) queue.pause()
|
|
51
|
-
})
|
|
52
|
-
queue.push(1)
|
|
53
|
-
queue.push(2)
|
|
54
|
-
queue.push(3)
|
|
55
|
-
queue.resume()
|
|
56
|
-
expect(seen).toEqual([1, 2])
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
test('MethodQueue defers a call until resumed, then runs it', () => {
|
|
60
|
-
const queue = new MethodQueue()
|
|
61
|
-
const spy = vi.fn((a: number, b: number) => a + b)
|
|
62
|
-
const call = queue.queueMethod(spy)
|
|
63
|
-
call(2, 3)
|
|
64
|
-
expect(spy).not.toHaveBeenCalled()
|
|
65
|
-
queue.resume()
|
|
66
|
-
expect(spy).toHaveBeenCalledWith(2, 3)
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
test('MethodQueue preserves running state when a method is queued after resume', () => {
|
|
70
|
-
const queue = new MethodQueue()
|
|
71
|
-
queue.resume()
|
|
72
|
-
expect(queue.running).toBe(true)
|
|
73
|
-
queue.queueMethod(() => {})
|
|
74
|
-
expect(queue.running).toBe(true)
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
test('MethodQueue runs a method queued after resume immediately', () => {
|
|
78
|
-
const queue = new MethodQueue()
|
|
79
|
-
queue.resume()
|
|
80
|
-
const spy = vi.fn()
|
|
81
|
-
const call = queue.queueMethod(spy)
|
|
82
|
-
call('hi')
|
|
83
|
-
expect(spy).toHaveBeenCalledWith('hi')
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
test('MethodQueue methods queued before and after resume share running state', () => {
|
|
87
|
-
const queue = new MethodQueue()
|
|
88
|
-
const early = vi.fn()
|
|
89
|
-
const callEarly = queue.queueMethod(early)
|
|
90
|
-
const late = vi.fn()
|
|
91
|
-
const callLate = queue.queueMethod(late)
|
|
92
|
-
callEarly('a')
|
|
93
|
-
queue.resume()
|
|
94
|
-
callLate('b')
|
|
95
|
-
expect(early).toHaveBeenCalledWith('a')
|
|
96
|
-
expect(late).toHaveBeenCalledWith('b')
|
|
97
|
-
})
|
package/test/tsconfig.json
DELETED
package/tsconfig.json
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"allowImportingTsExtensions": true,
|
|
5
|
-
"composite": true,
|
|
6
|
-
"rewriteRelativeImportExtensions": true,
|
|
7
|
-
"rootDir": "./src",
|
|
8
|
-
"lib": ["ESNext"],
|
|
9
|
-
"outDir": "dist"
|
|
10
|
-
},
|
|
11
|
-
"include": ["src"]
|
|
12
|
-
}
|