@procedures/collections 0.47.1 → 0.48.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 (238) hide show
  1. package/CHANGELOG.md +32 -8
  2. package/README.md +3 -1
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/index.d.ts +1 -59
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +3 -95
  7. package/dist/index.js.map +1 -1
  8. package/dist/storage/index.d.ts +2 -2
  9. package/dist/storage/index.d.ts.map +1 -1
  10. package/dist/storage/index.js +1 -1
  11. package/dist/storage/index.js.map +1 -1
  12. package/dist/storage/memory.d.ts +1 -1
  13. package/dist/storage/memory.d.ts.map +1 -1
  14. package/dist/storage/memory.js +1 -1
  15. package/dist/storage/memory.js.map +1 -1
  16. package/dist/testing/index.d.ts +1 -1
  17. package/dist/testing/index.d.ts.map +1 -1
  18. package/dist/testing/index.js +1 -1
  19. package/dist/testing/index.js.map +1 -1
  20. package/dist/testing/storage-contract.js +1 -1
  21. package/dist/testing/storage-contract.js.map +1 -1
  22. package/dist/undo/index.d.ts +1 -1
  23. package/dist/undo/index.js +1 -1
  24. package/dist/undo/undo-log.d.ts +1 -1
  25. package/dist/undo/undo-log.js +1 -1
  26. package/package.json +5 -5
  27. package/src/index.ts +338 -214
  28. package/src/storage/index.ts +2 -2
  29. package/src/storage/memory.ts +1 -1
  30. package/src/testing/index.ts +1 -1
  31. package/src/testing/storage-contract.ts +1 -1
  32. package/src/undo/index.ts +1 -1
  33. package/src/undo/undo-log.ts +1 -1
  34. package/dist/async/async-queue.d.ts +0 -170
  35. package/dist/async/async-queue.d.ts.map +0 -1
  36. package/dist/async/async-queue.js +0 -404
  37. package/dist/async/async-queue.js.map +0 -1
  38. package/dist/async/channels.d.ts +0 -211
  39. package/dist/async/channels.d.ts.map +0 -1
  40. package/dist/async/channels.js +0 -393
  41. package/dist/async/channels.js.map +0 -1
  42. package/dist/async/settled.d.ts +0 -23
  43. package/dist/async/settled.d.ts.map +0 -1
  44. package/dist/async/settled.js +0 -32
  45. package/dist/async/settled.js.map +0 -1
  46. package/dist/behaviors/bounded.d.ts +0 -79
  47. package/dist/behaviors/bounded.d.ts.map +0 -1
  48. package/dist/behaviors/bounded.js +0 -393
  49. package/dist/behaviors/bounded.js.map +0 -1
  50. package/dist/behaviors/composite.d.ts +0 -17
  51. package/dist/behaviors/composite.d.ts.map +0 -1
  52. package/dist/behaviors/composite.js +0 -93
  53. package/dist/behaviors/composite.js.map +0 -1
  54. package/dist/behaviors/evented.d.ts +0 -154
  55. package/dist/behaviors/evented.d.ts.map +0 -1
  56. package/dist/behaviors/evented.js +0 -299
  57. package/dist/behaviors/evented.js.map +0 -1
  58. package/dist/behaviors/lru.d.ts +0 -84
  59. package/dist/behaviors/lru.d.ts.map +0 -1
  60. package/dist/behaviors/lru.js +0 -376
  61. package/dist/behaviors/lru.js.map +0 -1
  62. package/dist/behaviors/readonly.d.ts +0 -73
  63. package/dist/behaviors/readonly.d.ts.map +0 -1
  64. package/dist/behaviors/readonly.js +0 -189
  65. package/dist/behaviors/readonly.js.map +0 -1
  66. package/dist/behaviors/safe.d.ts +0 -163
  67. package/dist/behaviors/safe.d.ts.map +0 -1
  68. package/dist/behaviors/safe.js +0 -176
  69. package/dist/behaviors/safe.js.map +0 -1
  70. package/dist/behaviors/synchronized.d.ts +0 -88
  71. package/dist/behaviors/synchronized.d.ts.map +0 -1
  72. package/dist/behaviors/synchronized.js +0 -285
  73. package/dist/behaviors/synchronized.js.map +0 -1
  74. package/dist/behaviors/ttl.d.ts +0 -118
  75. package/dist/behaviors/ttl.d.ts.map +0 -1
  76. package/dist/behaviors/ttl.js +0 -354
  77. package/dist/behaviors/ttl.js.map +0 -1
  78. package/dist/contracts/model.d.ts +0 -29
  79. package/dist/contracts/model.d.ts.map +0 -1
  80. package/dist/contracts/model.js +0 -26
  81. package/dist/contracts/model.js.map +0 -1
  82. package/dist/core/effects.d.ts +0 -176
  83. package/dist/core/effects.d.ts.map +0 -1
  84. package/dist/core/effects.js +0 -180
  85. package/dist/core/effects.js.map +0 -1
  86. package/dist/core/events.d.ts +0 -192
  87. package/dist/core/events.d.ts.map +0 -1
  88. package/dist/core/events.js +0 -291
  89. package/dist/core/events.js.map +0 -1
  90. package/dist/core/middleware.d.ts +0 -231
  91. package/dist/core/middleware.d.ts.map +0 -1
  92. package/dist/core/middleware.js +0 -161
  93. package/dist/core/middleware.js.map +0 -1
  94. package/dist/core/policies.d.ts +0 -220
  95. package/dist/core/policies.d.ts.map +0 -1
  96. package/dist/core/policies.js +0 -48
  97. package/dist/core/policies.js.map +0 -1
  98. package/dist/core/traits.d.ts +0 -142
  99. package/dist/core/traits.d.ts.map +0 -1
  100. package/dist/core/traits.js +0 -8
  101. package/dist/core/traits.js.map +0 -1
  102. package/dist/fx/collectors.d.ts +0 -193
  103. package/dist/fx/collectors.d.ts.map +0 -1
  104. package/dist/fx/collectors.js +0 -375
  105. package/dist/fx/collectors.js.map +0 -1
  106. package/dist/fx/iter.d.ts +0 -166
  107. package/dist/fx/iter.d.ts.map +0 -1
  108. package/dist/fx/iter.js +0 -421
  109. package/dist/fx/iter.js.map +0 -1
  110. package/dist/impl/array-deque.d.ts +0 -112
  111. package/dist/impl/array-deque.d.ts.map +0 -1
  112. package/dist/impl/array-deque.js +0 -380
  113. package/dist/impl/array-deque.js.map +0 -1
  114. package/dist/impl/array-list.d.ts +0 -101
  115. package/dist/impl/array-list.d.ts.map +0 -1
  116. package/dist/impl/array-list.js +0 -387
  117. package/dist/impl/array-list.js.map +0 -1
  118. package/dist/impl/hash-map.d.ts +0 -149
  119. package/dist/impl/hash-map.d.ts.map +0 -1
  120. package/dist/impl/hash-map.js +0 -473
  121. package/dist/impl/hash-map.js.map +0 -1
  122. package/dist/impl/hash-set.d.ts +0 -125
  123. package/dist/impl/hash-set.d.ts.map +0 -1
  124. package/dist/impl/hash-set.js +0 -264
  125. package/dist/impl/hash-set.js.map +0 -1
  126. package/dist/impl/linked-hash-map.d.ts +0 -141
  127. package/dist/impl/linked-hash-map.d.ts.map +0 -1
  128. package/dist/impl/linked-hash-map.js +0 -442
  129. package/dist/impl/linked-hash-map.js.map +0 -1
  130. package/dist/impl/linked-list.d.ts +0 -109
  131. package/dist/impl/linked-list.d.ts.map +0 -1
  132. package/dist/impl/linked-list.js +0 -494
  133. package/dist/impl/linked-list.js.map +0 -1
  134. package/dist/impl/priority-queue.d.ts +0 -139
  135. package/dist/impl/priority-queue.d.ts.map +0 -1
  136. package/dist/impl/priority-queue.js +0 -343
  137. package/dist/impl/priority-queue.js.map +0 -1
  138. package/dist/impl/summary-tree.d.ts +0 -146
  139. package/dist/impl/summary-tree.d.ts.map +0 -1
  140. package/dist/impl/summary-tree.js +0 -524
  141. package/dist/impl/summary-tree.js.map +0 -1
  142. package/dist/impl/tree-map.d.ts +0 -153
  143. package/dist/impl/tree-map.d.ts.map +0 -1
  144. package/dist/impl/tree-map.js +0 -788
  145. package/dist/impl/tree-map.js.map +0 -1
  146. package/dist/impl/tree-set.d.ts +0 -112
  147. package/dist/impl/tree-set.d.ts.map +0 -1
  148. package/dist/impl/tree-set.js +0 -374
  149. package/dist/impl/tree-set.js.map +0 -1
  150. package/dist/interfaces/collection.d.ts +0 -119
  151. package/dist/interfaces/collection.d.ts.map +0 -1
  152. package/dist/interfaces/collection.js +0 -8
  153. package/dist/interfaces/collection.js.map +0 -1
  154. package/dist/interfaces/list.d.ts +0 -247
  155. package/dist/interfaces/list.d.ts.map +0 -1
  156. package/dist/interfaces/list.js +0 -8
  157. package/dist/interfaces/list.js.map +0 -1
  158. package/dist/interfaces/map.d.ts +0 -344
  159. package/dist/interfaces/map.d.ts.map +0 -1
  160. package/dist/interfaces/map.js +0 -8
  161. package/dist/interfaces/map.js.map +0 -1
  162. package/dist/interfaces/queue.d.ts +0 -304
  163. package/dist/interfaces/queue.d.ts.map +0 -1
  164. package/dist/interfaces/queue.js +0 -8
  165. package/dist/interfaces/queue.js.map +0 -1
  166. package/dist/interfaces/set.d.ts +0 -137
  167. package/dist/interfaces/set.d.ts.map +0 -1
  168. package/dist/interfaces/set.js +0 -8
  169. package/dist/interfaces/set.js.map +0 -1
  170. package/dist/order/index.d.ts +0 -7
  171. package/dist/order/index.d.ts.map +0 -1
  172. package/dist/order/index.js +0 -7
  173. package/dist/order/index.js.map +0 -1
  174. package/dist/order/rank.d.ts +0 -100
  175. package/dist/order/rank.d.ts.map +0 -1
  176. package/dist/order/rank.js +0 -286
  177. package/dist/order/rank.js.map +0 -1
  178. package/dist/storage/listeners.d.ts +0 -39
  179. package/dist/storage/listeners.d.ts.map +0 -1
  180. package/dist/storage/listeners.js +0 -65
  181. package/dist/storage/listeners.js.map +0 -1
  182. package/dist/testing/check.d.ts +0 -25
  183. package/dist/testing/check.d.ts.map +0 -1
  184. package/dist/testing/check.js +0 -85
  185. package/dist/testing/check.js.map +0 -1
  186. package/dist/utils/defaults.d.ts +0 -135
  187. package/dist/utils/defaults.d.ts.map +0 -1
  188. package/dist/utils/defaults.js +0 -322
  189. package/dist/utils/defaults.js.map +0 -1
  190. package/dist/utils/factories.d.ts +0 -144
  191. package/dist/utils/factories.d.ts.map +0 -1
  192. package/dist/utils/factories.js +0 -293
  193. package/dist/utils/factories.js.map +0 -1
  194. package/dist/utils/helpers.d.ts +0 -137
  195. package/dist/utils/helpers.d.ts.map +0 -1
  196. package/dist/utils/helpers.js +0 -221
  197. package/dist/utils/helpers.js.map +0 -1
  198. package/src/async/async-queue.ts +0 -481
  199. package/src/async/channels.ts +0 -450
  200. package/src/async/settled.ts +0 -41
  201. package/src/behaviors/bounded.ts +0 -485
  202. package/src/behaviors/composite.ts +0 -99
  203. package/src/behaviors/evented.ts +0 -464
  204. package/src/behaviors/lru.ts +0 -447
  205. package/src/behaviors/readonly.ts +0 -236
  206. package/src/behaviors/safe.ts +0 -372
  207. package/src/behaviors/synchronized.ts +0 -316
  208. package/src/behaviors/ttl.ts +0 -462
  209. package/src/contracts/model.ts +0 -44
  210. package/src/core/effects.ts +0 -323
  211. package/src/core/events.ts +0 -356
  212. package/src/core/middleware.ts +0 -415
  213. package/src/core/policies.ts +0 -316
  214. package/src/core/traits.ts +0 -164
  215. package/src/fx/collectors.ts +0 -500
  216. package/src/fx/iter.ts +0 -536
  217. package/src/impl/array-deque.ts +0 -463
  218. package/src/impl/array-list.ts +0 -490
  219. package/src/impl/hash-map.ts +0 -623
  220. package/src/impl/hash-set.ts +0 -342
  221. package/src/impl/linked-hash-map.ts +0 -572
  222. package/src/impl/linked-list.ts +0 -606
  223. package/src/impl/priority-queue.ts +0 -448
  224. package/src/impl/summary-tree.ts +0 -586
  225. package/src/impl/tree-map.ts +0 -912
  226. package/src/impl/tree-set.ts +0 -469
  227. package/src/interfaces/collection.ts +0 -141
  228. package/src/interfaces/list.ts +0 -281
  229. package/src/interfaces/map.ts +0 -405
  230. package/src/interfaces/queue.ts +0 -353
  231. package/src/interfaces/set.ts +0 -158
  232. package/src/order/index.ts +0 -6
  233. package/src/order/rank.ts +0 -271
  234. package/src/storage/listeners.ts +0 -72
  235. package/src/testing/check.ts +0 -81
  236. package/src/utils/defaults.ts +0 -345
  237. package/src/utils/factories.ts +0 -357
  238. package/src/utils/helpers.ts +0 -248
