@plugola/queue 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +22 -2
- 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 +35 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 John Wright
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @plugola/queue
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Small runnable queues that buffer work while stopped and drain it in FIFO order
|
|
4
|
+
once started.
|
|
5
|
+
|
|
6
|
+
- **`Queue<T>`** — buffers pushed items while stopped; on `start()` it drains
|
|
7
|
+
them through its `exec` callback and thereafter executes pushes immediately.
|
|
8
|
+
- **`DiscriminatedQueue`** — a `Queue` that routes each item to an executor
|
|
9
|
+
keyed by its `type` discriminant, with `addExec` to register more.
|
|
10
|
+
- **`MethodQueue`** — wraps arbitrary methods so their calls are queued and
|
|
11
|
+
resolve via a `Promise` when the queue runs.
|
|
12
|
+
- **`Runnable`** — the `start()` / `stop()` / `running` interface the queues
|
|
13
|
+
implement.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { Queue } from '@plugola/queue'
|
|
17
|
+
|
|
18
|
+
const seen: number[] = []
|
|
19
|
+
const queue = new Queue<number>((n) => seen.push(n))
|
|
20
|
+
queue.push(1)
|
|
21
|
+
queue.push(2)
|
|
22
|
+
queue.start() // seen === [1, 2]
|
|
23
|
+
```
|
|
@@ -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,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plugola/queue",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Runnable queues that buffer while stopped and drain on start",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc --build",
|
|
12
|
+
"clean": "tsc --build --clean && rimraf dist",
|
|
13
|
+
"start": "tsc --watch --preserveWatchOutput",
|
|
14
|
+
"test": "vitest --run && npm run test:types",
|
|
15
|
+
"test:types": "tsc -p test/tsconfig.json"
|
|
16
|
+
},
|
|
17
|
+
"author": "",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/jg-wright/plugola/issues"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"url": "https://github.com/jg-wright/plugola"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/jg-wright/plugola#readme",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "26.6.6",
|
|
28
|
+
"@vitest/runner": "4.1.11",
|
|
29
|
+
"rimraf": "6.1.3",
|
|
30
|
+
"ts-node": "10.9.2",
|
|
31
|
+
"typescript": "7.0.2",
|
|
32
|
+
"vitest": "5.1.0"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"tslib": "^2.9.1"
|
|
36
|
+
}
|
|
37
|
+
}
|