@procedures/client 0.2.0 → 0.2.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/CHANGELOG.md +6 -0
- package/README.md +4 -4
- package/dist/.tsbuildinfo +1 -1
- package/dist/browser.d.ts +1 -1
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js.map +1 -1
- package/dist/procedures/invoke.d.ts +6 -1
- package/dist/procedures/invoke.d.ts.map +1 -1
- package/dist/procedures/invoke.js +2 -2
- package/dist/procedures/invoke.js.map +1 -1
- package/dist/server/host-policy.d.ts +3 -3
- package/dist/server/host-policy.d.ts.map +1 -1
- package/dist/server/procedure-server.d.ts +2 -2
- package/dist/server/procedure-server.d.ts.map +1 -1
- package/dist/server/procedure-server.js.map +1 -1
- package/package.json +1 -1
- package/src/browser.ts +1 -0
- package/src/procedures/invoke.ts +10 -4
- package/src/server/host-policy.ts +3 -3
- package/src/server/procedure-server.ts +2 -2
- package/dist/client/middleware/batching.d.ts +0 -102
- package/dist/client/middleware/batching.d.ts.map +0 -1
- package/dist/client/middleware/batching.js +0 -245
- package/dist/client/middleware/batching.js.map +0 -1
- package/dist/collections/async/async-queue.d.ts +0 -144
- package/dist/collections/async/async-queue.d.ts.map +0 -1
- package/dist/collections/async/async-queue.js +0 -347
- package/dist/collections/async/async-queue.js.map +0 -1
- package/dist/collections/async/channels.d.ts +0 -188
- package/dist/collections/async/channels.d.ts.map +0 -1
- package/dist/collections/async/channels.js +0 -312
- package/dist/collections/async/channels.js.map +0 -1
- package/dist/collections/behaviors/bounded.d.ts +0 -79
- package/dist/collections/behaviors/bounded.d.ts.map +0 -1
- package/dist/collections/behaviors/bounded.js +0 -384
- package/dist/collections/behaviors/bounded.js.map +0 -1
- package/dist/collections/behaviors/evented.d.ts +0 -154
- package/dist/collections/behaviors/evented.d.ts.map +0 -1
- package/dist/collections/behaviors/evented.js +0 -299
- package/dist/collections/behaviors/evented.js.map +0 -1
- package/dist/collections/behaviors/lru.d.ts +0 -84
- package/dist/collections/behaviors/lru.d.ts.map +0 -1
- package/dist/collections/behaviors/lru.js +0 -330
- package/dist/collections/behaviors/lru.js.map +0 -1
- package/dist/collections/behaviors/readonly.d.ts +0 -73
- package/dist/collections/behaviors/readonly.d.ts.map +0 -1
- package/dist/collections/behaviors/readonly.js +0 -158
- package/dist/collections/behaviors/readonly.js.map +0 -1
- package/dist/collections/behaviors/safe.d.ts +0 -163
- package/dist/collections/behaviors/safe.d.ts.map +0 -1
- package/dist/collections/behaviors/safe.js +0 -172
- package/dist/collections/behaviors/safe.js.map +0 -1
- package/dist/collections/behaviors/synchronized.d.ts +0 -88
- package/dist/collections/behaviors/synchronized.d.ts.map +0 -1
- package/dist/collections/behaviors/synchronized.js +0 -294
- package/dist/collections/behaviors/synchronized.js.map +0 -1
- package/dist/collections/behaviors/ttl.d.ts +0 -118
- package/dist/collections/behaviors/ttl.d.ts.map +0 -1
- package/dist/collections/behaviors/ttl.js +0 -338
- package/dist/collections/behaviors/ttl.js.map +0 -1
- package/dist/collections/core/effects.d.ts +0 -176
- package/dist/collections/core/effects.d.ts.map +0 -1
- package/dist/collections/core/effects.js +0 -180
- package/dist/collections/core/effects.js.map +0 -1
- package/dist/collections/core/events.d.ts +0 -192
- package/dist/collections/core/events.d.ts.map +0 -1
- package/dist/collections/core/events.js +0 -291
- package/dist/collections/core/events.js.map +0 -1
- package/dist/collections/core/middleware.d.ts +0 -231
- package/dist/collections/core/middleware.d.ts.map +0 -1
- package/dist/collections/core/middleware.js +0 -161
- package/dist/collections/core/middleware.js.map +0 -1
- package/dist/collections/core/policies.d.ts +0 -220
- package/dist/collections/core/policies.d.ts.map +0 -1
- package/dist/collections/core/policies.js +0 -48
- package/dist/collections/core/policies.js.map +0 -1
- package/dist/collections/core/traits.d.ts +0 -142
- package/dist/collections/core/traits.d.ts.map +0 -1
- package/dist/collections/core/traits.js +0 -8
- package/dist/collections/core/traits.js.map +0 -1
- package/dist/collections/examples.d.ts +0 -13
- package/dist/collections/examples.d.ts.map +0 -1
- package/dist/collections/examples.js +0 -302
- package/dist/collections/examples.js.map +0 -1
- package/dist/collections/fx/collectors.d.ts +0 -190
- package/dist/collections/fx/collectors.d.ts.map +0 -1
- package/dist/collections/fx/collectors.js +0 -369
- package/dist/collections/fx/collectors.js.map +0 -1
- package/dist/collections/fx/iter.d.ts +0 -166
- package/dist/collections/fx/iter.d.ts.map +0 -1
- package/dist/collections/fx/iter.js +0 -421
- package/dist/collections/fx/iter.js.map +0 -1
- package/dist/collections/impl/array-deque.d.ts +0 -107
- package/dist/collections/impl/array-deque.d.ts.map +0 -1
- package/dist/collections/impl/array-deque.js +0 -381
- package/dist/collections/impl/array-deque.js.map +0 -1
- package/dist/collections/impl/array-list.d.ts +0 -101
- package/dist/collections/impl/array-list.d.ts.map +0 -1
- package/dist/collections/impl/array-list.js +0 -387
- package/dist/collections/impl/array-list.js.map +0 -1
- package/dist/collections/impl/hash-map.d.ts +0 -141
- package/dist/collections/impl/hash-map.d.ts.map +0 -1
- package/dist/collections/impl/hash-map.js +0 -450
- package/dist/collections/impl/hash-map.js.map +0 -1
- package/dist/collections/impl/hash-set.d.ts +0 -125
- package/dist/collections/impl/hash-set.d.ts.map +0 -1
- package/dist/collections/impl/hash-set.js +0 -264
- package/dist/collections/impl/hash-set.js.map +0 -1
- package/dist/collections/impl/linked-hash-map.d.ts +0 -141
- package/dist/collections/impl/linked-hash-map.d.ts.map +0 -1
- package/dist/collections/impl/linked-hash-map.js +0 -472
- package/dist/collections/impl/linked-hash-map.js.map +0 -1
- package/dist/collections/impl/linked-list.d.ts +0 -109
- package/dist/collections/impl/linked-list.d.ts.map +0 -1
- package/dist/collections/impl/linked-list.js +0 -491
- package/dist/collections/impl/linked-list.js.map +0 -1
- package/dist/collections/impl/priority-queue.d.ts +0 -134
- package/dist/collections/impl/priority-queue.d.ts.map +0 -1
- package/dist/collections/impl/priority-queue.js +0 -336
- package/dist/collections/impl/priority-queue.js.map +0 -1
- package/dist/collections/impl/tree-map.d.ts +0 -146
- package/dist/collections/impl/tree-map.d.ts.map +0 -1
- package/dist/collections/impl/tree-map.js +0 -781
- package/dist/collections/impl/tree-map.js.map +0 -1
- package/dist/collections/impl/tree-set.d.ts +0 -112
- package/dist/collections/impl/tree-set.d.ts.map +0 -1
- package/dist/collections/impl/tree-set.js +0 -374
- package/dist/collections/impl/tree-set.js.map +0 -1
- package/dist/collections/index.d.ts +0 -105
- package/dist/collections/index.d.ts.map +0 -1
- package/dist/collections/index.js +0 -135
- package/dist/collections/index.js.map +0 -1
- package/dist/collections/interfaces/collection.d.ts +0 -119
- package/dist/collections/interfaces/collection.d.ts.map +0 -1
- package/dist/collections/interfaces/collection.js +0 -8
- package/dist/collections/interfaces/collection.js.map +0 -1
- package/dist/collections/interfaces/list.d.ts +0 -247
- package/dist/collections/interfaces/list.d.ts.map +0 -1
- package/dist/collections/interfaces/list.js +0 -8
- package/dist/collections/interfaces/list.js.map +0 -1
- package/dist/collections/interfaces/map.d.ts +0 -344
- package/dist/collections/interfaces/map.d.ts.map +0 -1
- package/dist/collections/interfaces/map.js +0 -8
- package/dist/collections/interfaces/map.js.map +0 -1
- package/dist/collections/interfaces/queue.d.ts +0 -304
- package/dist/collections/interfaces/queue.d.ts.map +0 -1
- package/dist/collections/interfaces/queue.js +0 -8
- package/dist/collections/interfaces/queue.js.map +0 -1
- package/dist/collections/interfaces/set.d.ts +0 -137
- package/dist/collections/interfaces/set.d.ts.map +0 -1
- package/dist/collections/interfaces/set.js +0 -8
- package/dist/collections/interfaces/set.js.map +0 -1
- package/dist/collections/storage/api.d.ts +0 -109
- package/dist/collections/storage/api.d.ts.map +0 -1
- package/dist/collections/storage/api.js +0 -182
- package/dist/collections/storage/api.js.map +0 -1
- package/dist/collections/storage/hybrid.d.ts +0 -164
- package/dist/collections/storage/hybrid.d.ts.map +0 -1
- package/dist/collections/storage/hybrid.js +0 -416
- package/dist/collections/storage/hybrid.js.map +0 -1
- package/dist/collections/storage/index.d.ts +0 -16
- package/dist/collections/storage/index.d.ts.map +0 -1
- package/dist/collections/storage/index.js +0 -13
- package/dist/collections/storage/index.js.map +0 -1
- package/dist/collections/storage/interface.d.ts +0 -141
- package/dist/collections/storage/interface.d.ts.map +0 -1
- package/dist/collections/storage/interface.js +0 -23
- package/dist/collections/storage/interface.js.map +0 -1
- package/dist/collections/storage/memory.d.ts +0 -68
- package/dist/collections/storage/memory.d.ts.map +0 -1
- package/dist/collections/storage/memory.js +0 -154
- package/dist/collections/storage/memory.js.map +0 -1
- package/dist/collections/utils/defaults.d.ts +0 -135
- package/dist/collections/utils/defaults.d.ts.map +0 -1
- package/dist/collections/utils/defaults.js +0 -322
- package/dist/collections/utils/defaults.js.map +0 -1
- package/dist/collections/utils/factories.d.ts +0 -144
- package/dist/collections/utils/factories.d.ts.map +0 -1
- package/dist/collections/utils/factories.js +0 -293
- package/dist/collections/utils/factories.js.map +0 -1
- package/dist/collections/utils/helpers.d.ts +0 -137
- package/dist/collections/utils/helpers.d.ts.map +0 -1
- package/dist/collections/utils/helpers.js +0 -221
- package/dist/collections/utils/helpers.js.map +0 -1
- package/dist/collections.d.ts +0 -12
- package/dist/collections.d.ts.map +0 -1
- package/dist/collections.js +0 -10
- package/dist/collections.js.map +0 -1
- package/dist/components/define.d.ts +0 -134
- package/dist/components/define.d.ts.map +0 -1
- package/dist/components/define.js +0 -486
- package/dist/components/define.js.map +0 -1
- package/dist/components/index.d.ts +0 -39
- package/dist/components/index.d.ts.map +0 -1
- package/dist/components/index.js +0 -38
- package/dist/components/index.js.map +0 -1
- package/dist/components/types.d.ts +0 -264
- package/dist/components/types.d.ts.map +0 -1
- package/dist/components/types.js +0 -46
- package/dist/components/types.js.map +0 -1
- package/dist/procedures/storage/adapter.d.ts +0 -148
- package/dist/procedures/storage/adapter.d.ts.map +0 -1
- package/dist/procedures/storage/adapter.js +0 -214
- package/dist/procedures/storage/adapter.js.map +0 -1
- package/dist/procedures/storage/api.d.ts +0 -125
- package/dist/procedures/storage/api.d.ts.map +0 -1
- package/dist/procedures/storage/api.js +0 -220
- package/dist/procedures/storage/api.js.map +0 -1
- package/dist/procedures/storage/factory.d.ts +0 -134
- package/dist/procedures/storage/factory.d.ts.map +0 -1
- package/dist/procedures/storage/factory.js +0 -166
- package/dist/procedures/storage/factory.js.map +0 -1
- package/dist/procedures/storage/hybrid.d.ts +0 -193
- package/dist/procedures/storage/hybrid.d.ts.map +0 -1
- package/dist/procedures/storage/hybrid.js +0 -519
- package/dist/procedures/storage/hybrid.js.map +0 -1
- package/dist/procedures/storage/index.d.ts +0 -17
- package/dist/procedures/storage/index.d.ts.map +0 -1
- package/dist/procedures/storage/index.js +0 -17
- package/dist/procedures/storage/index.js.map +0 -1
- package/dist/procedures/storage/procedures.d.ts +0 -39
- package/dist/procedures/storage/procedures.d.ts.map +0 -1
- package/dist/procedures/storage/procedures.js +0 -261
- package/dist/procedures/storage/procedures.js.map +0 -1
- package/dist/procedures/storage/serialization.d.ts +0 -123
- package/dist/procedures/storage/serialization.d.ts.map +0 -1
- package/dist/procedures/storage/serialization.js +0 -216
- package/dist/procedures/storage/serialization.js.map +0 -1
- package/dist/procedures/storage/synced-registry.d.ts +0 -246
- package/dist/procedures/storage/synced-registry.d.ts.map +0 -1
- package/dist/procedures/storage/synced-registry.js +0 -575
- package/dist/procedures/storage/synced-registry.js.map +0 -1
- package/dist/procedures/storage/types.d.ts +0 -130
- package/dist/procedures/storage/types.d.ts.map +0 -1
- package/dist/procedures/storage/types.js +0 -8
- package/dist/procedures/storage/types.js.map +0 -1
|
@@ -1,347 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AsyncQueue - An async iterable queue with backpressure support.
|
|
3
|
-
*
|
|
4
|
-
* Designed for producer-consumer patterns where producers can wait when
|
|
5
|
-
* the queue is full and consumers can wait when the queue is empty.
|
|
6
|
-
* Implements AsyncIterable for use with for-await-of loops.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* const queue = asyncQueue<number>({ capacity: 10 })
|
|
10
|
-
*
|
|
11
|
-
* // Producer
|
|
12
|
-
* await queue.put(42) // Waits if queue is full
|
|
13
|
-
*
|
|
14
|
-
* // Consumer
|
|
15
|
-
* const value = await queue.take() // Waits if queue is empty
|
|
16
|
-
*
|
|
17
|
-
* // Async iteration
|
|
18
|
-
* for await (const value of queue) {
|
|
19
|
-
* console.log(value)
|
|
20
|
-
* }
|
|
21
|
-
*/
|
|
22
|
-
/**
|
|
23
|
-
* AsyncQueue<T> - Thread-safe async queue with backpressure.
|
|
24
|
-
*
|
|
25
|
-
* Features:
|
|
26
|
-
* - Blocking put() when full (backpressure)
|
|
27
|
-
* - Blocking take() when empty
|
|
28
|
-
* - AsyncIterable support
|
|
29
|
-
* - Graceful closing
|
|
30
|
-
* - Timeouts
|
|
31
|
-
*
|
|
32
|
-
* Perfect for:
|
|
33
|
-
* - Producer-consumer patterns
|
|
34
|
-
* - Rate limiting
|
|
35
|
-
* - Task queues
|
|
36
|
-
* - Stream processing
|
|
37
|
-
*/
|
|
38
|
-
export class AsyncQueue {
|
|
39
|
-
buffer = [];
|
|
40
|
-
putters = [];
|
|
41
|
-
takers = [];
|
|
42
|
-
_isClosed = false;
|
|
43
|
-
capacity;
|
|
44
|
-
defaultTimeout;
|
|
45
|
-
constructor(options = {}) {
|
|
46
|
-
this.capacity = options.capacity ?? Infinity;
|
|
47
|
-
if (options.timeout !== undefined) {
|
|
48
|
-
this.defaultTimeout = options.timeout;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
// ========================================================================
|
|
52
|
-
// Size and state
|
|
53
|
-
// ========================================================================
|
|
54
|
-
get size() {
|
|
55
|
-
return this.buffer.length;
|
|
56
|
-
}
|
|
57
|
-
get isEmpty() {
|
|
58
|
-
return this.buffer.length === 0;
|
|
59
|
-
}
|
|
60
|
-
get isFull() {
|
|
61
|
-
return this.buffer.length >= this.capacity;
|
|
62
|
-
}
|
|
63
|
-
get isClosed() {
|
|
64
|
-
return this._isClosed;
|
|
65
|
-
}
|
|
66
|
-
get remainingCapacity() {
|
|
67
|
-
return this.capacity === Infinity ? Infinity : this.capacity - this.buffer.length;
|
|
68
|
-
}
|
|
69
|
-
// ========================================================================
|
|
70
|
-
// Blocking operations
|
|
71
|
-
// ========================================================================
|
|
72
|
-
/**
|
|
73
|
-
* Adds an element to the queue.
|
|
74
|
-
* If the queue is full, waits until space is available.
|
|
75
|
-
*
|
|
76
|
-
* @throws Error if queue is closed
|
|
77
|
-
* @throws Error if timeout expires
|
|
78
|
-
*/
|
|
79
|
-
async put(element, timeout) {
|
|
80
|
-
if (this._isClosed) {
|
|
81
|
-
throw new Error("Queue is closed");
|
|
82
|
-
}
|
|
83
|
-
// If there are waiting takers, give to them directly
|
|
84
|
-
if (this.takers.length > 0) {
|
|
85
|
-
const taker = this.takers.shift();
|
|
86
|
-
this.clearTimeout(taker);
|
|
87
|
-
taker.resolve(element);
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
// If queue has space, add immediately
|
|
91
|
-
if (this.buffer.length < this.capacity) {
|
|
92
|
-
this.buffer.push(element);
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
// Queue is full, wait for space
|
|
96
|
-
return new Promise((resolve, reject) => {
|
|
97
|
-
const putter = { resolve, reject };
|
|
98
|
-
const timeoutMs = timeout ?? this.defaultTimeout;
|
|
99
|
-
if (timeoutMs !== undefined) {
|
|
100
|
-
putter.timeoutId = setTimeout(() => {
|
|
101
|
-
const index = this.putters.indexOf(putter);
|
|
102
|
-
if (index !== -1) {
|
|
103
|
-
this.putters.splice(index, 1);
|
|
104
|
-
}
|
|
105
|
-
reject(new Error(`Put timeout after ${timeoutMs}ms`));
|
|
106
|
-
}, timeoutMs);
|
|
107
|
-
}
|
|
108
|
-
this.putters.push(putter);
|
|
109
|
-
// Immediately try to resolve if a taker arrives
|
|
110
|
-
if (this.takers.length > 0) {
|
|
111
|
-
const taker = this.takers.shift();
|
|
112
|
-
this.clearTimeout(taker);
|
|
113
|
-
this.clearTimeout(putter);
|
|
114
|
-
this.putters.pop(); // Remove putter
|
|
115
|
-
taker.resolve(element);
|
|
116
|
-
resolve();
|
|
117
|
-
}
|
|
118
|
-
else if (this.buffer.length < this.capacity) {
|
|
119
|
-
this.clearTimeout(putter);
|
|
120
|
-
this.putters.pop();
|
|
121
|
-
this.buffer.push(element);
|
|
122
|
-
resolve();
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Removes and returns an element from the queue.
|
|
128
|
-
* If the queue is empty, waits until an element is available.
|
|
129
|
-
*
|
|
130
|
-
* @throws Error if queue is closed and empty
|
|
131
|
-
* @throws Error if timeout expires
|
|
132
|
-
*/
|
|
133
|
-
async take(timeout) {
|
|
134
|
-
// If buffer has elements, return immediately
|
|
135
|
-
if (this.buffer.length > 0) {
|
|
136
|
-
const element = this.buffer.shift();
|
|
137
|
-
this.fulfillPutter();
|
|
138
|
-
return element;
|
|
139
|
-
}
|
|
140
|
-
// If closed and empty, throw
|
|
141
|
-
if (this._isClosed) {
|
|
142
|
-
throw new Error("Queue is closed and empty");
|
|
143
|
-
}
|
|
144
|
-
// Wait for an element
|
|
145
|
-
return new Promise((resolve, reject) => {
|
|
146
|
-
const taker = { resolve, reject };
|
|
147
|
-
const timeoutMs = timeout ?? this.defaultTimeout;
|
|
148
|
-
if (timeoutMs !== undefined) {
|
|
149
|
-
taker.timeoutId = setTimeout(() => {
|
|
150
|
-
const index = this.takers.indexOf(taker);
|
|
151
|
-
if (index !== -1) {
|
|
152
|
-
this.takers.splice(index, 1);
|
|
153
|
-
}
|
|
154
|
-
reject(new Error(`Take timeout after ${timeoutMs}ms`));
|
|
155
|
-
}, timeoutMs);
|
|
156
|
-
}
|
|
157
|
-
this.takers.push(taker);
|
|
158
|
-
// Check if putter can immediately fulfill
|
|
159
|
-
if (this.putters.length > 0 || this.buffer.length > 0) {
|
|
160
|
-
const t = this.takers.pop();
|
|
161
|
-
this.clearTimeout(t);
|
|
162
|
-
if (this.buffer.length > 0) {
|
|
163
|
-
const element = this.buffer.shift();
|
|
164
|
-
this.fulfillPutter();
|
|
165
|
-
resolve(element);
|
|
166
|
-
}
|
|
167
|
-
else if (this.putters.length > 0) {
|
|
168
|
-
// Direct handoff from putter
|
|
169
|
-
const putter = this.putters.shift();
|
|
170
|
-
this.clearTimeout(putter);
|
|
171
|
-
putter.resolve();
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
// ========================================================================
|
|
177
|
-
// Non-blocking operations
|
|
178
|
-
// ========================================================================
|
|
179
|
-
/**
|
|
180
|
-
* Attempts to add an element without waiting.
|
|
181
|
-
* Returns false if queue is full.
|
|
182
|
-
*/
|
|
183
|
-
tryPut(element) {
|
|
184
|
-
if (this._isClosed || this.buffer.length >= this.capacity) {
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
// If there are waiting takers, give to them directly
|
|
188
|
-
if (this.takers.length > 0) {
|
|
189
|
-
const taker = this.takers.shift();
|
|
190
|
-
this.clearTimeout(taker);
|
|
191
|
-
taker.resolve(element);
|
|
192
|
-
return true;
|
|
193
|
-
}
|
|
194
|
-
this.buffer.push(element);
|
|
195
|
-
return true;
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Attempts to remove an element without waiting.
|
|
199
|
-
* Returns undefined if queue is empty.
|
|
200
|
-
*/
|
|
201
|
-
tryTake() {
|
|
202
|
-
if (this.buffer.length === 0) {
|
|
203
|
-
return undefined;
|
|
204
|
-
}
|
|
205
|
-
const element = this.buffer.shift();
|
|
206
|
-
this.fulfillPutter();
|
|
207
|
-
return element;
|
|
208
|
-
}
|
|
209
|
-
// ========================================================================
|
|
210
|
-
// Peeking
|
|
211
|
-
// ========================================================================
|
|
212
|
-
/**
|
|
213
|
-
* Returns the next element without removing it.
|
|
214
|
-
* Waits if queue is empty.
|
|
215
|
-
*/
|
|
216
|
-
async peek() {
|
|
217
|
-
if (this.buffer.length > 0) {
|
|
218
|
-
return this.buffer[0];
|
|
219
|
-
}
|
|
220
|
-
if (this._isClosed) {
|
|
221
|
-
throw new Error("Queue is closed and empty");
|
|
222
|
-
}
|
|
223
|
-
// Wait for an element, then peek
|
|
224
|
-
const element = await this.take();
|
|
225
|
-
this.buffer.unshift(element);
|
|
226
|
-
return element;
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Returns the next element without waiting.
|
|
230
|
-
* Returns undefined if queue is empty.
|
|
231
|
-
*/
|
|
232
|
-
tryPeek() {
|
|
233
|
-
return this.buffer[0];
|
|
234
|
-
}
|
|
235
|
-
// ========================================================================
|
|
236
|
-
// Closing and draining
|
|
237
|
-
// ========================================================================
|
|
238
|
-
/**
|
|
239
|
-
* Closes the queue. No more elements can be added.
|
|
240
|
-
* Existing elements can still be consumed.
|
|
241
|
-
*/
|
|
242
|
-
close() {
|
|
243
|
-
this._isClosed = true;
|
|
244
|
-
// Reject all waiting putters
|
|
245
|
-
for (const putter of this.putters) {
|
|
246
|
-
this.clearTimeout(putter);
|
|
247
|
-
putter.reject(new Error("Queue closed"));
|
|
248
|
-
}
|
|
249
|
-
this.putters = [];
|
|
250
|
-
// Wake up takers if queue is empty
|
|
251
|
-
if (this.buffer.length === 0) {
|
|
252
|
-
for (const taker of this.takers) {
|
|
253
|
-
this.clearTimeout(taker);
|
|
254
|
-
taker.reject(new Error("Queue closed and empty"));
|
|
255
|
-
}
|
|
256
|
-
this.takers = [];
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* Drains all elements into an array.
|
|
261
|
-
* Returns immediately with current elements.
|
|
262
|
-
*/
|
|
263
|
-
drain() {
|
|
264
|
-
const elements = this.buffer.slice();
|
|
265
|
-
this.buffer = [];
|
|
266
|
-
// Fulfill all waiting putters
|
|
267
|
-
while (this.putters.length > 0) {
|
|
268
|
-
this.fulfillPutter();
|
|
269
|
-
}
|
|
270
|
-
return elements;
|
|
271
|
-
}
|
|
272
|
-
// ========================================================================
|
|
273
|
-
// Async iteration
|
|
274
|
-
// ========================================================================
|
|
275
|
-
/**
|
|
276
|
-
* Async iterator that yields elements as they become available.
|
|
277
|
-
* Stops when queue is closed and empty.
|
|
278
|
-
*/
|
|
279
|
-
async *[Symbol.asyncIterator]() {
|
|
280
|
-
while (true) {
|
|
281
|
-
if (this.buffer.length > 0) {
|
|
282
|
-
yield this.buffer.shift();
|
|
283
|
-
this.fulfillPutter();
|
|
284
|
-
}
|
|
285
|
-
else if (this._isClosed) {
|
|
286
|
-
break;
|
|
287
|
-
}
|
|
288
|
-
else {
|
|
289
|
-
try {
|
|
290
|
-
yield await this.take();
|
|
291
|
-
}
|
|
292
|
-
catch (e) {
|
|
293
|
-
// Queue was closed
|
|
294
|
-
break;
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
// ========================================================================
|
|
300
|
-
// Private helpers
|
|
301
|
-
// ========================================================================
|
|
302
|
-
fulfillPutter() {
|
|
303
|
-
if (this.putters.length > 0 && this.buffer.length < this.capacity) {
|
|
304
|
-
const putter = this.putters.shift();
|
|
305
|
-
this.clearTimeout(putter);
|
|
306
|
-
putter.resolve();
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
clearTimeout(resolver) {
|
|
310
|
-
if (resolver.timeoutId) {
|
|
311
|
-
clearTimeout(resolver.timeoutId);
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
// ========================================================================
|
|
315
|
-
// Utility methods
|
|
316
|
-
// ========================================================================
|
|
317
|
-
/**
|
|
318
|
-
* Returns statistics about the queue.
|
|
319
|
-
*/
|
|
320
|
-
getStats() {
|
|
321
|
-
return {
|
|
322
|
-
size: this.size,
|
|
323
|
-
capacity: this.capacity,
|
|
324
|
-
isEmpty: this.isEmpty,
|
|
325
|
-
isFull: this.isFull,
|
|
326
|
-
isClosed: this._isClosed,
|
|
327
|
-
waitingPutters: this.putters.length,
|
|
328
|
-
waitingTakers: this.takers.length,
|
|
329
|
-
remainingCapacity: this.remainingCapacity,
|
|
330
|
-
};
|
|
331
|
-
}
|
|
332
|
-
toString() {
|
|
333
|
-
return `AsyncQueue[size=${this.size}, capacity=${this.capacity}, closed=${this._isClosed}]`;
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
/**
|
|
337
|
-
* Factory function to create an AsyncQueue.
|
|
338
|
-
*
|
|
339
|
-
* @example
|
|
340
|
-
* const queue = asyncQueue<number>({ capacity: 100 })
|
|
341
|
-
* await queue.put(42)
|
|
342
|
-
* const value = await queue.take()
|
|
343
|
-
*/
|
|
344
|
-
export function asyncQueue(options) {
|
|
345
|
-
return new AsyncQueue(options);
|
|
346
|
-
}
|
|
347
|
-
//# sourceMappingURL=async-queue.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"async-queue.js","sourceRoot":"","sources":["../../../src/collections/async/async-queue.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AA8BH;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,UAAU;IACb,MAAM,GAAQ,EAAE,CAAC;IACjB,OAAO,GAAqB,EAAE,CAAC;IAC/B,MAAM,GAAkB,EAAE,CAAC;IAC3B,SAAS,GAAG,KAAK,CAAC;IACT,QAAQ,CAAS;IACjB,cAAc,CAAU;IAEzC,YAAY,UAA6B,EAAE;QACzC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,QAAQ,CAAC;QAC7C,IAAI,OAAO,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;YAClC,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC;QACxC,CAAC;IACH,CAAC;IAED,2EAA2E;IAC3E,iBAAiB;IACjB,2EAA2E;IAE3E,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;IAC5B,CAAC;IAED,IAAI,OAAO;QAC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sequential Processes).
|
|
3
|
-
*
|
|
4
|
-
* Provides typed, bidirectional communication channels inspired by Go.
|
|
5
|
-
* Perfect for coordinating async operations and implementing concurrent patterns.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* const ch = channel<number>()
|
|
9
|
-
*
|
|
10
|
-
* // Sender
|
|
11
|
-
* await ch.send(42)
|
|
12
|
-
*
|
|
13
|
-
* // Receiver
|
|
14
|
-
* const value = await ch.receive()
|
|
15
|
-
*
|
|
16
|
-
* // Select from multiple channels
|
|
17
|
-
* const result = await select(
|
|
18
|
-
* ch1.case((v) => console.log('ch1:', v)),
|
|
19
|
-
* ch2.case((v) => console.log('ch2:', v))
|
|
20
|
-
* )
|
|
21
|
-
*/
|
|
22
|
-
/**
|
|
23
|
-
* Channel<T> - Bidirectional communication channel.
|
|
24
|
-
*
|
|
25
|
-
* Inspired by Go channels. Provides send/receive operations
|
|
26
|
-
* that can block until the other side is ready.
|
|
27
|
-
*/
|
|
28
|
-
export declare class Channel<T> {
|
|
29
|
-
private queue;
|
|
30
|
-
private _isClosed;
|
|
31
|
-
constructor(bufferSize?: number);
|
|
32
|
-
/**
|
|
33
|
-
* Sends a value through the channel.
|
|
34
|
-
* Blocks if channel buffer is full.
|
|
35
|
-
*
|
|
36
|
-
* @throws Error if channel is closed
|
|
37
|
-
*/
|
|
38
|
-
send(value: T): Promise<void>;
|
|
39
|
-
/**
|
|
40
|
-
* Receives a value from the channel.
|
|
41
|
-
* Blocks if no value is available.
|
|
42
|
-
*
|
|
43
|
-
* @throws Error if channel is closed and empty
|
|
44
|
-
*/
|
|
45
|
-
receive(): Promise<T>;
|
|
46
|
-
/**
|
|
47
|
-
* Attempts to send without blocking.
|
|
48
|
-
* Returns false if channel is full.
|
|
49
|
-
*/
|
|
50
|
-
trySend(value: T): boolean;
|
|
51
|
-
/**
|
|
52
|
-
* Attempts to receive without blocking.
|
|
53
|
-
* Returns undefined if no value is available.
|
|
54
|
-
*/
|
|
55
|
-
tryReceive(): T | undefined;
|
|
56
|
-
/**
|
|
57
|
-
* Closes the channel. No more values can be sent.
|
|
58
|
-
* Receivers can still drain remaining values.
|
|
59
|
-
*/
|
|
60
|
-
close(): void;
|
|
61
|
-
get isClosed(): boolean;
|
|
62
|
-
get size(): number;
|
|
63
|
-
/**
|
|
64
|
-
* Creates a select case for this channel.
|
|
65
|
-
* Used with the select() function for multiplexing.
|
|
66
|
-
*/
|
|
67
|
-
case<R>(handler: (value: T) => R): SelectCase<R>;
|
|
68
|
-
/**
|
|
69
|
-
* Async iteration over channel values.
|
|
70
|
-
* Stops when channel is closed and empty.
|
|
71
|
-
*/
|
|
72
|
-
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* SelectCase represents a case in a select statement.
|
|
76
|
-
*/
|
|
77
|
-
export interface SelectCase<R> {
|
|
78
|
-
channel: Channel<any>;
|
|
79
|
-
handler: (value: any) => R;
|
|
80
|
-
tryReceive: () => any;
|
|
81
|
-
receive: () => Promise<any>;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Selects from multiple channels (like Go's select statement).
|
|
85
|
-
*
|
|
86
|
-
* Waits for the first channel to have a value available,
|
|
87
|
-
* then calls its handler and returns the result.
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
* const result = await select(
|
|
91
|
-
* ch1.case((v) => `ch1: ${v}`),
|
|
92
|
-
* ch2.case((v) => `ch2: ${v}`),
|
|
93
|
-
* timeout(1000).case(() => 'timeout')
|
|
94
|
-
* )
|
|
95
|
-
*/
|
|
96
|
-
export declare function select<R>(...cases: SelectCase<R>[]): Promise<R>;
|
|
97
|
-
/**
|
|
98
|
-
* Creates a timeout channel that sends after the specified duration.
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* const result = await select(
|
|
102
|
-
* dataChannel.case((v) => v),
|
|
103
|
-
* timeout(5000).case(() => 'timeout!')
|
|
104
|
-
* )
|
|
105
|
-
*/
|
|
106
|
-
export declare function timeout(ms: number): Channel<void>;
|
|
107
|
-
/**
|
|
108
|
-
* Creates a ticker channel that sends at regular intervals.
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* const tick = ticker(1000) // Every second
|
|
112
|
-
* for await (const t of tick) {
|
|
113
|
-
* console.log('Tick:', t)
|
|
114
|
-
* }
|
|
115
|
-
*/
|
|
116
|
-
export declare function ticker(ms: number): Channel<number> & {
|
|
117
|
-
stop: () => void;
|
|
118
|
-
};
|
|
119
|
-
/**
|
|
120
|
-
* Pipeline: pipes values from input channel through transform to output channel.
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* const input = channel<number>()
|
|
124
|
-
* const output = pipeline(input, (x) => x * 2)
|
|
125
|
-
*
|
|
126
|
-
* await input.send(5)
|
|
127
|
-
* const result = await output.receive() // 10
|
|
128
|
-
*/
|
|
129
|
-
export declare function pipeline<T, U>(input: Channel<T>, transform: (value: T) => U | Promise<U>, bufferSize?: number): Channel<U>;
|
|
130
|
-
/**
|
|
131
|
-
* Fan-out: distributes values from one input channel to multiple output channels.
|
|
132
|
-
*
|
|
133
|
-
* @example
|
|
134
|
-
* const input = channel<number>()
|
|
135
|
-
* const [out1, out2, out3] = fanOut(input, 3)
|
|
136
|
-
*
|
|
137
|
-
* // Each output gets a copy of every input value
|
|
138
|
-
*/
|
|
139
|
-
export declare function fanOut<T>(input: Channel<T>, count: number, bufferSize?: number): Channel<T>[];
|
|
140
|
-
/**
|
|
141
|
-
* Fan-in: merges multiple input channels into one output channel.
|
|
142
|
-
*
|
|
143
|
-
* @example
|
|
144
|
-
* const ch1 = channel<number>()
|
|
145
|
-
* const ch2 = channel<number>()
|
|
146
|
-
* const merged = fanIn(ch1, ch2)
|
|
147
|
-
*
|
|
148
|
-
* // merged receives values from both ch1 and ch2
|
|
149
|
-
*/
|
|
150
|
-
export declare function fanIn<T>(...inputs: Channel<T>[]): Channel<T>;
|
|
151
|
-
/**
|
|
152
|
-
* Merge: like fanIn but closes output when first input closes.
|
|
153
|
-
*/
|
|
154
|
-
export declare function merge<T>(...inputs: Channel<T>[]): Channel<T>;
|
|
155
|
-
/**
|
|
156
|
-
* Buffered channel with specified capacity.
|
|
157
|
-
*/
|
|
158
|
-
export declare function buffered<T>(capacity: number): Channel<T>;
|
|
159
|
-
/**
|
|
160
|
-
* Unbuffered channel (rendezvous point).
|
|
161
|
-
* Sends block until receiver is ready.
|
|
162
|
-
*/
|
|
163
|
-
export declare function unbuffered<T>(): Channel<T>;
|
|
164
|
-
/**
|
|
165
|
-
* Factory function to create a channel.
|
|
166
|
-
*
|
|
167
|
-
* @example
|
|
168
|
-
* const ch = channel<number>()
|
|
169
|
-
* const bufferedCh = channel<string>(10)
|
|
170
|
-
*/
|
|
171
|
-
export declare function channel<T>(bufferSize?: number): Channel<T>;
|
|
172
|
-
/**
|
|
173
|
-
* WorkerPool: distributes work across multiple workers.
|
|
174
|
-
*
|
|
175
|
-
* @example
|
|
176
|
-
* const pool = workerPool(
|
|
177
|
-
* jobChannel,
|
|
178
|
-
* async (job) => await processJob(job),
|
|
179
|
-
* { workers: 4 }
|
|
180
|
-
* )
|
|
181
|
-
*
|
|
182
|
-
* // 4 workers concurrently process jobs from jobChannel
|
|
183
|
-
*/
|
|
184
|
-
export declare function workerPool<T, R>(jobs: Channel<T>, worker: (job: T) => Promise<R>, options?: {
|
|
185
|
-
workers: number;
|
|
186
|
-
bufferSize?: number;
|
|
187
|
-
}): Channel<R>;
|
|
188
|
-
//# sourceMappingURL=channels.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"channels.d.ts","sourceRoot":"","sources":["../../../src/collections/async/channels.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAIH;;;;;GAKG;AACH,qBAAa,OAAO,CAAC,CAAC;IACpB,OAAO,CAAC,KAAK,CAAgB;IAC7B,OAAO,CAAC,SAAS,CAAS;gBAEd,UAAU,GA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