@mark1russell7/collections 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +78 -2
- package/build/dag/index.d.ts +144 -0
- package/build/dag/index.d.ts.map +1 -0
- package/build/dag/index.js +294 -0
- package/build/dag/index.js.map +1 -0
- package/build/graph/index.d.ts +20 -0
- package/build/graph/index.d.ts.map +1 -0
- package/build/graph/index.js +48 -0
- package/build/graph/index.js.map +1 -0
- package/build/index.d.ts +4952 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +7811 -0
- package/build/index.js.map +1 -0
- package/build/testing/index.d.ts +25 -0
- package/build/testing/index.d.ts.map +1 -0
- package/build/testing/index.js +72 -0
- package/build/testing/index.js.map +1 -0
- package/package.json +77 -3
- package/src/async/async-queue.ts +481 -0
- package/src/async/channels.ts +450 -0
- package/src/async/settled.ts +41 -0
- package/src/behaviors/bounded.ts +485 -0
- package/src/behaviors/composite.ts +99 -0
- package/src/behaviors/evented.ts +464 -0
- package/src/behaviors/lru.ts +449 -0
- package/src/behaviors/readonly.ts +236 -0
- package/src/behaviors/safe.ts +358 -0
- package/src/behaviors/synchronized.ts +316 -0
- package/src/behaviors/ttl.ts +464 -0
- package/src/core/effects.ts +327 -0
- package/src/core/events.ts +363 -0
- package/src/core/middleware.ts +413 -0
- package/src/core/policies.ts +316 -0
- package/src/core/traits.ts +164 -0
- package/src/dag/builder.ts +124 -0
- package/src/dag/executor.ts +190 -0
- package/src/dag/index.ts +14 -0
- package/src/dag/traversal.ts +129 -0
- package/src/dag/types.ts +87 -0
- package/src/fx/collectors.ts +500 -0
- package/src/fx/iter.ts +536 -0
- package/src/graph/hub-order.ts +53 -0
- package/src/graph/index.ts +5 -0
- package/src/impl/array-deque.ts +463 -0
- package/src/impl/array-list.ts +490 -0
- package/src/impl/hash-map.ts +623 -0
- package/src/impl/hash-set.ts +342 -0
- package/src/impl/linked-hash-map.ts +572 -0
- package/src/impl/linked-list.ts +610 -0
- package/src/impl/priority-queue.ts +448 -0
- package/src/impl/summary-tree.ts +594 -0
- package/src/impl/tree-map.ts +913 -0
- package/src/impl/tree-set.ts +469 -0
- package/src/index.ts +237 -0
- package/src/interfaces/collection.ts +141 -0
- package/src/interfaces/list.ts +281 -0
- package/src/interfaces/map.ts +405 -0
- package/src/interfaces/queue.ts +353 -0
- package/src/interfaces/set.ts +158 -0
- package/src/order/index.ts +6 -0
- package/src/order/rank.ts +271 -0
- package/src/storage/listeners.ts +72 -0
- package/src/testing/check.ts +81 -0
- package/src/testing/index.ts +7 -0
- package/src/utils/defaults.ts +345 -0
- package/src/utils/factories.ts +357 -0
- package/src/utils/helpers.ts +248 -0
|
@@ -0,0 +1,464 @@
|
|
|
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 gives 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
|
+
* This function makes a TTL Map middleware.
|
|
64
|
+
*
|
|
65
|
+
* Automatically removes entries after TTL expires.
|
|
66
|
+
* This function 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
|
+
* This function makes 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 can fail 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 readonly ttl: number;
|
|
307
|
+
private readonly checkInterval: number;
|
|
308
|
+
private readonly onExpire: ((key: K, value: V) => void) | undefined;
|
|
309
|
+
private map = new Map<K, TTLEntry<V>>();
|
|
310
|
+
private intervalId: ReturnType<typeof setInterval> | null = null;
|
|
311
|
+
|
|
312
|
+
constructor(ttl: number, checkInterval: number = 1000, onExpire?: ((key: K, value: V) => void) | undefined) {
|
|
313
|
+
this.ttl = ttl;
|
|
314
|
+
this.checkInterval = checkInterval;
|
|
315
|
+
this.onExpire = onExpire;
|
|
316
|
+
this.startExpirationCheck();
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
get size(): number {
|
|
320
|
+
this.cleanup();
|
|
321
|
+
return this.map.size;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
get(key: K): V | undefined {
|
|
325
|
+
const entry = this.map.get(key);
|
|
326
|
+
if (!entry) return undefined;
|
|
327
|
+
|
|
328
|
+
if (Date.now() >= entry.expiresAt) {
|
|
329
|
+
this.map.delete(key);
|
|
330
|
+
if (this.onExpire) {
|
|
331
|
+
this.onExpire(key, entry.value);
|
|
332
|
+
}
|
|
333
|
+
return undefined;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return entry.value;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
set(key: K, value: V, customTTL?: number): void {
|
|
340
|
+
const now = Date.now();
|
|
341
|
+
const ttlToUse = customTTL !== undefined ? customTTL : this.ttl;
|
|
342
|
+
|
|
343
|
+
this.map.set(key, {
|
|
344
|
+
value,
|
|
345
|
+
expiresAt: now + ttlToUse,
|
|
346
|
+
createdAt: now,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
has(key: K): boolean {
|
|
351
|
+
return this.get(key) !== undefined;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
delete(key: K): boolean {
|
|
355
|
+
return this.map.delete(key);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
clear(): void {
|
|
359
|
+
this.map.clear();
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Manually trigger cleanup of expired entries.
|
|
364
|
+
*/
|
|
365
|
+
cleanup(): void {
|
|
366
|
+
const now = Date.now();
|
|
367
|
+
const expired: K[] = [];
|
|
368
|
+
|
|
369
|
+
for (const [key, entry] of this.map) {
|
|
370
|
+
if (now >= entry.expiresAt) {
|
|
371
|
+
expired.push(key);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
for (const key of expired) {
|
|
376
|
+
const entry = this.map.get(key);
|
|
377
|
+
if (entry) {
|
|
378
|
+
this.map.delete(key);
|
|
379
|
+
if (this.onExpire) {
|
|
380
|
+
this.onExpire(key, entry.value);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Get remaining TTL for a key in milliseconds.
|
|
388
|
+
* This method gives undefined if key doesn't exist or is expired.
|
|
389
|
+
*/
|
|
390
|
+
getTTL(key: K): number | undefined {
|
|
391
|
+
const entry = this.map.get(key);
|
|
392
|
+
if (!entry) return undefined;
|
|
393
|
+
|
|
394
|
+
const remaining = entry.expiresAt - Date.now();
|
|
395
|
+
return remaining > 0 ? remaining : undefined;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Update TTL for an existing key.
|
|
400
|
+
*/
|
|
401
|
+
touch(key: K, customTTL?: number): boolean {
|
|
402
|
+
const entry = this.map.get(key);
|
|
403
|
+
if (!entry) return false;
|
|
404
|
+
|
|
405
|
+
if (Date.now() >= entry.expiresAt) {
|
|
406
|
+
this.map.delete(key);
|
|
407
|
+
return false;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const ttlToUse = customTTL !== undefined ? customTTL : this.ttl;
|
|
411
|
+
entry.expiresAt = Date.now() + ttlToUse;
|
|
412
|
+
return true;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
private startExpirationCheck(): void {
|
|
416
|
+
this.intervalId = startExpirationTimer(() => this.cleanup(), this.checkInterval);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Stop the background expiration checker and clean up.
|
|
421
|
+
*/
|
|
422
|
+
dispose(): void {
|
|
423
|
+
if (this.intervalId) {
|
|
424
|
+
clearInterval(this.intervalId);
|
|
425
|
+
this.intervalId = null;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
*keys(): IterableIterator<K> {
|
|
430
|
+
this.cleanup();
|
|
431
|
+
yield* this.map.keys();
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
*values(): IterableIterator<V> {
|
|
435
|
+
this.cleanup();
|
|
436
|
+
for (const entry of this.map.values()) {
|
|
437
|
+
yield entry.value;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
*entries(): IterableIterator<[K, V]> {
|
|
442
|
+
this.cleanup();
|
|
443
|
+
for (const [key, entry] of this.map) {
|
|
444
|
+
yield [key, entry.value];
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Factory function to make a TTL cache.
|
|
451
|
+
*/
|
|
452
|
+
export function ttlCache<K, V>(
|
|
453
|
+
ttl: number,
|
|
454
|
+
options?: {
|
|
455
|
+
checkInterval?: number;
|
|
456
|
+
onExpire?: (key: K, value: V) => void;
|
|
457
|
+
}
|
|
458
|
+
): TTLCache<K, V> {
|
|
459
|
+
return new TTLCache(
|
|
460
|
+
ttl,
|
|
461
|
+
options?.checkInterval,
|
|
462
|
+
options?.onExpire
|
|
463
|
+
);
|
|
464
|
+
}
|