@procedures/collections 0.0.0-stage → 0.2.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.
Files changed (205) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/LICENSE +21 -0
  3. package/README.md +394 -2
  4. package/dist/.tsbuildinfo +1 -0
  5. package/dist/async/async-queue.d.ts +155 -0
  6. package/dist/async/async-queue.d.ts.map +1 -0
  7. package/dist/async/async-queue.js +342 -0
  8. package/dist/async/async-queue.js.map +1 -0
  9. package/dist/async/channels.d.ts +188 -0
  10. package/dist/async/channels.d.ts.map +1 -0
  11. package/dist/async/channels.js +314 -0
  12. package/dist/async/channels.js.map +1 -0
  13. package/dist/behaviors/bounded.d.ts +79 -0
  14. package/dist/behaviors/bounded.d.ts.map +1 -0
  15. package/dist/behaviors/bounded.js +384 -0
  16. package/dist/behaviors/bounded.js.map +1 -0
  17. package/dist/behaviors/composite.d.ts +17 -0
  18. package/dist/behaviors/composite.d.ts.map +1 -0
  19. package/dist/behaviors/composite.js +93 -0
  20. package/dist/behaviors/composite.js.map +1 -0
  21. package/dist/behaviors/evented.d.ts +154 -0
  22. package/dist/behaviors/evented.d.ts.map +1 -0
  23. package/dist/behaviors/evented.js +299 -0
  24. package/dist/behaviors/evented.js.map +1 -0
  25. package/dist/behaviors/lru.d.ts +84 -0
  26. package/dist/behaviors/lru.d.ts.map +1 -0
  27. package/dist/behaviors/lru.js +376 -0
  28. package/dist/behaviors/lru.js.map +1 -0
  29. package/dist/behaviors/readonly.d.ts +73 -0
  30. package/dist/behaviors/readonly.d.ts.map +1 -0
  31. package/dist/behaviors/readonly.js +177 -0
  32. package/dist/behaviors/readonly.js.map +1 -0
  33. package/dist/behaviors/safe.d.ts +163 -0
  34. package/dist/behaviors/safe.d.ts.map +1 -0
  35. package/dist/behaviors/safe.js +176 -0
  36. package/dist/behaviors/safe.js.map +1 -0
  37. package/dist/behaviors/synchronized.d.ts +88 -0
  38. package/dist/behaviors/synchronized.d.ts.map +1 -0
  39. package/dist/behaviors/synchronized.js +285 -0
  40. package/dist/behaviors/synchronized.js.map +1 -0
  41. package/dist/behaviors/ttl.d.ts +118 -0
  42. package/dist/behaviors/ttl.d.ts.map +1 -0
  43. package/dist/behaviors/ttl.js +354 -0
  44. package/dist/behaviors/ttl.js.map +1 -0
  45. package/dist/contracts/model.d.ts +29 -0
  46. package/dist/contracts/model.d.ts.map +1 -0
  47. package/dist/contracts/model.js +26 -0
  48. package/dist/contracts/model.js.map +1 -0
  49. package/dist/core/effects.d.ts +176 -0
  50. package/dist/core/effects.d.ts.map +1 -0
  51. package/dist/core/effects.js +180 -0
  52. package/dist/core/effects.js.map +1 -0
  53. package/dist/core/events.d.ts +192 -0
  54. package/dist/core/events.d.ts.map +1 -0
  55. package/dist/core/events.js +291 -0
  56. package/dist/core/events.js.map +1 -0
  57. package/dist/core/middleware.d.ts +231 -0
  58. package/dist/core/middleware.d.ts.map +1 -0
  59. package/dist/core/middleware.js +161 -0
  60. package/dist/core/middleware.js.map +1 -0
  61. package/dist/core/policies.d.ts +220 -0
  62. package/dist/core/policies.d.ts.map +1 -0
  63. package/dist/core/policies.js +48 -0
  64. package/dist/core/policies.js.map +1 -0
  65. package/dist/core/traits.d.ts +142 -0
  66. package/dist/core/traits.d.ts.map +1 -0
  67. package/dist/core/traits.js +8 -0
  68. package/dist/core/traits.js.map +1 -0
  69. package/dist/fx/collectors.d.ts +193 -0
  70. package/dist/fx/collectors.d.ts.map +1 -0
  71. package/dist/fx/collectors.js +375 -0
  72. package/dist/fx/collectors.js.map +1 -0
  73. package/dist/fx/iter.d.ts +166 -0
  74. package/dist/fx/iter.d.ts.map +1 -0
  75. package/dist/fx/iter.js +421 -0
  76. package/dist/fx/iter.js.map +1 -0
  77. package/dist/impl/array-deque.d.ts +112 -0
  78. package/dist/impl/array-deque.d.ts.map +1 -0
  79. package/dist/impl/array-deque.js +380 -0
  80. package/dist/impl/array-deque.js.map +1 -0
  81. package/dist/impl/array-list.d.ts +101 -0
  82. package/dist/impl/array-list.d.ts.map +1 -0
  83. package/dist/impl/array-list.js +387 -0
  84. package/dist/impl/array-list.js.map +1 -0
  85. package/dist/impl/hash-map.d.ts +149 -0
  86. package/dist/impl/hash-map.d.ts.map +1 -0
  87. package/dist/impl/hash-map.js +473 -0
  88. package/dist/impl/hash-map.js.map +1 -0
  89. package/dist/impl/hash-set.d.ts +125 -0
  90. package/dist/impl/hash-set.d.ts.map +1 -0
  91. package/dist/impl/hash-set.js +264 -0
  92. package/dist/impl/hash-set.js.map +1 -0
  93. package/dist/impl/linked-hash-map.d.ts +141 -0
  94. package/dist/impl/linked-hash-map.d.ts.map +1 -0
  95. package/dist/impl/linked-hash-map.js +442 -0
  96. package/dist/impl/linked-hash-map.js.map +1 -0
  97. package/dist/impl/linked-list.d.ts +109 -0
  98. package/dist/impl/linked-list.d.ts.map +1 -0
  99. package/dist/impl/linked-list.js +494 -0
  100. package/dist/impl/linked-list.js.map +1 -0
  101. package/dist/impl/priority-queue.d.ts +139 -0
  102. package/dist/impl/priority-queue.d.ts.map +1 -0
  103. package/dist/impl/priority-queue.js +343 -0
  104. package/dist/impl/priority-queue.js.map +1 -0
  105. package/dist/impl/tree-map.d.ts +153 -0
  106. package/dist/impl/tree-map.d.ts.map +1 -0
  107. package/dist/impl/tree-map.js +788 -0
  108. package/dist/impl/tree-map.js.map +1 -0
  109. package/dist/impl/tree-set.d.ts +112 -0
  110. package/dist/impl/tree-set.d.ts.map +1 -0
  111. package/dist/impl/tree-set.js +374 -0
  112. package/dist/impl/tree-set.js.map +1 -0
  113. package/dist/index.d.ts +106 -0
  114. package/dist/index.d.ts.map +1 -0
  115. package/dist/index.js +139 -0
  116. package/dist/index.js.map +1 -0
  117. package/dist/interfaces/collection.d.ts +119 -0
  118. package/dist/interfaces/collection.d.ts.map +1 -0
  119. package/dist/interfaces/collection.js +8 -0
  120. package/dist/interfaces/collection.js.map +1 -0
  121. package/dist/interfaces/list.d.ts +247 -0
  122. package/dist/interfaces/list.d.ts.map +1 -0
  123. package/dist/interfaces/list.js +8 -0
  124. package/dist/interfaces/list.js.map +1 -0
  125. package/dist/interfaces/map.d.ts +344 -0
  126. package/dist/interfaces/map.d.ts.map +1 -0
  127. package/dist/interfaces/map.js +8 -0
  128. package/dist/interfaces/map.js.map +1 -0
  129. package/dist/interfaces/queue.d.ts +304 -0
  130. package/dist/interfaces/queue.d.ts.map +1 -0
  131. package/dist/interfaces/queue.js +8 -0
  132. package/dist/interfaces/queue.js.map +1 -0
  133. package/dist/interfaces/set.d.ts +137 -0
  134. package/dist/interfaces/set.d.ts.map +1 -0
  135. package/dist/interfaces/set.js +8 -0
  136. package/dist/interfaces/set.js.map +1 -0
  137. package/dist/register.d.ts +8 -0
  138. package/dist/register.d.ts.map +1 -0
  139. package/dist/register.js +8 -0
  140. package/dist/register.js.map +1 -0
  141. package/dist/storage/index.d.ts +15 -0
  142. package/dist/storage/index.d.ts.map +1 -0
  143. package/dist/storage/index.js +14 -0
  144. package/dist/storage/index.js.map +1 -0
  145. package/dist/storage/interface.d.ts +141 -0
  146. package/dist/storage/interface.d.ts.map +1 -0
  147. package/dist/storage/interface.js +23 -0
  148. package/dist/storage/interface.js.map +1 -0
  149. package/dist/storage/memory.d.ts +68 -0
  150. package/dist/storage/memory.d.ts.map +1 -0
  151. package/dist/storage/memory.js +154 -0
  152. package/dist/storage/memory.js.map +1 -0
  153. package/dist/utils/defaults.d.ts +135 -0
  154. package/dist/utils/defaults.d.ts.map +1 -0
  155. package/dist/utils/defaults.js +322 -0
  156. package/dist/utils/defaults.js.map +1 -0
  157. package/dist/utils/factories.d.ts +144 -0
  158. package/dist/utils/factories.d.ts.map +1 -0
  159. package/dist/utils/factories.js +293 -0
  160. package/dist/utils/factories.js.map +1 -0
  161. package/dist/utils/helpers.d.ts +137 -0
  162. package/dist/utils/helpers.d.ts.map +1 -0
  163. package/dist/utils/helpers.js +221 -0
  164. package/dist/utils/helpers.js.map +1 -0
  165. package/package.json +68 -3
  166. package/src/async/async-queue.ts +417 -0
  167. package/src/async/channels.ts +374 -0
  168. package/src/behaviors/bounded.ts +477 -0
  169. package/src/behaviors/composite.ts +99 -0
  170. package/src/behaviors/evented.ts +464 -0
  171. package/src/behaviors/lru.ts +447 -0
  172. package/src/behaviors/readonly.ts +224 -0
  173. package/src/behaviors/safe.ts +372 -0
  174. package/src/behaviors/synchronized.ts +316 -0
  175. package/src/behaviors/ttl.ts +462 -0
  176. package/src/contracts/model.ts +44 -0
  177. package/src/core/effects.ts +323 -0
  178. package/src/core/events.ts +356 -0
  179. package/src/core/middleware.ts +415 -0
  180. package/src/core/policies.ts +316 -0
  181. package/src/core/traits.ts +164 -0
  182. package/src/fx/collectors.ts +500 -0
  183. package/src/fx/iter.ts +536 -0
  184. package/src/impl/array-deque.ts +463 -0
  185. package/src/impl/array-list.ts +490 -0
  186. package/src/impl/hash-map.ts +623 -0
  187. package/src/impl/hash-set.ts +342 -0
  188. package/src/impl/linked-hash-map.ts +572 -0
  189. package/src/impl/linked-list.ts +606 -0
  190. package/src/impl/priority-queue.ts +448 -0
  191. package/src/impl/tree-map.ts +912 -0
  192. package/src/impl/tree-set.ts +469 -0
  193. package/src/index.ts +277 -0
  194. package/src/interfaces/collection.ts +141 -0
  195. package/src/interfaces/list.ts +281 -0
  196. package/src/interfaces/map.ts +405 -0
  197. package/src/interfaces/queue.ts +353 -0
  198. package/src/interfaces/set.ts +158 -0
  199. package/src/register.ts +8 -0
  200. package/src/storage/index.ts +16 -0
  201. package/src/storage/interface.ts +179 -0
  202. package/src/storage/memory.ts +180 -0
  203. package/src/utils/defaults.ts +345 -0
  204. package/src/utils/factories.ts +357 -0
  205. package/src/utils/helpers.ts +248 -0
