@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,353 @@
1
+ /**
2
+ * Queue and Deque interfaces that mirror Java's Queue and Deque.
3
+ *
4
+ * Queues are collections designed for holding elements prior to processing.
5
+ * Deques (double-ended queues) allow efficient insertion/removal at both ends.
6
+ */
7
+
8
+ import type { Collection, ReadonlyCollection } from "./collection.js";
9
+ import type { Compare } from "../core/traits.js";
10
+
11
+ /**
12
+ * ReadonlyQueue<T> - Readonly FIFO (First-In-First-Out) queue.
13
+ *
14
+ * Provides read-only access to queue operations.
15
+ */
16
+ export interface ReadonlyQueue<T> extends ReadonlyCollection<T> {
17
+ /**
18
+ * Retrieves, but does not remove, the head of this queue.
19
+ *
20
+ * @returns The head element
21
+ * @throws Error if queue is empty
22
+ */
23
+ peek(): T;
24
+
25
+ /**
26
+ * Returns the head element without throwing if empty.
27
+ * Returns undefined if queue is empty.
28
+ */
29
+ peekOrUndefined(): T | undefined;
30
+ }
31
+
32
+ /**
33
+ * Queue<T> - FIFO (First-In-First-Out) queue.
34
+ *
35
+ * Mirrors java.util.Queue.
36
+ * Elements are processed in the order they were added.
37
+ */
38
+ export interface Queue<T> extends ReadonlyQueue<T>, Collection<T> {
39
+ /**
40
+ * Inserts an element into the queue.
41
+ * For bounded queues with overflow='throw', throws if queue is full.
42
+ *
43
+ * @param element The element to add
44
+ * @returns true if element was added
45
+ * @throws Error if queue is full (bounded with throw policy)
46
+ */
47
+ offer(element: T): boolean;
48
+
49
+ /**
50
+ * Retrieves and removes the head of this queue.
51
+ *
52
+ * @returns The head element
53
+ * @throws Error if queue is empty
54
+ */
55
+ poll(): T;
56
+
57
+ /**
58
+ * Retrieves and removes the head of this queue, or returns undefined if empty.
59
+ */
60
+ pollOrUndefined(): T | undefined;
61
+
62
+ /**
63
+ * Alias for offer(). Adds element to end of queue.
64
+ * Included for consistency with standard queue terminology.
65
+ */
66
+ enqueue(element: T): boolean;
67
+
68
+ /**
69
+ * Alias for poll(). Removes and returns element from front of queue.
70
+ * Included for consistency with standard queue terminology.
71
+ */
72
+ dequeue(): T;
73
+ }
74
+
75
+ /**
76
+ * Deque<T> - Double-ended queue.
77
+ *
78
+ * Mirrors java.util.Deque.
79
+ * Supports insertion and removal at both ends.
80
+ * Can be used as a queue (FIFO) or stack (LIFO).
81
+ */
82
+ export interface Deque<T> extends Queue<T> {
83
+ // First element operations (head of queue)
84
+
85
+ /**
86
+ * Inserts element at the front of this deque.
87
+ *
88
+ * @param element The element to add
89
+ * @throws Error if deque is full (bounded with throw policy)
90
+ */
91
+ addFirst(element: T): void;
92
+
93
+ /**
94
+ * Inserts element at the front, returning false if full.
95
+ */
96
+ offerFirst(element: T): boolean;
97
+
98
+ /**
99
+ * Retrieves and removes the first element.
100
+ *
101
+ * @throws Error if deque is empty
102
+ */
103
+ removeFirst(): T;
104
+
105
+ /**
106
+ * Retrieves and removes the first element, or undefined if empty.
107
+ */
108
+ pollFirst(): T | undefined;
109
+
110
+ /**
111
+ * Retrieves but does not remove the first element.
112
+ *
113
+ * @throws Error if deque is empty
114
+ */
115
+ peekFirst(): T;
116
+
117
+ /**
118
+ * Retrieves but does not remove the first element, or undefined if empty.
119
+ */
120
+ peekFirstOrUndefined(): T | undefined;
121
+
122
+ // Last element operations (tail of queue)
123
+
124
+ /**
125
+ * Inserts element at the end of this deque.
126
+ *
127
+ * @param element The element to add
128
+ * @throws Error if deque is full (bounded with throw policy)
129
+ */
130
+ addLast(element: T): void;
131
+
132
+ /**
133
+ * Inserts element at the end, returning false if full.
134
+ */
135
+ offerLast(element: T): boolean;
136
+
137
+ /**
138
+ * Retrieves and removes the last element.
139
+ *
140
+ * @throws Error if deque is empty
141
+ */
142
+ removeLast(): T;
143
+
144
+ /**
145
+ * Retrieves and removes the last element, or undefined if empty.
146
+ */
147
+ pollLast(): T | undefined;
148
+
149
+ /**
150
+ * Retrieves but does not remove the last element.
151
+ *
152
+ * @throws Error if deque is empty
153
+ */
154
+ peekLast(): T;
155
+
156
+ /**
157
+ * Retrieves but does not remove the last element, or undefined if empty.
158
+ */
159
+ peekLastOrUndefined(): T | undefined;
160
+
161
+ // Stack operations (LIFO). As on a JavaScript array and on every List of this package,
162
+ // push and pop work at the end. (Java's Deque uses the front.) The top of the stack is peekLast().
163
+
164
+ /**
165
+ * Pushes element onto the stack represented by this deque.
166
+ * Equivalent to addLast().
167
+ *
168
+ * @param element The element to push
169
+ */
170
+ push(element: T): void;
171
+
172
+ /**
173
+ * Pops an element from the stack represented by this deque.
174
+ * Equivalent to removeLast().
175
+ *
176
+ * @returns The popped element
177
+ * @throws Error if deque is empty
178
+ */
179
+ pop(): T;
180
+ }
181
+
182
+ /**
183
+ * BlockingQueue<T> - Queue that supports blocking operations.
184
+ *
185
+ * Mirrors java.util.concurrent.BlockingQueue.
186
+ * Useful for producer-consumer patterns.
187
+ */
188
+ export interface BlockingQueue<T> extends Queue<T> {
189
+ /**
190
+ * Inserts element, waiting if necessary for space to become available.
191
+ *
192
+ * @param element The element to add
193
+ * @param timeoutMs Optional timeout in milliseconds
194
+ * @returns Promise that resolves to true when element is added
195
+ * @throws Error if timeout expires
196
+ */
197
+ put(element: T, timeoutMs?: number): Promise<void>;
198
+
199
+ /**
200
+ * Retrieves and removes the head, waiting if necessary until an element is available.
201
+ *
202
+ * @param timeoutMs Optional timeout in milliseconds
203
+ * @returns Promise that resolves to the head element
204
+ * @throws Error if timeout expires
205
+ */
206
+ take(timeoutMs?: number): Promise<T>;
207
+
208
+ /**
209
+ * Returns the remaining capacity.
210
+ * Returns Infinity for unbounded queues.
211
+ */
212
+ readonly remainingCapacity: number;
213
+
214
+ /**
215
+ * Removes all available elements and adds them to the given array.
216
+ * Returns the number of elements transferred.
217
+ */
218
+ drainTo(target: T[], maxElements?: number): number;
219
+ }
220
+
221
+ /**
222
+ * PriorityQueue<T> - Queue that orders elements by priority.
223
+ *
224
+ * Mirrors java.util.PriorityQueue.
225
+ * Elements are removed in priority order (not insertion order).
226
+ * Requires a comparison function to determine priority.
227
+ */
228
+ export interface PriorityQueue<T> extends Queue<T> {
229
+ /**
230
+ * The comparison function used to determine priority.
231
+ * Elements with lower compare value have higher priority.
232
+ */
233
+ readonly compare: Compare<T>;
234
+
235
+ /**
236
+ * Returns the comparator used to order elements, or undefined if natural ordering.
237
+ */
238
+ readonly comparator: Compare<T> | undefined;
239
+ }
240
+
241
+ /**
242
+ * AsyncQueue<T> - Async iterable queue.
243
+ *
244
+ * Designed for async/await patterns and streaming data.
245
+ * Implements AsyncIterable for use with for-await-of loops.
246
+ */
247
+ export interface AsyncQueue<T> extends AsyncIterable<T> {
248
+ /** Number of elements currently in queue */
249
+ readonly size: number;
250
+
251
+ /** True if queue is empty */
252
+ readonly isEmpty: boolean;
253
+
254
+ /**
255
+ * Adds element to queue.
256
+ * For bounded queues, may wait for space to become available.
257
+ */
258
+ put(element: T): Promise<void>;
259
+
260
+ /**
261
+ * Removes and returns next element.
262
+ * Waits for an element if queue is empty.
263
+ */
264
+ take(): Promise<T>;
265
+
266
+ /**
267
+ * Attempts to add element without waiting.
268
+ * Returns false if queue is full.
269
+ */
270
+ tryPut(element: T): boolean;
271
+
272
+ /**
273
+ * Attempts to remove element without waiting.
274
+ * Returns undefined if queue is empty.
275
+ */
276
+ tryTake(): T | undefined;
277
+
278
+ /**
279
+ * Returns next element without removing it.
280
+ * Waits for an element if queue is empty.
281
+ */
282
+ peek(): Promise<T>;
283
+
284
+ /**
285
+ * Closes the queue, preventing further additions.
286
+ * Allows existing elements to be consumed.
287
+ */
288
+ close(): void;
289
+
290
+ /**
291
+ * Returns true if queue is closed.
292
+ */
293
+ readonly isClosed: boolean;
294
+ }
295
+
296
+ /**
297
+ * TransferQueue<T> - Queue where producers can wait for consumers.
298
+ *
299
+ * Mirrors java.util.concurrent.TransferQueue.
300
+ * Useful for direct handoff between producer and consumer threads.
301
+ */
302
+ export interface TransferQueue<T> extends BlockingQueue<T> {
303
+ /**
304
+ * Transfers element to consumer, waiting if necessary.
305
+ * Waits for a consumer to receive the element.
306
+ *
307
+ * @param element The element to transfer
308
+ * @returns Promise that resolves when element is received
309
+ */
310
+ transfer(element: T): Promise<void>;
311
+
312
+ /**
313
+ * Attempts to transfer element to waiting consumer.
314
+ * Returns false immediately if no consumer is waiting.
315
+ *
316
+ * @param element The element to transfer
317
+ * @param timeoutMs Optional timeout in milliseconds
318
+ */
319
+ tryTransfer(element: T, timeoutMs?: number): Promise<boolean>;
320
+
321
+ /**
322
+ * Returns true if there are consumers waiting to receive elements.
323
+ */
324
+ readonly hasWaitingConsumer: boolean;
325
+
326
+ /**
327
+ * Returns an estimate of the number of consumers waiting to receive elements.
328
+ */
329
+ readonly waitingConsumerCount: number;
330
+ }
331
+
332
+ /**
333
+ * DelayQueue<T> - Queue where elements become available after a delay.
334
+ *
335
+ * Mirrors java.util.concurrent.DelayQueue.
336
+ * Elements can only be removed once their delay has expired.
337
+ */
338
+ export interface DelayQueue<T> extends BlockingQueue<T> {
339
+ /**
340
+ * Adds element with specified delay.
341
+ *
342
+ * @param element The element to add
343
+ * @param delayMs Delay in milliseconds before element becomes available (defaults to 0)
344
+ */
345
+ offer(element: T, delayMs?: number): boolean;
346
+
347
+ /**
348
+ * Returns the delay (in ms) until next element becomes available.
349
+ * Returns 0 if element is ready now.
350
+ * Returns Infinity if queue is empty.
351
+ */
352
+ readonly nextDelay: number;
353
+ }
@@ -0,0 +1,158 @@
1
+ /**
2
+ * Set interfaces that mirror Java's Set interface hierarchy.
3
+ *
4
+ * Sets are collections that contain no duplicate elements.
5
+ * They provide efficient membership testing.
6
+ */
7
+
8
+ import type { Collection, ReadonlyCollection } from "./collection.js";
9
+ import type { Compare } from "../core/traits.js";
10
+
11
+ /**
12
+ * ReadonlySet<T> - Readonly set with no duplicates.
13
+ */
14
+ export interface ReadonlySet<T> extends ReadonlyCollection<T> {
15
+ /**
16
+ * Returns true if this set contains the specified element.
17
+ */
18
+ contains(element: T): boolean;
19
+
20
+ /**
21
+ * Returns true if this set contains all elements in the specified collection.
22
+ */
23
+ containsAll(other: Iterable<T>): boolean;
24
+ }
25
+
26
+ /**
27
+ * Set<T> - Mutable set with no duplicates.
28
+ *
29
+ * Mirrors java.util.Set.
30
+ * Elements are unique based on the set's equality function.
31
+ */
32
+ export interface Set<T> extends ReadonlySet<T>, Collection<T> {
33
+ /**
34
+ * Adds the specified element to this set if it is not already present.
35
+ *
36
+ * @returns true if this set did not already contain the element
37
+ */
38
+ add(element: T): boolean;
39
+
40
+ /**
41
+ * Removes the specified element from this set if it is present.
42
+ *
43
+ * @returns true if this set contained the element
44
+ */
45
+ remove(element: T): boolean;
46
+
47
+ /**
48
+ * Adds all elements in the specified collection to this set.
49
+ * This is a union operation.
50
+ *
51
+ * @returns true if this set changed
52
+ */
53
+ addAll(other: Iterable<T>): boolean;
54
+
55
+ /**
56
+ * Removes from this set all of its elements that are contained in the specified collection.
57
+ * This is a difference operation.
58
+ *
59
+ * @returns true if this set changed
60
+ */
61
+ removeAll(other: Iterable<T>): boolean;
62
+
63
+ /**
64
+ * Retains only the elements in this set that are contained in the specified collection.
65
+ * This is an intersection operation.
66
+ *
67
+ * @returns true if this set changed
68
+ */
69
+ retainAll(other: Iterable<T>): boolean;
70
+ }
71
+
72
+ /**
73
+ * SortedSet<T> - Set that maintains elements in sorted order.
74
+ *
75
+ * Mirrors java.util.SortedSet.
76
+ */
77
+ export interface SortedSet<T> extends Set<T> {
78
+ /**
79
+ * Returns the comparator used to order elements in this set.
80
+ */
81
+ readonly comparator: Compare<T>;
82
+
83
+ /**
84
+ * Returns the first (lowest) element in this set.
85
+ *
86
+ * @throws Error if set is empty
87
+ */
88
+ first(): T;
89
+
90
+ /**
91
+ * Returns the last (highest) element in this set.
92
+ *
93
+ * @throws Error if set is empty
94
+ */
95
+ last(): T;
96
+
97
+ /**
98
+ * Returns a view of the portion of this set whose elements are strictly less than toElement.
99
+ */
100
+ headSet(toElement: T): SortedSet<T>;
101
+
102
+ /**
103
+ * Returns a view of the portion of this set whose elements are greater than or equal to fromElement.
104
+ */
105
+ tailSet(fromElement: T): SortedSet<T>;
106
+
107
+ /**
108
+ * Returns a view of the portion of this set whose elements range from fromElement to toElement.
109
+ */
110
+ subSet(fromElement: T, toElement: T): SortedSet<T>;
111
+ }
112
+
113
+ /**
114
+ * NavigableSet<T> - Extended sorted set with navigation methods.
115
+ *
116
+ * Mirrors java.util.NavigableSet.
117
+ */
118
+ export interface NavigableSet<T> extends SortedSet<T> {
119
+ /**
120
+ * Returns the greatest element less than or equal to the given element, or undefined if none.
121
+ */
122
+ floor(element: T): T | undefined;
123
+
124
+ /**
125
+ * Returns the least element greater than or equal to the given element, or undefined if none.
126
+ */
127
+ ceiling(element: T): T | undefined;
128
+
129
+ /**
130
+ * Returns the greatest element strictly less than the given element, or undefined if none.
131
+ */
132
+ lower(element: T): T | undefined;
133
+
134
+ /**
135
+ * Returns the least element strictly greater than the given element, or undefined if none.
136
+ */
137
+ higher(element: T): T | undefined;
138
+
139
+ /**
140
+ * Retrieves and removes the first (lowest) element, or returns undefined if empty.
141
+ */
142
+ pollFirst(): T | undefined;
143
+
144
+ /**
145
+ * Retrieves and removes the last (highest) element, or returns undefined if empty.
146
+ */
147
+ pollLast(): T | undefined;
148
+
149
+ /**
150
+ * Returns a reverse order view of this set.
151
+ */
152
+ descendingSet(): NavigableSet<T>;
153
+
154
+ /**
155
+ * Returns a reverse order iterator over the elements in this set.
156
+ */
157
+ descendingIterator(): Iterator<T>;
158
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Registration Entry Point for client-collections
3
+ *
4
+ * This package provides collection interfaces and storage abstractions.
5
+ * No procedures to register - just re-exports the main module.
6
+ */
7
+
8
+ export * from "./index.js";
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Collection Storage Backends
3
+ *
4
+ * Abstract storage layer enabling collections to be backed by:
5
+ * - In-memory (fast, volatile)
6
+ *
7
+ * The backends that need an RPC Client (ApiStorage: remote API, HybridStorage: local
8
+ * cache + remote sync) live in @procedures/client and are exported from
9
+ * "@procedures/client/collections". Keeping them there means this package
10
+ * does not depend on the client package.
11
+ */
12
+
13
+ export type { CollectionStorage, StorageMetadata } from "./interface.js";
14
+ export { normalizeStorageResult } from "./interface.js";
15
+
16
+ export { InMemoryStorage } from "./memory.js";
@@ -0,0 +1,179 @@
1
+ /**
2
+ * Collection Storage Interface
3
+ *
4
+ * Abstract storage layer for collections, enabling different backends:
5
+ * - InMemoryStorage: Fast, volatile storage using Map
6
+ * - ApiStorage: Remote persistence via universal client
7
+ * - HybridStorage: Local cache + remote sync with conflict resolution
8
+ * - SyncedStorage: Periodic background sync with offline support
9
+ *
10
+ * IMPORTANT: All methods return Promise to ensure client code is
11
+ * transport-agnostic (cannot distinguish local from remote storage).
12
+ */
13
+
14
+ /**
15
+ * Storage backend interface for collections.
16
+ *
17
+ * All methods return Promise<T> to ensure consistent async behavior.
18
+ * This guarantees client code remains agnostic to the storage backend,
19
+ * enabling seamless switching between local and remote storage.
20
+ */
21
+ export interface CollectionStorage<T> {
22
+ //
23
+ // ═══ Read Operations ═══
24
+ //
25
+
26
+ /**
27
+ * Get item by ID.
28
+ *
29
+ * @param id - Item identifier
30
+ * @returns Promise resolving to item or undefined if not found
31
+ */
32
+ get(id: string): Promise<T | undefined>;
33
+
34
+ /**
35
+ * Get all items in collection.
36
+ *
37
+ * @returns Promise resolving to array of all items
38
+ */
39
+ getAll(): Promise<T[]>;
40
+
41
+ /**
42
+ * Find items matching predicate.
43
+ *
44
+ * @param predicate - Filter function
45
+ * @returns Promise resolving to array of matching items
46
+ */
47
+ find(predicate: (item: T) => boolean): Promise<T[]>;
48
+
49
+ /**
50
+ * Check if item exists.
51
+ *
52
+ * @param id - Item identifier
53
+ * @returns Promise resolving to true if item exists
54
+ */
55
+ has(id: string): Promise<boolean>;
56
+
57
+ /**
58
+ * Get number of items in collection.
59
+ *
60
+ * @returns Promise resolving to count of items
61
+ */
62
+ size(): Promise<number>;
63
+
64
+ //
65
+ // ═══ Write Operations ═══
66
+ //
67
+
68
+ /**
69
+ * Set/update item by ID.
70
+ *
71
+ * @param id - Item identifier
72
+ * @param value - Item to store
73
+ * @returns Promise resolving when operation completes
74
+ */
75
+ set(id: string, value: T): Promise<void>;
76
+
77
+ /**
78
+ * Delete item by ID.
79
+ *
80
+ * @param id - Item identifier
81
+ * @returns Promise resolving to true if item was deleted, false if not found
82
+ */
83
+ delete(id: string): Promise<boolean>;
84
+
85
+ /**
86
+ * Clear all items from collection.
87
+ *
88
+ * @returns Promise resolving when operation completes
89
+ */
90
+ clear(): Promise<void>;
91
+
92
+ //
93
+ // ═══ Bulk Operations ═══
94
+ //
95
+
96
+ /**
97
+ * Set multiple items at once (more efficient than individual sets).
98
+ *
99
+ * @param items - Array of [id, value] tuples
100
+ * @returns Promise resolving when operation completes
101
+ */
102
+ setBatch(items: Array<[string, T]>): Promise<void>;
103
+
104
+ /**
105
+ * Delete multiple items at once.
106
+ *
107
+ * @param ids - Array of item identifiers
108
+ * @returns Promise resolving to number of items actually deleted
109
+ */
110
+ deleteBatch(ids: string[]): Promise<number>;
111
+
112
+ /**
113
+ * Get multiple items by ID at once.
114
+ *
115
+ * @param ids - Array of item identifiers
116
+ * @returns Promise resolving to Map of found items (missing IDs are omitted)
117
+ */
118
+ getBatch(ids: string[]): Promise<Map<string, T>>;
119
+
120
+ //
121
+ // ═══ Lifecycle & Metadata ═══
122
+ //
123
+
124
+ /**
125
+ * Close storage and cleanup resources.
126
+ * Should be called when collection is no longer needed.
127
+ *
128
+ * @returns Promise resolving when cleanup completes
129
+ */
130
+ close(): Promise<void>;
131
+
132
+ /**
133
+ * Get storage metadata/stats (optional).
134
+ * Useful for monitoring and debugging.
135
+ *
136
+ * @returns Promise resolving to storage-specific metadata
137
+ */
138
+ getMetadata?(): Promise<StorageMetadata>;
139
+ }
140
+
141
+ /**
142
+ * Optional storage metadata for monitoring.
143
+ */
144
+ export interface StorageMetadata {
145
+ /** Storage backend type */
146
+ type: "memory" | "api" | "hybrid" | "synced" | "custom";
147
+
148
+ /** Number of items */
149
+ size: number;
150
+
151
+ /** Storage-specific stats */
152
+ stats?: {
153
+ /** Cache hit rate (for hybrid/synced storage) */
154
+ hitRate?: number;
155
+
156
+ /** Last sync timestamp (for synced storage) */
157
+ lastSync?: number;
158
+
159
+ /** Pending sync operations (for synced storage) */
160
+ pendingOps?: number;
161
+
162
+ /** Memory usage in bytes (for memory storage) */
163
+ memoryUsage?: number;
164
+
165
+ /** Custom stats */
166
+ [key: string]: unknown;
167
+ };
168
+ }
169
+
170
+ /**
171
+ * @deprecated All storage methods now return Promise.
172
+ * This helper is kept for backward compatibility but is no longer needed.
173
+ *
174
+ * @param value - Promise value
175
+ * @returns The same Promise
176
+ */
177
+ export function normalizeStorageResult<T>(value: Promise<T>): Promise<T> {
178
+ return value;
179
+ }