@ac-kit/data 0.1.5 → 0.3.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/CHANGELOG.md +58 -0
- package/README.md +10 -9
- package/dist/cache/lru-cache.d.ts +6 -5
- package/dist/cache/lru-cache.d.ts.map +1 -1
- package/dist/cache/lru-cache.js +8 -3
- package/dist/cache/lru-cache.js.map +1 -1
- package/dist/cache/lru-map.d.ts +6 -5
- package/dist/cache/lru-map.d.ts.map +1 -1
- package/dist/cache/lru-map.js +8 -3
- package/dist/cache/lru-map.js.map +1 -1
- package/dist/cache/ttl-map.d.ts +108 -0
- package/dist/cache/ttl-map.d.ts.map +1 -0
- package/dist/cache/ttl-map.js +212 -0
- package/dist/cache/ttl-map.js.map +1 -0
- package/dist/collection/_bounded.d.ts +4 -7
- package/dist/collection/_bounded.d.ts.map +1 -1
- package/dist/collection/_bounded.js +6 -12
- package/dist/collection/_bounded.js.map +1 -1
- package/dist/collection/ibounded.d.ts +7 -13
- package/dist/collection/ibounded.d.ts.map +1 -1
- package/dist/deque/blocking-deque.d.ts +1 -1
- package/dist/deque/blocking-deque.d.ts.map +1 -1
- package/dist/deque/blocking-deque.js +3 -3
- package/dist/deque/blocking-deque.js.map +1 -1
- package/dist/deque/bounded-deque.d.ts +1 -1
- package/dist/deque/bounded-deque.d.ts.map +1 -1
- package/dist/deque/bounded-deque.js +2 -2
- package/dist/deque/bounded-deque.js.map +1 -1
- package/dist/fuzzy/fuzzy-map.d.ts +6 -5
- package/dist/fuzzy/fuzzy-map.d.ts.map +1 -1
- package/dist/fuzzy/fuzzy-map.js +7 -2
- package/dist/fuzzy/fuzzy-map.js.map +1 -1
- package/dist/fuzzy/fuzzy-multi-map.d.ts +6 -5
- package/dist/fuzzy/fuzzy-multi-map.d.ts.map +1 -1
- package/dist/fuzzy/fuzzy-multi-map.js +7 -2
- package/dist/fuzzy/fuzzy-multi-map.js.map +1 -1
- package/dist/heap/blocking-binary-heap.d.ts +1 -1
- package/dist/heap/blocking-binary-heap.d.ts.map +1 -1
- package/dist/heap/blocking-binary-heap.js +3 -3
- package/dist/heap/blocking-binary-heap.js.map +1 -1
- package/dist/heap/bounded-binary-heap.d.ts +1 -1
- package/dist/heap/bounded-binary-heap.d.ts.map +1 -1
- package/dist/heap/bounded-binary-heap.js +3 -3
- package/dist/heap/bounded-binary-heap.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/list/blocking-array-list.d.ts +1 -1
- package/dist/list/blocking-array-list.d.ts.map +1 -1
- package/dist/list/blocking-array-list.js +3 -3
- package/dist/list/blocking-array-list.js.map +1 -1
- package/dist/list/blocking-circular-array-list.d.ts +1 -1
- package/dist/list/blocking-circular-array-list.d.ts.map +1 -1
- package/dist/list/blocking-circular-array-list.js +3 -3
- package/dist/list/blocking-circular-array-list.js.map +1 -1
- package/dist/list/blocking-doubly-linked-list.d.ts +1 -1
- package/dist/list/blocking-doubly-linked-list.d.ts.map +1 -1
- package/dist/list/blocking-doubly-linked-list.js +3 -3
- package/dist/list/blocking-doubly-linked-list.js.map +1 -1
- package/dist/list/blocking-linked-list.d.ts +1 -1
- package/dist/list/blocking-linked-list.d.ts.map +1 -1
- package/dist/list/blocking-linked-list.js +3 -3
- package/dist/list/blocking-linked-list.js.map +1 -1
- package/dist/list/bounded-array-list.d.ts +1 -1
- package/dist/list/bounded-array-list.d.ts.map +1 -1
- package/dist/list/bounded-array-list.js +3 -3
- package/dist/list/bounded-array-list.js.map +1 -1
- package/dist/list/bounded-circular-array-list.d.ts +1 -1
- package/dist/list/bounded-circular-array-list.d.ts.map +1 -1
- package/dist/list/bounded-circular-array-list.js +3 -3
- package/dist/list/bounded-circular-array-list.js.map +1 -1
- package/dist/list/bounded-doubly-linked-list.d.ts +1 -1
- package/dist/list/bounded-doubly-linked-list.d.ts.map +1 -1
- package/dist/list/bounded-doubly-linked-list.js +3 -3
- package/dist/list/bounded-doubly-linked-list.js.map +1 -1
- package/dist/list/bounded-linked-list.d.ts +1 -1
- package/dist/list/bounded-linked-list.d.ts.map +1 -1
- package/dist/list/bounded-linked-list.js +3 -3
- package/dist/list/bounded-linked-list.js.map +1 -1
- package/dist/priority-queue/blocking-priority-queue.d.ts +1 -1
- package/dist/priority-queue/blocking-priority-queue.d.ts.map +1 -1
- package/dist/priority-queue/blocking-priority-queue.js +3 -3
- package/dist/priority-queue/blocking-priority-queue.js.map +1 -1
- package/dist/priority-queue/bounded-priority-queue.d.ts +1 -1
- package/dist/priority-queue/bounded-priority-queue.d.ts.map +1 -1
- package/dist/priority-queue/bounded-priority-queue.js +3 -3
- package/dist/priority-queue/bounded-priority-queue.js.map +1 -1
- package/dist/probabilistic/bloom-filter.d.ts +9 -8
- package/dist/probabilistic/bloom-filter.d.ts.map +1 -1
- package/dist/probabilistic/bloom-filter.js +8 -4
- package/dist/probabilistic/bloom-filter.js.map +1 -1
- package/dist/probabilistic/count-min-sketch.d.ts +7 -8
- package/dist/probabilistic/count-min-sketch.d.ts.map +1 -1
- package/dist/probabilistic/count-min-sketch.js +5 -4
- package/dist/probabilistic/count-min-sketch.js.map +1 -1
- package/dist/probabilistic/cuckoo-filter.d.ts +10 -9
- package/dist/probabilistic/cuckoo-filter.d.ts.map +1 -1
- package/dist/probabilistic/cuckoo-filter.js +9 -5
- package/dist/probabilistic/cuckoo-filter.js.map +1 -1
- package/dist/probabilistic/hyper-log-log.d.ts +7 -5
- package/dist/probabilistic/hyper-log-log.d.ts.map +1 -1
- package/dist/probabilistic/hyper-log-log.js +7 -3
- package/dist/probabilistic/hyper-log-log.js.map +1 -1
- package/dist/probabilistic/top-k.d.ts +7 -7
- package/dist/probabilistic/top-k.d.ts.map +1 -1
- package/dist/probabilistic/top-k.js +10 -5
- package/dist/probabilistic/top-k.js.map +1 -1
- package/dist/queue/blocking-queue.d.ts +1 -1
- package/dist/queue/blocking-queue.d.ts.map +1 -1
- package/dist/queue/blocking-queue.js +3 -3
- package/dist/queue/blocking-queue.js.map +1 -1
- package/dist/queue/bounded-queue.d.ts +4 -4
- package/dist/queue/bounded-queue.d.ts.map +1 -1
- package/dist/queue/bounded-queue.js +6 -6
- package/dist/queue/bounded-queue.js.map +1 -1
- package/dist/queue/lossy-queue.d.ts +3 -2
- package/dist/queue/lossy-queue.d.ts.map +1 -1
- package/dist/queue/lossy-queue.js +5 -5
- package/dist/queue/lossy-queue.js.map +1 -1
- package/dist/similarity/vp-tree.d.ts +6 -5
- package/dist/similarity/vp-tree.d.ts.map +1 -1
- package/dist/similarity/vp-tree.js +7 -2
- package/dist/similarity/vp-tree.js.map +1 -1
- package/dist/stack/blocking-stack.d.ts +1 -1
- package/dist/stack/blocking-stack.d.ts.map +1 -1
- package/dist/stack/blocking-stack.js +3 -3
- package/dist/stack/blocking-stack.js.map +1 -1
- package/dist/stack/bounded-stack.d.ts +1 -1
- package/dist/stack/bounded-stack.d.ts.map +1 -1
- package/dist/stack/bounded-stack.js +3 -3
- package/dist/stack/bounded-stack.js.map +1 -1
- package/dist/storage/ring-vector.d.ts +3 -4
- package/dist/storage/ring-vector.d.ts.map +1 -1
- package/dist/storage/ring-vector.js.map +1 -1
- package/dist/storage/segmented-ring-vector.d.ts +3 -4
- package/dist/storage/segmented-ring-vector.d.ts.map +1 -1
- package/dist/storage/segmented-ring-vector.js.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,63 @@
|
|
|
1
1
|
# @ac-kit/data
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 99ca4b9: `TtlMap<K, V>`: a bounded map whose entries expire a time after they were
|
|
8
|
+
set — the map's `ttlMs`, or one given to `set` — on an injected clock. An
|
|
9
|
+
expired entry is never seen by `get`, `has`, `take`, `count` or iteration.
|
|
10
|
+
`take(key)` removes the entry it answers, for one-time challenges. When full,
|
|
11
|
+
expired entries go first, then the entry set longest ago (`"fifo"`), and `set`
|
|
12
|
+
returns what it displaced; overwriting a held key never evicts.
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- Updated dependencies [99ca4b9]
|
|
17
|
+
- @ac-kit/core@0.8.0
|
|
18
|
+
|
|
19
|
+
## 0.2.0
|
|
20
|
+
|
|
21
|
+
### Minor Changes
|
|
22
|
+
|
|
23
|
+
- 7f9f15d: Constructor argument shapes follow one rule: required arguments are
|
|
24
|
+
positional and come first, `options` is optional with every field optional.
|
|
25
|
+
Capacity-bounded classes take the capacity first, then their unbounded
|
|
26
|
+
sibling's arguments unchanged; structures defined by a function take that
|
|
27
|
+
function first, then their sizing, then `iterable?`, then `options?`.
|
|
28
|
+
|
|
29
|
+
- `new BoundedQueue(capacity, iterable?, options?)`, and the same shape for
|
|
30
|
+
`BlockingQueue`, `BoundedStack`, `BlockingStack`, `BoundedDeque`,
|
|
31
|
+
`BlockingDeque`, `BoundedArrayList`, `BoundedCircularArrayList`,
|
|
32
|
+
`BoundedLinkedList`, `BoundedDoublyLinkedList` and their `Blocking*`
|
|
33
|
+
counterparts. `BoundedOptions<TStorage>` (and every `Bounded*Options`/
|
|
34
|
+
`Blocking*Options` alias) now only holds `storage`; a storage that carries
|
|
35
|
+
its own capacity no longer supplies the collection's, it only caps it.
|
|
36
|
+
- `new BoundedBinaryHeap(capacity, precedes, iterable?, options?)`,
|
|
37
|
+
`new BlockingBinaryHeap(capacity, precedes, iterable?, options?)`,
|
|
38
|
+
`new BoundedPriorityQueue(capacity, precedes?, iterable?, options?)`,
|
|
39
|
+
`new BlockingPriorityQueue(capacity, precedes?, iterable?, options?)`.
|
|
40
|
+
- `new LossyQueue(capacity, iterable?, options?)`; `LossyQueueOptions` keeps
|
|
41
|
+
`overflowPolicy` (default `"evict"`) and `storage`.
|
|
42
|
+
- `new LruCache(capacity, iterable?)`, `new LruMap(capacity, iterable?)`;
|
|
43
|
+
`LruCacheOptions` and `LruMapOptions` are removed.
|
|
44
|
+
- `new FuzzyMap(normalize, iterable?)`, `new FuzzyMultiMap(normalize,
|
|
45
|
+
iterable?)`; `FuzzyMapOptions` and `FuzzyMultiMapOptions` are removed.
|
|
46
|
+
- `new VpTree(distance, iterable?)`; `VpTreeOptions` is removed.
|
|
47
|
+
- `new BloomFilter(hash, expectedItems, iterable?, options?)` with
|
|
48
|
+
`BloomFilterOptions.falsePositiveRate` (default `0.01`);
|
|
49
|
+
`new CuckooFilter(hash, expectedItems, iterable?, options?)` with
|
|
50
|
+
`bucketSize` (default `4`) and `maxKicks` (default `500`);
|
|
51
|
+
`new HyperLogLog(hash, iterable?, options?)` with `precision` (default
|
|
52
|
+
`12`); `new CountMinSketch(hash, options?)` with `epsilon` and `delta`
|
|
53
|
+
(default `0.001` each); `new TopK(hash, k, options?)`, where `TopKOptions`
|
|
54
|
+
is `CountMinSketchOptions`. These option types are no longer generic.
|
|
55
|
+
|
|
56
|
+
### Patch Changes
|
|
57
|
+
|
|
58
|
+
- Updated dependencies [7f9f15d]
|
|
59
|
+
- @ac-kit/core@0.7.0
|
|
60
|
+
|
|
3
61
|
## 0.1.5
|
|
4
62
|
|
|
5
63
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ indexes and probabilistic sketches.
|
|
|
8
8
|
| Sequences | `ArrayList`, `CircularArrayList`, `LinkedList`, `DoublyLinkedList` |
|
|
9
9
|
| Queues and stacks | `Queue`, `Deque`, `Stack`, `BinaryHeap`, `PriorityQueue` |
|
|
10
10
|
| Sets and maps | `EnhancedSet`, `MultiSet`, `MultiMap`, `FuzzyMap`, `FuzzyMultiMap` |
|
|
11
|
-
| Caches | `LruMap`, `LruCache`
|
|
11
|
+
| Caches | `LruMap`, `LruCache`, `TtlMap` |
|
|
12
12
|
| Trees | `BinarySearchTree`, `AvlTree`, `Trie` |
|
|
13
13
|
| Graphs | `Graph`, `DirectedGraph`, `StaticDisjointSet` |
|
|
14
14
|
| Indexes | `InvertedIndex`, `SuffixArray`, `GeneralizedSuffixArray`, `VpTree`, `StaticIntervalTree` |
|
|
@@ -22,16 +22,16 @@ answer to "what happens when it is full", rather than one class with a flag:
|
|
|
22
22
|
| variant | when full | example |
|
|
23
23
|
| ----------- | ------------------------------------- | ------------------------------------------------- |
|
|
24
24
|
| `Queue` | never full | `new Queue()` |
|
|
25
|
-
| `BoundedX` | throws | `new BoundedQueue(
|
|
26
|
-
| `BlockingX` | `waitEnqueue` waits, `enqueue` throws | `new BlockingQueue(
|
|
27
|
-
| `LossyX` | evicts or skips | `new LossyQueue(
|
|
25
|
+
| `BoundedX` | throws | `new BoundedQueue(100)` |
|
|
26
|
+
| `BlockingX` | `waitEnqueue` waits, `enqueue` throws | `new BlockingQueue(100)` |
|
|
27
|
+
| `LossyX` | evicts or skips | `new LossyQueue(100)` |
|
|
28
28
|
|
|
29
|
-
The capacity model is the class; the backing is an option.
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
The capacity model is the class; the backing is an option. The capacity is
|
|
30
|
+
always the first constructor argument, and a fixed-size collection is any
|
|
31
|
+
bounded class over a storage that never grows:
|
|
32
32
|
|
|
33
33
|
```ts
|
|
34
|
-
new
|
|
34
|
+
new BoundedQueue(1024, undefined, {
|
|
35
35
|
storage: new RingVector(undefined, { capacity: 1024 }),
|
|
36
36
|
});
|
|
37
37
|
```
|
|
@@ -54,7 +54,8 @@ repeatedly.
|
|
|
54
54
|
## Approximate structures need a hash
|
|
55
55
|
|
|
56
56
|
`BloomFilter`, `CuckooFilter`, `CountMinSketch`, `HyperLogLog` and `TopK` each
|
|
57
|
-
take a required `hash: Hash32<T
|
|
57
|
+
take a required `hash: Hash32<T>` as their first constructor argument. There is
|
|
58
|
+
no default, because
|
|
58
59
|
`@ac-kit/noncrypto-hash`'s `murmur3_32`, `xxhash_32`, `fnv_32` and `djb2` all
|
|
59
60
|
take a `Uint8Array` and no library can invent a canonical encoding from an
|
|
60
61
|
arbitrary `T` to bytes. Wrapping one takes a line:
|
|
@@ -2,10 +2,6 @@ import type { DefinedValue } from "@ac-kit/core";
|
|
|
2
2
|
import { ReplacementPolicy } from "../collection/ireplacing.js";
|
|
3
3
|
import { IReplacingSet } from "../set/ireplacing-set.js";
|
|
4
4
|
import { ISet } from "../set/iset.js";
|
|
5
|
-
export type LruCacheOptions = {
|
|
6
|
-
/** The number of items held before adding one displaces another. */
|
|
7
|
-
capacity: number;
|
|
8
|
-
};
|
|
9
5
|
/**
|
|
10
6
|
* A fixed-capacity set that evicts the least recently used item to make room.
|
|
11
7
|
*
|
|
@@ -34,7 +30,12 @@ export declare class LruCache<T extends DefinedValue> implements IReplacingSet<T
|
|
|
34
30
|
* same way `BoundedQueue`/`LossyQueue` are, never a constructor flag.
|
|
35
31
|
*/
|
|
36
32
|
readonly overflowPolicy: ReplacementPolicy;
|
|
37
|
-
|
|
33
|
+
/**
|
|
34
|
+
* @param capacity The number of items held before adding one displaces
|
|
35
|
+
* another.
|
|
36
|
+
* @param iterable Initial items, added in order.
|
|
37
|
+
*/
|
|
38
|
+
constructor(capacity: number, iterable?: Iterable<T>);
|
|
38
39
|
/** Most recently used first. */
|
|
39
40
|
[Symbol.iterator](): Iterator<T>;
|
|
40
41
|
count(): number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lru-cache.d.ts","sourceRoot":"","sources":["../../src/cache/lru-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAEhE,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAKtC
|
|
1
|
+
{"version":3,"file":"lru-cache.d.ts","sourceRoot":"","sources":["../../src/cache/lru-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,c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EjC;IAED,oEAAoE;IACpE,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAExC;IAED,oEAAoE;IACpE,UAAU,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAEtC;IAED,oEAAoE;IACpE,mBAAmB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAE/C;IAED,cAAc;IACd,UAAU,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,OAAO,CAEtC;IAED,YAAY;IACZ,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,OAAO,CAQxC;IAED,YAAY;IACZ,cAAc,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,OAAO,CAQ1C;IAED,OAAO,CAAC,KAAK;CAGb"}
|
package/dist/cache/lru-cache.js
CHANGED
|
@@ -29,9 +29,14 @@ export class LruCache {
|
|
|
29
29
|
* same way `BoundedQueue`/`LossyQueue` are, never a constructor flag.
|
|
30
30
|
*/
|
|
31
31
|
overflowPolicy = "lru";
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
/**
|
|
33
|
+
* @param capacity The number of items held before adding one displaces
|
|
34
|
+
* another.
|
|
35
|
+
* @param iterable Initial items, added in order.
|
|
36
|
+
*/
|
|
37
|
+
constructor(capacity, iterable) {
|
|
38
|
+
this.capacity = capacity;
|
|
39
|
+
this.store = new LruStore(capacity);
|
|
35
40
|
if (iterable) {
|
|
36
41
|
this.addAll(iterable);
|
|
37
42
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lru-cache.js","sourceRoot":"","sources":["../../src/cache/lru-cache.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAGrD,OAAO,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CAAC,KAAK,CAAC,CAAC;IACvC,CAAC;IAED,oEAAoE;IACpE,mBAAmB,CAAC,KAAkB;QACrC,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC;IAChD,CAAC;IAED,cAAc;IACd,UAAU,CAAC,KAAkB;QAC5B,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;IACvC,CAAC;IAED,YAAY;IACZ,YAAY,CAAC,KAAkB;QAC9B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YAC1B,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC3B,OAAO,KAAK,CAAC;YACd,CAAC;QACF,CAAC;QAED,OAAO,IAAI,CAAC;IACb,CAAC;IAED,YAAY;IACZ,cAAc,CAAC,KAAkB;QAChC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YAC1B,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC1B,OAAO,KAAK,CAAC;YACd,CAAC;QACF,CAAC;QAED,OAAO,IAAI,CAAC;IACb,CAAC;IAEO,KAAK;QACZ,OAAO,IAAI,WAAW,CAAI,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IAC9C,CAAC;CACD"}
|
package/dist/cache/lru-map.d.ts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { DefinedValue } from "@ac-kit/core";
|
|
2
2
|
import { ReplacementPolicy } from "../collection/ireplacing.js";
|
|
3
3
|
import { IReplacingMap } from "../map/ireplacing-map.js";
|
|
4
|
-
export type LruMapOptions = {
|
|
5
|
-
/** The number of entries held before setting one displaces another. */
|
|
6
|
-
capacity: number;
|
|
7
|
-
};
|
|
8
4
|
/**
|
|
9
5
|
* A fixed-capacity map that evicts the least recently used entry to make room.
|
|
10
6
|
*
|
|
@@ -35,7 +31,12 @@ export declare class LruMap<K, V extends DefinedValue> implements IReplacingMap<
|
|
|
35
31
|
* same way `BoundedQueue`/`LossyQueue` are, never a constructor flag.
|
|
36
32
|
*/
|
|
37
33
|
readonly overflowPolicy: ReplacementPolicy;
|
|
38
|
-
|
|
34
|
+
/**
|
|
35
|
+
* @param capacity The number of entries held before setting one displaces
|
|
36
|
+
* another.
|
|
37
|
+
* @param iterable Initial entries, set in order.
|
|
38
|
+
*/
|
|
39
|
+
constructor(capacity: number, iterable?: Iterable<readonly [K, V]>);
|
|
39
40
|
/** Most recently used first. Does not count as a use. */
|
|
40
41
|
[Symbol.iterator](): Iterator<readonly [K, V]>;
|
|
41
42
|
count(): number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lru-map.d.ts","sourceRoot":"","sources":["../../src/cache/lru-map.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAKzD
|
|
1
|
+
{"version":3,"file":"lru-map.d.ts","sourceRoot":"","sources":["../../src/cache/lru-map.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAKzD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,MAAM,CAAC,CAAC,EAAE,CAAC,SAAS,YAAY,CAAE,YAAW,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC;IAC5E,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAiB;IAEvC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAE1B;;;OAGG;IACH,QAAQ,CAAC,cAAc,EAAE,iBAAi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Policy = "lru";
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
/**
|
|
34
|
+
* @param capacity The number of entries held before setting one displaces
|
|
35
|
+
* another.
|
|
36
|
+
* @param iterable Initial entries, set in order.
|
|
37
|
+
*/
|
|
38
|
+
constructor(capacity, iterable) {
|
|
39
|
+
this.capacity = capacity;
|
|
40
|
+
this.store = new LruStore(capacity);
|
|
36
41
|
if (iterable) {
|
|
37
42
|
for (const [key, value] of iterable) {
|
|
38
43
|
this.store.set(key, value);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lru-map.js","sourceRoot":"","sources":["../../src/cache/lru-map.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3C,MAAM,iBAAiB,GAAyC,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"lru-map.js","sourceRoot":"","sources":["../../src/cache/lru-map.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3C,MAAM,iBAAiB,GAAyC,EAAE,CAAC;AAEnE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,OAAO,MAAM;IACD,KAAK,CAAiB;IAE9B,QAAQ,CAAS;IAE1B;;;OAGG;IACM,cAAc,GAAsB,KAAK,CAAC;IAEnD;;;;OAIG;IACH,YAAY,QAAgB,EAAE,QAAoC;QACjE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,IAAI,QAAQ,CAAO,QAAQ,CAAC,CAAC;QAE1C,IAAI,QAAQ,EAAE,CAAC;YACd,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,QAAQ,EAAE,CAAC;gBACrC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YAC5B,CAAC;QACF,CAAC;IACF,CAAC;IAED,yDAAyD;IACz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IAC7B,CAAC;IAED;;;;;OAKG;IACH,GAAG;QACF,OAAO,IAAI,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC;IACjC,CAAC;CACD"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { type Clock, type DefinedValue } from "@ac-kit/core";
|
|
2
|
+
import type { ReplacementPolicy } from "../collection/ireplacing.js";
|
|
3
|
+
import type { IReplacingMap } from "../map/ireplacing-map.js";
|
|
4
|
+
/** Options of a {@link TtlMap}. */
|
|
5
|
+
export type TtlMapOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* The clock entries age on, in milliseconds on one monotonic time line:
|
|
8
|
+
* `SYSTEM_CLOCK` (`performance.now()`) by default, a fake one in tests.
|
|
9
|
+
*/
|
|
10
|
+
clock?: Pick<Clock, "now">;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* A bounded map whose entries expire a set time after they were set: a store of
|
|
14
|
+
* one-time challenges, nonces, codes, or anything stale after a while. Reach
|
|
15
|
+
* for `LruMap` instead when entries never go stale and only room matters.
|
|
16
|
+
*
|
|
17
|
+
* An expired entry is gone: `get`, `has`, `take`, `count` and iteration never
|
|
18
|
+
* see it, and it is dropped when met, so nothing runs in the background. Time
|
|
19
|
+
* comes from the injected clock, so tests need no timers. When full, setting a
|
|
20
|
+
* new key drops the expired entries, then the entry set longest ago (`"fifo"`)
|
|
21
|
+
* if still full, so a flood of new keys costs memory up to `capacity` only.
|
|
22
|
+
* Overwriting a held key never evicts.
|
|
23
|
+
*
|
|
24
|
+
* Time: `get`, `has`, `take` and `delete` are O(1); `set` is O(log n)
|
|
25
|
+
* amortised, n the entries held; `count` and iteration are O(1) and O(n) plus
|
|
26
|
+
* O(log n) per expired entry they drop. Memory is O(capacity).
|
|
27
|
+
*
|
|
28
|
+
* Keys are compared by `SameValueZero`, native `Map` semantics.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* const challenges = new TtlMap<string, Challenge>(
|
|
32
|
+
* 10_000,
|
|
33
|
+
* 5 * MS_PER_MINUTE,
|
|
34
|
+
* );
|
|
35
|
+
* challenges.set(id, challenge);
|
|
36
|
+
* // Later, from the response, at most once:
|
|
37
|
+
* const pending = challenges.take(id);
|
|
38
|
+
*
|
|
39
|
+
* @template K The key type.
|
|
40
|
+
* @template V The value type.
|
|
41
|
+
*/
|
|
42
|
+
export declare class TtlMap<K, V extends DefinedValue> implements IReplacingMap<K, V> {
|
|
43
|
+
readonly capacity: number;
|
|
44
|
+
readonly ttlMs: number;
|
|
45
|
+
/**
|
|
46
|
+
* Always `"fifo"`, among entries not yet expired: the entry set longest ago
|
|
47
|
+
* makes room for a new one.
|
|
48
|
+
*/
|
|
49
|
+
readonly overflowPolicy: ReplacementPolicy;
|
|
50
|
+
private readonly entries_;
|
|
51
|
+
private expiries;
|
|
52
|
+
private staleCount;
|
|
53
|
+
private readonly clock;
|
|
54
|
+
/**
|
|
55
|
+
* @param capacity The entries held at once, expired ones not counted.
|
|
56
|
+
* @param ttlMs How long an entry lasts when `set` is given no time of its
|
|
57
|
+
* own, in milliseconds; `Infinity` for never.
|
|
58
|
+
* @throws {RangeError} When `capacity` is not a non-negative integer, or
|
|
59
|
+
* `ttlMs` is not positive.
|
|
60
|
+
*/
|
|
61
|
+
constructor(capacity: number, ttlMs: number, options?: TtlMapOptions);
|
|
62
|
+
/** In set order, the oldest first. */
|
|
63
|
+
[Symbol.iterator](): Iterator<readonly [K, V]>;
|
|
64
|
+
/** The entries not yet expired. */
|
|
65
|
+
count(): number;
|
|
66
|
+
clear(): void;
|
|
67
|
+
/** O(1). Reading does not extend an entry's life. */
|
|
68
|
+
get(key: K): V | undefined;
|
|
69
|
+
/** O(1). */
|
|
70
|
+
has(key: K): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Holds `value` under `key` for `ttlMs`, replacing any entry of `key` and
|
|
73
|
+
* restarting its time.
|
|
74
|
+
*
|
|
75
|
+
* @param ttlMs How long this entry lasts, `Infinity` for never; the map's
|
|
76
|
+
* `ttlMs` when omitted.
|
|
77
|
+
* @returns The entry evicted to make room, in a one-element array, or an
|
|
78
|
+
* empty array when nothing was. With `capacity` 0, the incoming entry.
|
|
79
|
+
* @throws {RangeError} When `ttlMs` is not positive.
|
|
80
|
+
*/
|
|
81
|
+
set(key: K, value: V, ttlMs?: number): readonly (readonly [K, V])[];
|
|
82
|
+
/**
|
|
83
|
+
* The value under `key`, removed so that it is never taken twice: for a
|
|
84
|
+
* one-time challenge or code. O(1).
|
|
85
|
+
*
|
|
86
|
+
* @returns `undefined` when none is held, or it expired.
|
|
87
|
+
*/
|
|
88
|
+
take(key: K): V | undefined;
|
|
89
|
+
/**
|
|
90
|
+
* O(1).
|
|
91
|
+
*
|
|
92
|
+
* @returns Whether an entry not yet expired was held.
|
|
93
|
+
*/
|
|
94
|
+
delete(key: K): boolean;
|
|
95
|
+
/** In set order, the oldest first. */
|
|
96
|
+
keys(): IterableIterator<K>;
|
|
97
|
+
/** In set order, the oldest first. */
|
|
98
|
+
values(): IterableIterator<V>;
|
|
99
|
+
/**
|
|
100
|
+
* In set order, the oldest first: the entries not expired when iteration
|
|
101
|
+
* started.
|
|
102
|
+
*/
|
|
103
|
+
entries(): IterableIterator<readonly [K, V]>;
|
|
104
|
+
private findLive;
|
|
105
|
+
private removeEntry;
|
|
106
|
+
private dropExpired;
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=ttl-map.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ttl-map.d.ts","sourceRoot":"","sources":["../../src/cache/ttl-map.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,KAAK,EAAE,KAAK,YAAY,EAAgB,MAAM,cAAc,CAAC;AAE3E,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAErE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAE9D,mCAAmC;AACnC,MAAM,MAAM,aAAa,GAAG;IA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CAQ1B;IAED;;;;OAIG;IACH,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,OAAO,CAKtB;IAED,sCAAsC;IACrC,IAAI,IAAI,gBAAgB,CAAC,CAAC,CAAC,CAI3B;IAED,sCAAsC;IACrC,MAAM,IAAI,gBAAgB,CAAC,CAAC,CAAC,CAI7B;IAED;;;OAGG;IACF,OAAO,IAAI,gBAAgB,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAK5C;IAGD,OAAO,CAAC,QAAQ;IAchB,OAAO,CAAC,WAAW;IAgBnB,OAAO,CAAC,WAAW;CAenB"}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { SYSTEM_CLOCK } from "@ac-kit/core";
|
|
2
|
+
import { BinaryHeap } from "../heap/binary-heap.js";
|
|
3
|
+
const NOTHING_DISPLACED_ = [];
|
|
4
|
+
// Stale heap nodes tolerated before the heap is rebuilt from the live entries,
|
|
5
|
+
// beyond one per live entry: keeps small maps from rebuilding on every delete.
|
|
6
|
+
const MIN_STALE_BEFORE_REBUILD_ = 32;
|
|
7
|
+
function expiresFirst_(a, b) {
|
|
8
|
+
return a.expiresAt < b.expiresAt;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A bounded map whose entries expire a set time after they were set: a store of
|
|
12
|
+
* one-time challenges, nonces, codes, or anything stale after a while. Reach
|
|
13
|
+
* for `LruMap` instead when entries never go stale and only room matters.
|
|
14
|
+
*
|
|
15
|
+
* An expired entry is gone: `get`, `has`, `take`, `count` and iteration never
|
|
16
|
+
* see it, and it is dropped when met, so nothing runs in the background. Time
|
|
17
|
+
* comes from the injected clock, so tests need no timers. When full, setting a
|
|
18
|
+
* new key drops the expired entries, then the entry set longest ago (`"fifo"`)
|
|
19
|
+
* if still full, so a flood of new keys costs memory up to `capacity` only.
|
|
20
|
+
* Overwriting a held key never evicts.
|
|
21
|
+
*
|
|
22
|
+
* Time: `get`, `has`, `take` and `delete` are O(1); `set` is O(log n)
|
|
23
|
+
* amortised, n the entries held; `count` and iteration are O(1) and O(n) plus
|
|
24
|
+
* O(log n) per expired entry they drop. Memory is O(capacity).
|
|
25
|
+
*
|
|
26
|
+
* Keys are compared by `SameValueZero`, native `Map` semantics.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* const challenges = new TtlMap<string, Challenge>(
|
|
30
|
+
* 10_000,
|
|
31
|
+
* 5 * MS_PER_MINUTE,
|
|
32
|
+
* );
|
|
33
|
+
* challenges.set(id, challenge);
|
|
34
|
+
* // Later, from the response, at most once:
|
|
35
|
+
* const pending = challenges.take(id);
|
|
36
|
+
*
|
|
37
|
+
* @template K The key type.
|
|
38
|
+
* @template V The value type.
|
|
39
|
+
*/
|
|
40
|
+
export class TtlMap {
|
|
41
|
+
capacity;
|
|
42
|
+
ttlMs;
|
|
43
|
+
/**
|
|
44
|
+
* Always `"fifo"`, among entries not yet expired: the entry set longest ago
|
|
45
|
+
* makes room for a new one.
|
|
46
|
+
*/
|
|
47
|
+
overflowPolicy = "fifo";
|
|
48
|
+
// In set order, the oldest first, which eviction relies on.
|
|
49
|
+
entries_ = new Map();
|
|
50
|
+
// Every live entry by expiry, plus stale nodes of replaced or removed ones.
|
|
51
|
+
expiries = new BinaryHeap(expiresFirst_);
|
|
52
|
+
staleCount = 0;
|
|
53
|
+
clock;
|
|
54
|
+
/**
|
|
55
|
+
* @param capacity The entries held at once, expired ones not counted.
|
|
56
|
+
* @param ttlMs How long an entry lasts when `set` is given no time of its
|
|
57
|
+
* own, in milliseconds; `Infinity` for never.
|
|
58
|
+
* @throws {RangeError} When `capacity` is not a non-negative integer, or
|
|
59
|
+
* `ttlMs` is not positive.
|
|
60
|
+
*/
|
|
61
|
+
constructor(capacity, ttlMs, options) {
|
|
62
|
+
this.capacity = capacity;
|
|
63
|
+
this.ttlMs = ttlMs;
|
|
64
|
+
if (!Number.isInteger(capacity) || capacity < 0) {
|
|
65
|
+
throw new RangeError(`Capacity must be a non-negative integer, got ${capacity}`);
|
|
66
|
+
}
|
|
67
|
+
assertTtl_(ttlMs);
|
|
68
|
+
this.clock = options?.clock ?? SYSTEM_CLOCK;
|
|
69
|
+
}
|
|
70
|
+
/** In set order, the oldest first. */
|
|
71
|
+
[Symbol.iterator]() {
|
|
72
|
+
return this.entries();
|
|
73
|
+
}
|
|
74
|
+
/** The entries not yet expired. */
|
|
75
|
+
count() {
|
|
76
|
+
this.dropExpired(this.clock.now());
|
|
77
|
+
return this.entries_.size;
|
|
78
|
+
}
|
|
79
|
+
clear() {
|
|
80
|
+
this.entries_.clear();
|
|
81
|
+
this.expiries.clear();
|
|
82
|
+
this.staleCount = 0;
|
|
83
|
+
}
|
|
84
|
+
/** O(1). Reading does not extend an entry's life. */
|
|
85
|
+
get(key) {
|
|
86
|
+
return this.findLive(key)?.value;
|
|
87
|
+
}
|
|
88
|
+
/** O(1). */
|
|
89
|
+
has(key) {
|
|
90
|
+
return this.findLive(key) !== undefined;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Holds `value` under `key` for `ttlMs`, replacing any entry of `key` and
|
|
94
|
+
* restarting its time.
|
|
95
|
+
*
|
|
96
|
+
* @param ttlMs How long this entry lasts, `Infinity` for never; the map's
|
|
97
|
+
* `ttlMs` when omitted.
|
|
98
|
+
* @returns The entry evicted to make room, in a one-element array, or an
|
|
99
|
+
* empty array when nothing was. With `capacity` 0, the incoming entry.
|
|
100
|
+
* @throws {RangeError} When `ttlMs` is not positive.
|
|
101
|
+
*/
|
|
102
|
+
set(key, value, ttlMs = this.ttlMs) {
|
|
103
|
+
assertTtl_(ttlMs);
|
|
104
|
+
if (this.capacity === 0) {
|
|
105
|
+
return [[key, value]];
|
|
106
|
+
}
|
|
107
|
+
const now = this.clock.now();
|
|
108
|
+
this.removeEntry(key);
|
|
109
|
+
this.dropExpired(now);
|
|
110
|
+
let displaced = NOTHING_DISPLACED_;
|
|
111
|
+
if (this.entries_.size >= this.capacity) {
|
|
112
|
+
// Non-null: the map is full, and its capacity is not 0.
|
|
113
|
+
const oldest = this.entries_.values().next().value;
|
|
114
|
+
this.removeEntry(oldest.key);
|
|
115
|
+
displaced = [[oldest.key, oldest.value]];
|
|
116
|
+
}
|
|
117
|
+
const entry = { key, value, expiresAt: now + ttlMs };
|
|
118
|
+
this.entries_.set(key, entry);
|
|
119
|
+
this.expiries.insert(entry);
|
|
120
|
+
return displaced;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The value under `key`, removed so that it is never taken twice: for a
|
|
124
|
+
* one-time challenge or code. O(1).
|
|
125
|
+
*
|
|
126
|
+
* @returns `undefined` when none is held, or it expired.
|
|
127
|
+
*/
|
|
128
|
+
take(key) {
|
|
129
|
+
const entry = this.findLive(key);
|
|
130
|
+
if (entry === undefined) {
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
this.removeEntry(key);
|
|
134
|
+
return entry.value;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* O(1).
|
|
138
|
+
*
|
|
139
|
+
* @returns Whether an entry not yet expired was held.
|
|
140
|
+
*/
|
|
141
|
+
delete(key) {
|
|
142
|
+
const isHeld = this.findLive(key) !== undefined;
|
|
143
|
+
this.removeEntry(key);
|
|
144
|
+
return isHeld;
|
|
145
|
+
}
|
|
146
|
+
/** In set order, the oldest first. */
|
|
147
|
+
*keys() {
|
|
148
|
+
for (const [key] of this.entries()) {
|
|
149
|
+
yield key;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/** In set order, the oldest first. */
|
|
153
|
+
*values() {
|
|
154
|
+
for (const [, value] of this.entries()) {
|
|
155
|
+
yield value;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* In set order, the oldest first: the entries not expired when iteration
|
|
160
|
+
* started.
|
|
161
|
+
*/
|
|
162
|
+
*entries() {
|
|
163
|
+
this.dropExpired(this.clock.now());
|
|
164
|
+
for (const entry of this.entries_.values()) {
|
|
165
|
+
yield [entry.key, entry.value];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// The entry under `key` if not expired; an expired one is removed.
|
|
169
|
+
findLive(key) {
|
|
170
|
+
const entry = this.entries_.get(key);
|
|
171
|
+
if (entry === undefined) {
|
|
172
|
+
return undefined;
|
|
173
|
+
}
|
|
174
|
+
if (entry.expiresAt <= this.clock.now()) {
|
|
175
|
+
this.removeEntry(key);
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
return entry;
|
|
179
|
+
}
|
|
180
|
+
// Removes `key`'s entry, leaving its heap node stale.
|
|
181
|
+
removeEntry(key) {
|
|
182
|
+
if (!this.entries_.delete(key)) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
this.staleCount++;
|
|
186
|
+
if (this.staleCount > MIN_STALE_BEFORE_REBUILD_ &&
|
|
187
|
+
this.staleCount > this.entries_.size) {
|
|
188
|
+
// O(n), paid once per more than n removals: amortised O(1).
|
|
189
|
+
this.expiries = new BinaryHeap(expiresFirst_, this.entries_.values());
|
|
190
|
+
this.staleCount = 0;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// Pops the heap while its first node is stale or expired by `now`.
|
|
194
|
+
dropExpired(now) {
|
|
195
|
+
for (let first = this.expiries.peek(); first !== undefined &&
|
|
196
|
+
(first.expiresAt <= now || this.entries_.get(first.key) !== first); first = this.expiries.peek()) {
|
|
197
|
+
this.expiries.extract();
|
|
198
|
+
if (this.entries_.get(first.key) === first) {
|
|
199
|
+
this.entries_.delete(first.key);
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
this.staleCount--;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function assertTtl_(ttlMs) {
|
|
208
|
+
if (!(ttlMs > 0)) {
|
|
209
|
+
throw new RangeError(`A time to live must be positive, got ${ttlMs}`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
//# sourceMappingURL=tt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QAAQ,CAAC,IAAI,EAAE,EAChC,KAAK,KAAK,SAAS;YACnB,CAAC,KAAK,CAAC,SAAS,IAAI,GAAG,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,KAAK,CAAC,EAClE,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,EAC3B,CAAC;YACF,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC;YACxB,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,KAAK,EAAE,CAAC;gBAC5C,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YACjC,CAAC;iBAAM,CAAC;gBACP,IAAI,CAAC,UAAU,EAAE,CAAC;YACnB,CAAC;QACF,CAAC;IACF,CAAC;CACD;AAED,SAAS,UAAU,CAAC,KAAa;IAChC,IAAI,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE,CAAC;QAClB,MAAM,IAAI,UAAU,CAAC,wCAAwC,KAAK,EAAE,CAAC,CAAC;IACvE,CAAC;AACF,CAAC"}
|
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IBounded } from "./ibounded.js";
|
|
2
2
|
export declare function isBounded(value: unknown): value is IBounded;
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* Deliberately not generic over the storage: `BoundedOptions<TStorage>` is
|
|
7
|
-
* invariant in `TStorage` (it occurs both as a property and inside an
|
|
8
|
-
* intersection), so every caller would have to widen at the call site.
|
|
4
|
+
* Validates the capacity a bounded collection was constructed with.
|
|
9
5
|
*
|
|
10
6
|
* @param itemCount How many items the initial iterable held.
|
|
7
|
+
* @param storage The storage supplied in the options, if any.
|
|
11
8
|
* @throws {RangeError} If the capacity is not a finite, non-negative number,
|
|
12
9
|
* exceeds what the supplied storage can hold, or is already smaller than the
|
|
13
10
|
* initial iterable.
|
|
14
11
|
*/
|
|
15
|
-
export declare function resolveBoundedCapacity(itemCount: number,
|
|
12
|
+
export declare function resolveBoundedCapacity(capacity: number, itemCount: number, storage?: unknown): number;
|
|
16
13
|
/**
|
|
17
14
|
* The capacity check every `Bounded*` collection repeats before adding items.
|
|
18
15
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"_bounded.d.ts","sourceRoot":"","sources":["../../src/collection/_bounded.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"_bounded.d.ts","sourceRoot":"","sources":["../../src/collection/_bounded.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmC,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE1E,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,QAAQ,CAE3D;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CACrC,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,EACjB,OAAO,CAAC,EAAE,OAAO,GACf,MAAM,CAoBR;AAED;;;;;;;;;GASG;AACH,wBAAgB,aAAa,CAC5B,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,GACd,IAAI,CAIN"}
|
|
@@ -1,25 +1,19 @@
|
|
|
1
|
-
import { CollectionCapacityExceededError
|
|
1
|
+
import { CollectionCapacityExceededError } from "./ibounded.js";
|
|
2
2
|
export function isBounded(value) {
|
|
3
3
|
return typeof value === "object" && value !== null && "capacity" in value;
|
|
4
4
|
}
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* Deliberately not generic over the storage: `BoundedOptions<TStorage>` is
|
|
9
|
-
* invariant in `TStorage` (it occurs both as a property and inside an
|
|
10
|
-
* intersection), so every caller would have to widen at the call site.
|
|
6
|
+
* Validates the capacity a bounded collection was constructed with.
|
|
11
7
|
*
|
|
12
8
|
* @param itemCount How many items the initial iterable held.
|
|
9
|
+
* @param storage The storage supplied in the options, if any.
|
|
13
10
|
* @throws {RangeError} If the capacity is not a finite, non-negative number,
|
|
14
11
|
* exceeds what the supplied storage can hold, or is already smaller than the
|
|
15
12
|
* initial iterable.
|
|
16
13
|
*/
|
|
17
|
-
export function resolveBoundedCapacity(itemCount,
|
|
18
|
-
const storageCapacity = isBounded(
|
|
19
|
-
|
|
20
|
-
: undefined;
|
|
21
|
-
const capacity = options?.capacity ?? storageCapacity;
|
|
22
|
-
if (capacity === undefined || !Number.isFinite(capacity) || capacity < 0) {
|
|
14
|
+
export function resolveBoundedCapacity(capacity, itemCount, storage) {
|
|
15
|
+
const storageCapacity = isBounded(storage) ? storage.capacity : undefined;
|
|
16
|
+
if (!Number.isFinite(capacity) || capacity < 0) {
|
|
23
17
|
throw new RangeError(`Capacity must be a non-negative, finite number, got ${capacity}`);
|
|
24
18
|
}
|
|
25
19
|
if (storageCapacity !== undefined && capacity > storageCapacity) {
|