@plugola/queue 0.0.0-stage → 1.0.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
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))
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
- # Temporary Holding Version
1
+ # @plugola/queue
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
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
+ ```
package/package.json CHANGED
@@ -1,6 +1,37 @@
1
1
  {
2
2
  "name": "@plugola/queue",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
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": "25.9.2",
28
+ "@vitest/runner": "4.1.8",
29
+ "rimraf": "6.1.3",
30
+ "ts-node": "10.9.2",
31
+ "typescript": "6.0.3",
32
+ "vitest": "4.1.8"
33
+ },
34
+ "dependencies": {
35
+ "tslib": "^2.8.1"
36
+ }
37
+ }
@@ -0,0 +1,39 @@
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
+ }
@@ -0,0 +1,25 @@
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
+ }
@@ -0,0 +1,37 @@
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 ADDED
@@ -0,0 +1,32 @@
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
+ }
@@ -0,0 +1,5 @@
1
+ export interface Runnable {
2
+ resume(): void
3
+ pause(): void
4
+ get running(): boolean
5
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export { Queue } from './Queue.ts'
2
+ export { DiscriminatedQueue } from './DiscriminatedQueue.ts'
3
+ export { MethodQueue } from './MethodQueue.ts'
4
+ export type { Runnable } from './Runnable.ts'
5
+ export { Queuable } from './Queuable.ts'
@@ -0,0 +1,97 @@
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
+ })
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../tsconfig.json",
3
+ "compilerOptions": {
4
+ "composite": false,
5
+ "noEmit": true,
6
+ "rootDir": "../"
7
+ },
8
+ "include": ["../src", "."]
9
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
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
+ }