@@ -0,0 +1,374 @@
1
+ /**
2
+ * Go-style channels for CSP (Communicating 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
+ import { asyncQueue, type AsyncQueue } from "./async-queue.js";
24
+
25
+ /**
26
+ * Channel<T> - Bidirectional communication channel.
27
+ *
28
+ * Inspired by Go channels. Provides send/receive operations
29
+ * that can block until the other side is ready.
30
+ */
31
+ export class Channel<T> {
32
+ private queue: AsyncQueue<T>;
33
+ private _isClosed = false;
34
+
35
+ constructor(bufferSize: number = 0) {
36
+ // A buffer size of 0 is an unbuffered channel: send waits for a receiver (a rendezvous).
37
+ // (Before, `bufferSize || 1` gave it a buffer of 1: BUGS-2026-07 L17.)
38
+ this.queue = asyncQueue<T>({ capacity: bufferSize });
39
+ }
40
+
41
+ /**
42
+ * Sends a value through the channel.
43
+ * Blocks if channel buffer is full.
44
+ *
45
+ * @throws Error if channel is closed
46
+ */
47
+ async send(value: T): Promise<void> {
48
+ if (this._isClosed) {
49
+ throw new Error("Cannot send on closed channel");
50
+ }
51
+ await this.queue.put(value);
52
+ }
53
+
54
+ /**
55
+ * Receives a value from the channel.
56
+ * Blocks if no value is available.
57
+ *
58
+ * @throws Error if channel is closed and empty
59
+ */
60
+ async receive(): Promise<T> {
61
+ return await this.queue.take();
62
+ }
63
+
64
+ /**
65
+ * Attempts to send without blocking.
66
+ * Returns false if channel is full.
67
+ */
68
+ trySend(value: T): boolean {
69
+ if (this._isClosed) return false;
70
+ return this.queue.tryPut(value);
71
+ }
72
+
73
+ /**
74
+ * Attempts to receive without blocking.
75
+ * Returns undefined if no value is available.
76
+ */
77
+ tryReceive(): T | undefined {
78
+ return this.queue.tryTake();
79
+ }
80
+
81
+ /**
82
+ * Closes the channel. No more values can be sent.
83
+ * Receivers can still drain remaining values.
84
+ */
85
+ close(): void {
86
+ this._isClosed = true;
87
+ this.queue.close();
88
+ }
89
+
90
+ get isClosed(): boolean {
91
+ return this._isClosed;
92
+ }
93
+
94
+ get size(): number {
95
+ return this.queue.size;
96
+ }
97
+
98
+ /**
99
+ * Creates a select case for this channel.
100
+ * Used with the select() function for multiplexing.
101
+ */
102
+ case<R>(handler: (value: T) => R): SelectCase<R> {
103
+ return {
104
+ channel: this,
105
+ handler,
106
+ tryReceive: () => this.tryReceive(),
107
+ receive: () => this.receive(),
108
+ };
109
+ }
110
+
111
+ /**
112
+ * Async iteration over channel values.
113
+ * Stops when channel is closed and empty.
114
+ */
115
+ async *[Symbol.asyncIterator](): AsyncIterator<T> {
116
+ while (true) {
117
+ try {
118
+ yield await this.receive();
119
+ } catch (e) {
120
+ // Channel closed
121
+ break;
122
+ }
123
+ }
124
+ }
125
+ }
126
+
127
+ /**
128
+ * SelectCase represents a case in a select statement.
129
+ */
130
+ export interface SelectCase<R> {
131
+ channel: Channel<any>;
132
+ handler: (value: any) => R;
133
+ tryReceive: () => any;
134
+ receive: () => Promise<any>;
135
+ }
136
+
137
+ /**
138
+ * Selects from multiple channels (like Go's select statement).
139
+ *
140
+ * Waits for the first channel to have a value available,
141
+ * then calls its handler and returns the result.
142
+ *
143
+ * @example
144
+ * const result = await select(
145
+ * ch1.case((v) => `ch1: ${v}`),
146
+ * ch2.case((v) => `ch2: ${v}`),
147
+ * timeout(1000).case(() => 'timeout')
148
+ * )
149
+ */
150
+ export async function select<R>(...cases: SelectCase<R>[]): Promise<R> {
151
+ // First, try non-blocking receive on all channels
152
+ for (const c of cases) {
153
+ const value = c.tryReceive();
154
+ if (value !== undefined) {
155
+ return c.handler(value);
156
+ }
157
+ }
158
+
159
+ // If no immediate value, race all receive operations
160
+ const promises = cases.map((c, index) =>
161
+ c.receive().then((value) => ({ index, value }))
162
+ );
163
+
164
+ const { index, value } = await Promise.race(promises);
165
+ return cases[index]!.handler(value);
166
+ }
167
+
168
+ /**
169
+ * Creates a timeout channel that sends after the specified duration.
170
+ *
171
+ * @example
172
+ * const result = await select(
173
+ * dataChannel.case((v) => v),
174
+ * timeout(5000).case(() => 'timeout!')
175
+ * )
176
+ */
177
+ export function timeout(ms: number): Channel<void> {
178
+ const ch = new Channel<void>();
179
+ setTimeout(() => {
180
+ ch.send(undefined).catch(() => {}); // Ignore if closed
181
+ ch.close();
182
+ }, ms);
183
+ return ch;
184
+ }
185
+
186
+ /**
187
+ * Creates a ticker channel that sends at regular intervals.
188
+ *
189
+ * @example
190
+ * const tick = ticker(1000) // Every second
191
+ * for await (const t of tick) {
192
+ * console.log('Tick:', t)
193
+ * }
194
+ */
195
+ export function ticker(ms: number): Channel<number> & { stop: () => void } {
196
+ const ch = new Channel<number>(1);
197
+ let count = 0;
198
+
199
+ const intervalId = setInterval(() => {
200
+ ch.trySend(count++);
201
+ }, ms);
202
+
203
+ return Object.assign(ch, {
204
+ stop: () => {
205
+ clearInterval(intervalId);
206
+ ch.close();
207
+ },
208
+ });
209
+ }
210
+
211
+ /**
212
+ * Pipeline: pipes values from input channel through transform to output channel.
213
+ *
214
+ * @example
215
+ * const input = channel<number>()
216
+ * const output = pipeline(input, (x) => x * 2)
217
+ *
218
+ * await input.send(5)
219
+ * const result = await output.receive() // 10
220
+ */
221
+ export function pipeline<T, U>(
222
+ input: Channel<T>,
223
+ transform: (value: T) => U | Promise<U>,
224
+ bufferSize: number = 0
225
+ ): Channel<U> {
226
+ const output = new Channel<U>(bufferSize);
227
+
228
+ (async () => {
229
+ for await (const value of input) {
230
+ const transformed = await transform(value);
231
+ await output.send(transformed);
232
+ }
233
+ output.close();
234
+ })();
235
+
236
+ return output;
237
+ }
238
+
239
+ /**
240
+ * Fan-out: distributes values from one input channel to multiple output channels.
241
+ *
242
+ * @example
243
+ * const input = channel<number>()
244
+ * const [out1, out2, out3] = fanOut(input, 3)
245
+ *
246
+ * // Each output gets a copy of every input value
247
+ */
248
+ export function fanOut<T>(input: Channel<T>, count: number, bufferSize: number = 0): Channel<T>[] {
249
+ const outputs = Array.from({ length: count }, () => new Channel<T>(bufferSize));
250
+
251
+ (async () => {
252
+ for await (const value of input) {
253
+ await Promise.all(outputs.map((ch) => ch.send(value)));
254
+ }
255
+ outputs.forEach((ch) => ch.close());
256
+ })();
257
+
258
+ return outputs;
259
+ }
260
+
261
+ /**
262
+ * Fan-in: merges multiple input channels into one output channel.
263
+ *
264
+ * @example
265
+ * const ch1 = channel<number>()
266
+ * const ch2 = channel<number>()
267
+ * const merged = fanIn(ch1, ch2)
268
+ *
269
+ * // merged receives values from both ch1 and ch2
270
+ */
271
+ export function fanIn<T>(...inputs: Channel<T>[]): Channel<T> {
272
+ const output = new Channel<T>();
273
+
274
+ const workers = inputs.map(async (input) => {
275
+ for await (const value of input) {
276
+ await output.send(value);
277
+ }
278
+ });
279
+
280
+ Promise.all(workers).then(() => output.close());
281
+
282
+ return output;
283
+ }
284
+
285
+ /**
286
+ * Merge: like fanIn but closes output when first input closes.
287
+ */
288
+ export function merge<T>(...inputs: Channel<T>[]): Channel<T> {
289
+ const output = new Channel<T>();
290
+
291
+ (async () => {
292
+ const iterators = inputs.map((ch) => ch[Symbol.asyncIterator]());
293
+
294
+ while (true) {
295
+ const results = await Promise.race(
296
+ iterators.map(async (it, idx) => {
297
+ const result = await it.next();
298
+ return { idx, result };
299
+ })
300
+ );
301
+
302
+ if (results.result.done) {
303
+ output.close();
304
+ break;
305
+ }
306
+
307
+ await output.send(results.result.value);
308
+ }
309
+ })();
310
+
311
+ return output;
312
+ }
313
+
314
+ /**
315
+ * Buffered channel with specified capacity.
316
+ */
317
+ export function buffered<T>(capacity: number): Channel<T> {
318
+ return new Channel<T>(capacity);
319
+ }
320
+
321
+ /**
322
+ * Unbuffered channel (rendezvous point).
323
+ * Sends block until receiver is ready.
324
+ */
325
+ export function unbuffered<T>(): Channel<T> {
326
+ return new Channel<T>(0);
327
+ }
328
+
329
+ /**
330
+ * Factory function to create a channel.
331
+ *
332
+ * @example
333
+ * const ch = channel<number>()
334
+ * const bufferedCh = channel<string>(10)
335
+ */
336
+ export function channel<T>(bufferSize: number = 0): Channel<T> {
337
+ return new Channel<T>(bufferSize);
338
+ }
339
+
340
+ /**
341
+ * WorkerPool: distributes work across multiple workers.
342
+ *
343
+ * @example
344
+ * const pool = workerPool(
345
+ * jobChannel,
346
+ * async (job) => await processJob(job),
347
+ * { workers: 4 }
348
+ * )
349
+ *
350
+ * // 4 workers concurrently process jobs from jobChannel
351
+ */
352
+ export function workerPool<T, R>(
353
+ jobs: Channel<T>,
354
+ worker: (job: T) => Promise<R>,
355
+ options: { workers: number; bufferSize?: number } = { workers: 1 }
356
+ ): Channel<R> {
357
+ const results = new Channel<R>(options.bufferSize || 0);
358
+ const { workers: workerCount } = options;
359
+
360
+ const workers = Array.from({ length: workerCount }, async () => {
361
+ for await (const job of jobs) {
362
+ try {
363
+ const result = await worker(job);
364
+ await results.send(result);
365
+ } catch (error) {
366
+ console.error("Worker error:", error);
367
+ }
368
+ }
369
+ });
370
+
371
+ Promise.all(workers).then(() => results.close());
372
+
373
+ return results;
374
+ }