@@ -1,485 +0,0 @@
1
- /**
2
- * Bounded behavior - Enforces capacity limits on collections.
3
- *
4
- * Wraps collections to prevent them from exceeding a maximum size.
5
- * Provides various overflow policies to handle full collections.
6
- *
7
- * @example
8
- * const list = compose(
9
- * boundedList({ capacity: 100, policy: 'drop-oldest' })
10
- * )(arrayList<number>())
11
- *
12
- * // Add 101 elements - first element will be dropped
13
- * for (let i = 0; i < 101; i++) {
14
- * list.add(i)
15
- * }
16
- */
17
-
18
- import type { Middleware } from "../core/middleware.js";
19
- import type { List } from "../interfaces/list.js";
20
- import type { Queue, Deque } from "../interfaces/queue.js";
21
- import type { MapLike } from "../interfaces/map.js";
22
- import type { Collection } from "../interfaces/collection.js";
23
- import type {
24
- OverflowPolicy,
25
- OverflowHandler,
26
- OverflowContext,
27
- } from "../core/policies.js";
28
-
29
- /**
30
- * Options for bounded behavior.
31
- */
32
- export interface BoundedOptions<T> {
33
- /**
34
- * Maximum number of elements the collection can hold.
35
- */
36
- capacity: number;
37
-
38
- /**
39
- * Policy for handling overflow when collection is full.
40
- * @default 'throw'
41
- */
42
- policy?: OverflowPolicy;
43
-
44
- /**
45
- * Optional callback invoked when overflow occurs.
46
- * Called before the policy is applied.
47
- */
48
- onOverflow?: OverflowHandler<T>;
49
- }
50
-
51
- /**
52
- * Marker interface for bounded collections.
53
- */
54
- export interface BoundedCollection {
55
- readonly capacity: number;
56
- readonly isFull: boolean;
57
- readonly remainingCapacity: number;
58
- }
59
-
60
- /**
61
- * Creates a bounded List middleware.
62
- *
63
- * Intercepts add operations to enforce capacity limits.
64
- * Supports multiple overflow policies.
65
- */
66
- export function boundedList<T>(
67
- options: BoundedOptions<T>
68
- ): Middleware<List<T>, List<T> & BoundedCollection> {
69
- const { capacity, policy = "throw", onOverflow } = options;
70
-
71
- return (next: List<T>): List<T> & BoundedCollection => {
72
- // An unshift puts the element at the front, so drop-oldest drops the last element for it
73
- const handleOverflow = (element: T, dropFrom: "first" | "last" = "first"): void => {
74
- // Call overflow handler if provided
75
- if (onOverflow) {
76
- const context: OverflowContext<T> = {
77
- newElement: element,
78
- currentSize: next.size,
79
- capacity,
80
- timestamp: Date.now(),
81
- };
82
- onOverflow(context);
83
- }
84
-
85
- // Apply policy
86
- switch (policy) {
87
- case "throw":
88
- throw new Error(`List is full (capacity: ${capacity})`);
89
-
90
- case "drop-oldest":
91
- // Remove one element: the first, or the last for an unshift
92
- if (!next.isEmpty) {
93
- if (dropFrom === "last") next.pop();
94
- else next.shift();
95
- }
96
- break;
97
-
98
- case "drop-newest":
99
- // Don't add the new element (do nothing)
100
- return;
101
-
102
- case "reject":
103
- // Silently reject (return false for add operations)
104
- return;
105
-
106
- case "grow":
107
- // Allow growth (do nothing, element will be added)
108
- return;
109
-
110
- case "block":
111
- throw new Error("Block policy not supported for sync collections");
112
-
113
- default:
114
- throw new Error(`Unknown overflow policy: ${policy}`);
115
- }
116
- };
117
-
118
- // After drop-oldest removed the first element, an index of the caller is one place lower
119
- const shifted = (index: number): number => (policy === "drop-oldest" ? Math.max(0, index - 1) : index);
120
-
121
- return new Proxy(next, {
122
- get(target, prop, receiver) {
123
- // Capacity property
124
- if (prop === "capacity") {
125
- return capacity;
126
- }
127
-
128
- // isFull property
129
- if (prop === "isFull") {
130
- return target.size >= capacity;
131
- }
132
-
133
- // remainingCapacity property
134
- if (prop === "remainingCapacity") {
135
- return Math.max(0, capacity - target.size);
136
- }
137
-
138
- const value = Reflect.get(target, prop, receiver);
139
-
140
- // Intercept methods that add elements
141
- if (typeof value === "function") {
142
- switch (prop) {
143
- case "add":
144
- return function (element: T): boolean {
145
- if (target.size >= capacity && policy !== "grow") {
146
- handleOverflow(element);
147
- if (policy === "reject" || policy === "drop-newest") {
148
- return false;
149
- }
150
- }
151
- return (value as Function).call(target, element);
152
- };
153
-
154
- case "push":
155
- return function (element: T): void {
156
- if (target.size >= capacity && policy !== "grow") {
157
- handleOverflow(element);
158
- if (policy === "reject" || policy === "drop-newest") {
159
- return;
160
- }
161
- }
162
- (value as Function).call(target, element);
163
- };
164
-
165
- case "unshift":
166
- return function (element: T): void {
167
- if (target.size >= capacity && policy !== "grow") {
168
- // One element goes: the last one (before, the first and the last went: audit pass 2)
169
- handleOverflow(element, "last");
170
- if (policy === "reject" || policy === "drop-newest") {
171
- return;
172
- }
173
- }
174
- (value as Function).call(target, element);
175
- };
176
-
177
- case "insert":
178
- return function (index: number, element: T): void {
179
- let at = index;
180
- if (target.size >= capacity && policy !== "grow") {
181
- handleOverflow(element);
182
- if (policy === "reject" || policy === "drop-newest") {
183
- return;
184
- }
185
- at = shifted(at);
186
- }
187
- (value as Function).call(target, at, element);
188
- };
189
-
190
- case "addAll":
191
- case "insertAll":
192
- return function (...args: any[]): boolean {
193
- const elements = args[args.length - 1] as Iterable<T>;
194
- const elementsArray = Array.from(elements);
195
-
196
- // Check if adding all elements would exceed capacity
197
- if (target.size + elementsArray.length > capacity && policy !== "grow") {
198
- // Add elements one by one with overflow handling. An insert keeps the order of
199
- // the elements, at an index that follows each drop.
200
- let added = false;
201
- let at = args[0] as number;
202
- for (const element of elementsArray) {
203
- if (target.size >= capacity) {
204
- handleOverflow(element);
205
- if (policy === "reject" || policy === "drop-newest") {
206
- break;
207
- }
208
- at = shifted(at);
209
- }
210
- if (prop === "addAll") {
211
- target.add(element);
212
- } else {
213
- target.insert(at, element);
214
- at++;
215
- }
216
- added = true;
217
- }
218
- return added;
219
- }
220
-
221
- return (value as Function).apply(target, args);
222
- };
223
- }
224
- }
225
-
226
- return value;
227
- },
228
- }) as List<T> & BoundedCollection;
229
- };
230
- }
231
-
232
- /**
233
- * Creates a bounded Queue middleware.
234
- */
235
- export function boundedQueue<T>(
236
- options: BoundedOptions<T>
237
- ): Middleware<Queue<T>, Queue<T> & BoundedCollection> {
238
- const { capacity, policy = "throw", onOverflow } = options;
239
-
240
- return (next: Queue<T>): Queue<T> & BoundedCollection => {
241
- const handleOverflow = (element: T): void => {
242
- if (onOverflow) {
243
- const context: OverflowContext<T> = {
244
- newElement: element,
245
- currentSize: next.size,
246
- capacity,
247
- timestamp: Date.now(),
248
- };
249
- onOverflow(context);
250
- }
251
-
252
- switch (policy) {
253
- case "throw":
254
- throw new Error(`Queue is full (capacity: ${capacity})`);
255
- case "drop-oldest":
256
- if (!next.isEmpty) {
257
- next.poll();
258
- }
259
- break;
260
- case "drop-newest":
261
- case "reject":
262
- return;
263
- case "grow":
264
- return;
265
- case "block":
266
- throw new Error("Block policy not supported for sync collections");
267
- default:
268
- throw new Error(`Unknown overflow policy: ${policy}`);
269
- }
270
- };
271
-
272
- return new Proxy(next, {
273
- get(target, prop, receiver) {
274
- if (prop === "capacity") return capacity;
275
- if (prop === "isFull") return target.size >= capacity;
276
- if (prop === "remainingCapacity") return Math.max(0, capacity - target.size);
277
-
278
- const value = Reflect.get(target, prop, receiver);
279
-
280
- if (typeof value === "function") {
281
- switch (prop) {
282
- case "offer":
283
- case "enqueue":
284
- case "add":
285
- return function (element: T): boolean {
286
- if (target.size >= capacity && policy !== "grow") {
287
- handleOverflow(element);
288
- if (policy === "reject" || policy === "drop-newest") {
289
- return false;
290
- }
291
- }
292
- return (value as Function).call(target, element);
293
- };
294
- }
295
- }
296
-
297
- return value;
298
- },
299
- }) as Queue<T> & BoundedCollection;
300
- };
301
- }
302
-
303
- /**
304
- * Creates a bounded Deque middleware.
305
- */
306
- export function boundedDeque<T>(
307
- options: BoundedOptions<T>
308
- ): Middleware<Deque<T>, Deque<T> & BoundedCollection> {
309
- const { capacity, policy = "throw", onOverflow } = options;
310
-
311
- return (next: Deque<T>): Deque<T> & BoundedCollection => {
312
- const handleOverflow = (element: T): void => {
313
- if (onOverflow) {
314
- const context: OverflowContext<T> = {
315
- newElement: element,
316
- currentSize: next.size,
317
- capacity,
318
- timestamp: Date.now(),
319
- };
320
- onOverflow(context);
321
- }
322
-
323
- switch (policy) {
324
- case "throw":
325
- throw new Error(`Deque is full (capacity: ${capacity})`);
326
- case "drop-oldest":
327
- if (!next.isEmpty) {
328
- next.removeFirst();
329
- }
330
- break;
331
- case "drop-newest":
332
- case "reject":
333
- return;
334
- case "grow":
335
- return;
336
- case "block":
337
- throw new Error("Block policy not supported for sync collections");
338
- default:
339
- throw new Error(`Unknown overflow policy: ${policy}`);
340
- }
341
- };
342
-
343
- return new Proxy(next, {
344
- get(target, prop, receiver) {
345
- if (prop === "capacity") return capacity;
346
- if (prop === "isFull") return target.size >= capacity;
347
- if (prop === "remainingCapacity") return Math.max(0, capacity - target.size);
348
-
349
- const value = Reflect.get(target, prop, receiver);
350
-
351
- if (typeof value === "function") {
352
- switch (prop) {
353
- case "addFirst":
354
- case "addLast":
355
- case "offerFirst":
356
- case "offerLast":
357
- case "push":
358
- case "offer":
359
- case "add":
360
- return function (element: T): any {
361
- if (target.size >= capacity && policy !== "grow") {
362
- handleOverflow(element);
363
- if (policy === "reject" || policy === "drop-newest") {
364
- return prop.startsWith("offer") || prop === "add" ? false : undefined;
365
- }
366
- }
367
- return (value as Function).call(target, element);
368
- };
369
- }
370
- }
371
-
372
- return value;
373
- },
374
- }) as Deque<T> & BoundedCollection;
375
- };
376
- }
377
-
378
- /**
379
- * Creates a bounded Map middleware.
380
- */
381
- export function boundedMap<K, V>(
382
- options: Omit<BoundedOptions<V>, "onOverflow"> & {
383
- onOverflow?: (context: Omit<OverflowContext<V>, "newElement"> & { key: K; value: V }) => void;
384
- }
385
- ): Middleware<MapLike<K, V>, MapLike<K, V> & BoundedCollection> {
386
- const { capacity, policy = "throw", onOverflow } = options;
387
-
388
- return (next: MapLike<K, V>): MapLike<K, V> & BoundedCollection => {
389
- let insertionOrder: K[] = []; // Track insertion order for drop-oldest
390
-
391
- const handleOverflow = (key: K, value: V): void => {
392
- if (onOverflow) {
393
- onOverflow({
394
- key,
395
- value,
396
- currentSize: next.size,
397
- capacity,
398
- timestamp: Date.now(),
399
- });
400
- }
401
-
402
- switch (policy) {
403
- case "throw":
404
- throw new Error(`Map is full (capacity: ${capacity})`);
405
- case "drop-oldest":
406
- // Remove oldest key
407
- if (insertionOrder.length > 0) {
408
- const oldestKey = insertionOrder.shift()!;
409
- next.delete(oldestKey);
410
- }
411
- break;
412
- case "drop-newest":
413
- case "reject":
414
- return;
415
- case "grow":
416
- return;
417
- case "block":
418
- throw new Error("Block policy not supported for sync collections");
419
- default:
420
- throw new Error(`Unknown overflow policy: ${policy}`);
421
- }
422
- };
423
-
424
- return new Proxy(next, {
425
- get(target, prop, receiver) {
426
- if (prop === "capacity") return capacity;
427
- if (prop === "isFull") return target.size >= capacity;
428
- if (prop === "remainingCapacity") return Math.max(0, capacity - target.size);
429
-
430
- const value = Reflect.get(target, prop, receiver);
431
-
432
- if (typeof value === "function") {
433
- switch (prop) {
434
- case "set":
435
- return function (key: K, val: V): V | undefined {
436
- const hadKey = target.has(key);
437
-
438
- if (!hadKey && target.size >= capacity && policy !== "grow") {
439
- handleOverflow(key, val);
440
- if (policy === "reject" || policy === "drop-newest") {
441
- return undefined;
442
- }
443
- }
444
-
445
- const result = (value as Function).call(target, key, val);
446
-
447
- // Track insertion order
448
- if (!hadKey) {
449
- insertionOrder.push(key);
450
- }
451
-
452
- return result;
453
- };
454
-
455
- case "delete":
456
- return function (key: K): V | undefined {
457
- const result = (value as Function).call(target, key);
458
- // Remove from insertion order tracking
459
- insertionOrder = insertionOrder.filter((k) => !target.keyEq(k, key));
460
- return result;
461
- };
462
-
463
- case "clear":
464
- return function (): void {
465
- (value as Function).call(target);
466
- insertionOrder = [];
467
- };
468
- }
469
- }
470
-
471
- return value;
472
- },
473
- }) as MapLike<K, V> & BoundedCollection;
474
- };
475
- }
476
-
477
- /**
478
- * Generic bounded collection middleware.
479
- * Works with any collection type.
480
- */
481
- export function bounded<C extends Collection<any>>(
482
- options: BoundedOptions<any>
483
- ): Middleware<C, C & BoundedCollection> {
484
- return boundedList(options) as any;
485
- }
@@ -1,99 +0,0 @@
1
- /**
2
- * Composite map methods built on `has`, `get`, `set` and `delete`.
3
- *
4
- * A map behavior (lruMap, ttlMap) intercepts the primitive methods. It returns these
5
- * implementations for the composite methods, bound to the behavior's own proxy, so a call such
6
- * as `setIfAbsent` goes through the behavior's `set` and keeps its rules: capacity and recency
7
- * for LRU, expiry for TTL. (BUGS-2026-07 L13: the composite methods went straight to the map
8
- * below, so they bypassed the behavior.)
9
- *
10
- * The semantics follow HashMap. One difference: `replaceEntry` and `deleteEntry` compare
11
- * values with Object.is, not with a map's custom value equality.
12
- */
13
-
14
- import type { Entry, MapLike } from "../interfaces/map.js";
15
-
16
- type AnyFn = (...args: never[]) => unknown;
17
-
18
- export function compositeMapMethod<K, V>(map: MapLike<K, V>, name: PropertyKey): AnyFn | undefined {
19
- switch (name) {
20
- case "setIfAbsent":
21
- return ((key: K, value: V): V => {
22
- if (map.has(key)) return map.get(key);
23
- map.set(key, value);
24
- return value;
25
- }) as AnyFn;
26
-
27
- case "replace":
28
- return ((key: K, value: V): V | undefined => (map.has(key) ? map.set(key, value) : undefined)) as AnyFn;
29
-
30
- case "replaceEntry":
31
- return ((key: K, oldValue: V, newValue: V): boolean => {
32
- if (!map.has(key) || !Object.is(map.get(key), oldValue)) return false;
33
- map.set(key, newValue);
34
- return true;
35
- }) as AnyFn;
36
-
37
- case "deleteEntry":
38
- return ((key: K, value: V): boolean => {
39
- if (!map.has(key) || !Object.is(map.get(key), value)) return false;
40
- map.delete(key);
41
- return true;
42
- }) as AnyFn;
43
-
44
- case "computeIfAbsent":
45
- return ((key: K, mappingFunction: (key: K) => V): V => {
46
- if (map.has(key)) return map.get(key);
47
- const value = mappingFunction(key);
48
- map.set(key, value);
49
- return value;
50
- }) as AnyFn;
51
-
52
- case "computeIfPresent":
53
- return ((key: K, remapping: (key: K, value: V) => V | undefined): V | undefined => {
54
- if (!map.has(key)) return undefined;
55
- const value = remapping(key, map.get(key));
56
- if (value === undefined) {
57
- map.delete(key);
58
- return undefined;
59
- }
60
- map.set(key, value);
61
- return value;
62
- }) as AnyFn;
63
-
64
- case "compute":
65
- return ((key: K, remapping: (key: K, value: V | undefined) => V | undefined): V | undefined => {
66
- const present = map.has(key);
67
- const value = remapping(key, present ? map.get(key) : undefined);
68
- if (value === undefined) {
69
- if (present) map.delete(key);
70
- return undefined;
71
- }
72
- map.set(key, value);
73
- return value;
74
- }) as AnyFn;
75
-
76
- case "merge":
77
- return ((key: K, value: V, remapping: (oldValue: V, newValue: V) => V | undefined): V | undefined => {
78
- if (!map.has(key)) {
79
- map.set(key, value);
80
- return value;
81
- }
82
- const merged = remapping(map.get(key), value);
83
- if (merged === undefined) {
84
- map.delete(key);
85
- return undefined;
86
- }
87
- map.set(key, merged);
88
- return merged;
89
- }) as AnyFn;
90
-
91
- case "putAll":
92
- return ((other: Iterable<Entry<K, V>>): void => {
93
- for (const entry of other) {
94
- map.set(entry.key, entry.value);
95
- }
96
- }) as AnyFn;
97
- }
98
- return undefined;
99
- }