@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,462 +0,0 @@
1
- /**
2
- * TTL (Time-To-Live) expiration behavior.
3
- *
4
- * Automatically expires elements after a specified time period.
5
- * Perfect for temporary caches and time-limited data.
6
- *
7
- * @example
8
- * const cache = compose(
9
- * ttlMap({ ttl: 60000 }) // 60 seconds
10
- * )(hashMap<string, Data>())
11
- *
12
- * cache.set('key', data)
13
- * // After 60 seconds, 'key' is automatically removed
14
- */
15
-
16
- import type { Middleware } from "../core/middleware.js";
17
- import type { MapLike } from "../interfaces/map.js";
18
- import { compositeMapMethod } from "./composite.js";
19
- import type { Collection } from "../interfaces/collection.js";
20
-
21
- /**
22
- * Start the background expiration checker without keeping a Node.js process alive.
23
- * (In a browser, setInterval returns a number and there is nothing to unref.)
24
- */
25
- function startExpirationTimer(check: () => void, interval: number): ReturnType<typeof setInterval> {
26
- const timer = setInterval(check, interval);
27
- (timer as unknown as { unref?: () => void }).unref?.();
28
- return timer;
29
- }
30
-
31
- /**
32
- * Options for TTL behavior.
33
- */
34
- export interface TTLOptions {
35
- /**
36
- * Time-to-live in milliseconds.
37
- * Elements expire after this duration.
38
- */
39
- ttl: number;
40
-
41
- /**
42
- * Interval for checking expired elements (in milliseconds).
43
- * @default 1000 (check every second)
44
- */
45
- checkInterval?: number;
46
-
47
- /**
48
- * Optional callback invoked when an element expires.
49
- */
50
- onExpire?: (info: { key: any; value: any; createdAt: number; expiredAt: number }) => void;
51
- }
52
-
53
- /**
54
- * Metadata for TTL tracking.
55
- */
56
- interface TTLEntry<V> {
57
- value: V;
58
- expiresAt: number;
59
- createdAt: number;
60
- }
61
-
62
- /**
63
- * Creates a TTL Map middleware.
64
- *
65
- * Automatically removes entries after TTL expires.
66
- * Uses a background interval to check for expired entries.
67
- */
68
- export function ttlMap<K, V>(
69
- options: TTLOptions
70
- ): Middleware<MapLike<K, V>, MapLike<K, V> & { readonly dispose: () => void }> {
71
- const { ttl, checkInterval = 1000, onExpire } = options;
72
-
73
- return (
74
- next: MapLike<K, V>
75
- ): MapLike<K, V> & { readonly dispose: () => void } => {
76
- const metadata = new Map<K, TTLEntry<V>>();
77
- let intervalId: ReturnType<typeof setInterval> | null = null;
78
-
79
- const checkExpired = (): void => {
80
- const now = Date.now();
81
- const expired: K[] = [];
82
-
83
- for (const [key, entry] of metadata) {
84
- if (now >= entry.expiresAt) {
85
- expired.push(key);
86
- }
87
- }
88
-
89
- for (const key of expired) {
90
- const entry = metadata.get(key);
91
- if (entry) {
92
- next.delete(key);
93
- metadata.delete(key);
94
-
95
- if (onExpire) {
96
- onExpire({
97
- key,
98
- value: entry.value,
99
- createdAt: entry.createdAt,
100
- expiredAt: now,
101
- });
102
- }
103
- }
104
- }
105
- };
106
-
107
- // Start background expiration checker
108
- intervalId = startExpirationTimer(checkExpired, checkInterval);
109
-
110
- const dispose = (): void => {
111
- if (intervalId) {
112
- clearInterval(intervalId);
113
- intervalId = null;
114
- }
115
- };
116
-
117
- return new Proxy(next, {
118
- get(target, prop, receiver) {
119
- // Add dispose method
120
- if (prop === "dispose") {
121
- return dispose;
122
- }
123
-
124
- // Composite methods (setIfAbsent, compute, merge, putAll and others) go through this
125
- // proxy's own primitives, so they keep the expiry rules (BUGS-2026-07 L13)
126
- const composite = compositeMapMethod(receiver as MapLike<K, V>, prop);
127
- if (composite) {
128
- return composite;
129
- }
130
-
131
- const value = Reflect.get(target, prop, receiver);
132
-
133
- if (typeof value === "function") {
134
- switch (prop) {
135
- case "get":
136
- return function (key: K): V {
137
- // Check if expired before returning
138
- const entry = metadata.get(key);
139
- if (entry && Date.now() >= entry.expiresAt) {
140
- target.delete(key);
141
- metadata.delete(key);
142
- throw new Error(`Key expired: ${key}`);
143
- }
144
- return (value as Function).call(target, key);
145
- };
146
-
147
- case "has":
148
- return function (key: K): boolean {
149
- // Check if expired
150
- const entry = metadata.get(key);
151
- if (entry && Date.now() >= entry.expiresAt) {
152
- target.delete(key);
153
- metadata.delete(key);
154
- return false;
155
- }
156
- return (value as Function).call(target, key);
157
- };
158
-
159
- case "set":
160
- return function (key: K, val: V): V | undefined {
161
- const now = Date.now();
162
- const result = (value as Function).call(target, key, val);
163
-
164
- metadata.set(key, {
165
- value: val,
166
- expiresAt: now + ttl,
167
- createdAt: now,
168
- });
169
-
170
- return result;
171
- };
172
-
173
- case "delete":
174
- return function (key: K): V | undefined {
175
- const result = (value as Function).call(target, key);
176
- metadata.delete(key);
177
- return result;
178
- };
179
-
180
- case "clear":
181
- return function (): void {
182
- (value as Function).call(target);
183
- metadata.clear();
184
- };
185
- }
186
- }
187
-
188
- return value;
189
- },
190
- }) as MapLike<K, V> & { readonly dispose: () => void };
191
- };
192
- }
193
-
194
- /**
195
- * Creates a TTL Collection middleware.
196
- *
197
- * Note: This is less precise than TTL map because collections don't have keys.
198
- * Elements are tracked by reference, which may not work for primitive types.
199
- */
200
- export function ttlCollection<T>(
201
- options: Omit<TTLOptions, "onExpire"> & {
202
- onExpire?: (info: { element: T; createdAt: number; expiredAt: number }) => void;
203
- }
204
- ): Middleware<Collection<T>, Collection<T> & { readonly dispose: () => void }> {
205
- const { ttl, checkInterval = 1000, onExpire } = options;
206
-
207
- return (
208
- next: Collection<T>
209
- ): Collection<T> & { readonly dispose: () => void } => {
210
- const metadata = new Map<T, { expiresAt: number; createdAt: number }>();
211
- let intervalId: ReturnType<typeof setInterval> | null = null;
212
-
213
- const checkExpired = (): void => {
214
- const now = Date.now();
215
- const expired: T[] = [];
216
-
217
- for (const [element, entry] of metadata) {
218
- if (now >= entry.expiresAt) {
219
- expired.push(element);
220
- }
221
- }
222
-
223
- for (const element of expired) {
224
- const entry = metadata.get(element);
225
- if (entry) {
226
- next.remove(element);
227
- metadata.delete(element);
228
-
229
- if (onExpire) {
230
- onExpire({
231
- element,
232
- createdAt: entry.createdAt,
233
- expiredAt: now,
234
- });
235
- }
236
- }
237
- }
238
- };
239
-
240
- intervalId = startExpirationTimer(checkExpired, checkInterval);
241
-
242
- const dispose = (): void => {
243
- if (intervalId) {
244
- clearInterval(intervalId);
245
- intervalId = null;
246
- }
247
- };
248
-
249
- return new Proxy(next, {
250
- get(target, prop, receiver) {
251
- if (prop === "dispose") {
252
- return dispose;
253
- }
254
-
255
- const value = Reflect.get(target, prop, receiver);
256
-
257
- if (typeof value === "function") {
258
- switch (prop) {
259
- case "add":
260
- return function (element: T): boolean {
261
- const result = (value as Function).call(target, element);
262
- if (result) {
263
- const now = Date.now();
264
- metadata.set(element, {
265
- expiresAt: now + ttl,
266
- createdAt: now,
267
- });
268
- }
269
- return result;
270
- };
271
-
272
- case "remove":
273
- return function (element: T): boolean {
274
- const result = (value as Function).call(target, element);
275
- if (result) {
276
- metadata.delete(element);
277
- }
278
- return result;
279
- };
280
-
281
- case "clear":
282
- return function (): void {
283
- (value as Function).call(target);
284
- metadata.clear();
285
- };
286
- }
287
- }
288
-
289
- return value;
290
- },
291
- }) as Collection<T> & { readonly dispose: () => void };
292
- };
293
- }
294
-
295
- /**
296
- * Standalone TTL cache with Map-like API.
297
- *
298
- * @example
299
- * const cache = new TTLCache<string, number>(60000) // 60 second TTL
300
- * cache.set('key', 42)
301
- * setTimeout(() => {
302
- * cache.get('key') // undefined (expired)
303
- * }, 61000)
304
- */
305
- export class TTLCache<K, V> {
306
- private map = new Map<K, TTLEntry<V>>();
307
- private intervalId: ReturnType<typeof setInterval> | null = null;
308
-
309
- constructor(
310
- private readonly ttl: number,
311
- private readonly checkInterval: number = 1000,
312
- private readonly onExpire?: (key: K, value: V) => void
313
- ) {
314
- this.startExpirationCheck();
315
- }
316
-
317
- get size(): number {
318
- this.cleanup();
319
- return this.map.size;
320
- }
321
-
322
- get(key: K): V | undefined {
323
- const entry = this.map.get(key);
324
- if (!entry) return undefined;
325
-
326
- if (Date.now() >= entry.expiresAt) {
327
- this.map.delete(key);
328
- if (this.onExpire) {
329
- this.onExpire(key, entry.value);
330
- }
331
- return undefined;
332
- }
333
-
334
- return entry.value;
335
- }
336
-
337
- set(key: K, value: V, customTTL?: number): void {
338
- const now = Date.now();
339
- const ttlToUse = customTTL !== undefined ? customTTL : this.ttl;
340
-
341
- this.map.set(key, {
342
- value,
343
- expiresAt: now + ttlToUse,
344
- createdAt: now,
345
- });
346
- }
347
-
348
- has(key: K): boolean {
349
- return this.get(key) !== undefined;
350
- }
351
-
352
- delete(key: K): boolean {
353
- return this.map.delete(key);
354
- }
355
-
356
- clear(): void {
357
- this.map.clear();
358
- }
359
-
360
- /**
361
- * Manually trigger cleanup of expired entries.
362
- */
363
- cleanup(): void {
364
- const now = Date.now();
365
- const expired: K[] = [];
366
-
367
- for (const [key, entry] of this.map) {
368
- if (now >= entry.expiresAt) {
369
- expired.push(key);
370
- }
371
- }
372
-
373
- for (const key of expired) {
374
- const entry = this.map.get(key);
375
- if (entry) {
376
- this.map.delete(key);
377
- if (this.onExpire) {
378
- this.onExpire(key, entry.value);
379
- }
380
- }
381
- }
382
- }
383
-
384
- /**
385
- * Get remaining TTL for a key in milliseconds.
386
- * Returns undefined if key doesn't exist or is expired.
387
- */
388
- getTTL(key: K): number | undefined {
389
- const entry = this.map.get(key);
390
- if (!entry) return undefined;
391
-
392
- const remaining = entry.expiresAt - Date.now();
393
- return remaining > 0 ? remaining : undefined;
394
- }
395
-
396
- /**
397
- * Update TTL for an existing key.
398
- */
399
- touch(key: K, customTTL?: number): boolean {
400
- const entry = this.map.get(key);
401
- if (!entry) return false;
402
-
403
- if (Date.now() >= entry.expiresAt) {
404
- this.map.delete(key);
405
- return false;
406
- }
407
-
408
- const ttlToUse = customTTL !== undefined ? customTTL : this.ttl;
409
- entry.expiresAt = Date.now() + ttlToUse;
410
- return true;
411
- }
412
-
413
- private startExpirationCheck(): void {
414
- this.intervalId = startExpirationTimer(() => this.cleanup(), this.checkInterval);
415
- }
416
-
417
- /**
418
- * Stop the background expiration checker and clean up.
419
- */
420
- dispose(): void {
421
- if (this.intervalId) {
422
- clearInterval(this.intervalId);
423
- this.intervalId = null;
424
- }
425
- }
426
-
427
- *keys(): IterableIterator<K> {
428
- this.cleanup();
429
- yield* this.map.keys();
430
- }
431
-
432
- *values(): IterableIterator<V> {
433
- this.cleanup();
434
- for (const entry of this.map.values()) {
435
- yield entry.value;
436
- }
437
- }
438
-
439
- *entries(): IterableIterator<[K, V]> {
440
- this.cleanup();
441
- for (const [key, entry] of this.map) {
442
- yield [key, entry.value];
443
- }
444
- }
445
- }
446
-
447
- /**
448
- * Factory function to create a TTL cache.
449
- */
450
- export function ttlCache<K, V>(
451
- ttl: number,
452
- options?: {
453
- checkInterval?: number;
454
- onExpire?: (key: K, value: V) => void;
455
- }
456
- ): TTLCache<K, V> {
457
- return new TTLCache(
458
- ttl,
459
- options?.checkInterval,
460
- options?.onExpire
461
- );
462
- }
@@ -1,44 +0,0 @@
1
- /**
2
- * Helpers for the contract tests: random operation sequences that run against an implementation
3
- * and a plain JavaScript model, with a check after each operation (ARCHITECTURE-PROPOSALS P8).
4
- */
5
-
6
- import fc from "fast-check";
7
-
8
- /** One operation of a sequence: it changes the implementation and the model in the same way. */
9
- export interface Step<S, M> {
10
- readonly label: string;
11
- run(sut: S, model: M): void;
12
- }
13
-
14
- /** Small integers: many duplicates and collisions, so the edge cases come up often. */
15
- export const smallInt: fc.Arbitrary<number> = fc.integer({ min: -20, max: 20 });
16
-
17
- /**
18
- * This function runs random sequences of steps against a new implementation and a new model,
19
- * and calls `check` after each step. fast-check shrinks a failure to a short sequence.
20
- */
21
- export function runModel<S, M>(options: {
22
- create: () => { sut: S; model: M };
23
- steps: fc.Arbitrary<Step<S, M>>;
24
- check: (sut: S, model: M) => void;
25
- maxSteps?: number;
26
- numRuns?: number;
27
- }): void {
28
- fc.assert(
29
- fc.property(fc.array(options.steps, { maxLength: options.maxSteps ?? 120 }), (steps) => {
30
- const { sut, model } = options.create();
31
- options.check(sut, model);
32
- for (const step of steps) {
33
- step.run(sut, model);
34
- options.check(sut, model);
35
- }
36
- }),
37
- { numRuns: options.numRuns ?? 200 },
38
- );
39
- }
40
-
41
- /** A step with a label that shows in a failure report. */
42
- export function step<S, M>(label: string, run: (sut: S, model: M) => void): Step<S, M> {
43
- return { label, run, toString: () => label } as Step<S, M>;
44
- }