@bodil/core 0.3.1 → 0.4.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/dist/async/abortable-promise.d.ts +71 -0
- package/dist/async/abortable-promise.js +204 -0
- package/dist/async/abortable-promise.js.map +1 -0
- package/dist/async/abortable-promise.test.d.ts +1 -0
- package/dist/async/abortable-promise.test.js +134 -0
- package/dist/async/abortable-promise.test.js.map +1 -0
- package/dist/async.d.ts +20 -13
- package/dist/async.js +45 -21
- package/dist/async.js.map +1 -1
- package/dist/collections/lfu-cache.d.ts +45 -0
- package/dist/collections/lfu-cache.js +107 -0
- package/dist/collections/lfu-cache.js.map +1 -0
- package/dist/collections/lfu-cache.test.d.ts +1 -0
- package/dist/collections/lfu-cache.test.js +29 -0
- package/dist/collections/lfu-cache.test.js.map +1 -0
- package/dist/collections/linked-list.d.ts +41 -0
- package/dist/collections/linked-list.js +126 -0
- package/dist/collections/linked-list.js.map +1 -0
- package/dist/collections/lru-cache.d.ts +42 -0
- package/dist/collections/lru-cache.js +84 -0
- package/dist/collections/lru-cache.js.map +1 -0
- package/dist/collections/lru-cache.test.d.ts +1 -0
- package/dist/collections/lru-cache.test.js +27 -0
- package/dist/collections/lru-cache.test.js.map +1 -0
- package/dist/collections.d.ts +7 -0
- package/dist/collections.js +8 -0
- package/dist/collections.js.map +1 -0
- package/dist/disposable.d.ts +120 -0
- package/dist/disposable.js +219 -0
- package/dist/disposable.js.map +1 -0
- package/dist/fun.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/package.json +27 -11
- package/src/async/abortable-promise.test.ts +179 -0
- package/src/async/abortable-promise.ts +280 -0
- package/src/async.ts +56 -35
- package/src/collections/lfu-cache.test.ts +31 -0
- package/src/collections/lfu-cache.ts +141 -0
- package/src/collections/linked-list.ts +131 -0
- package/src/collections/lru-cache.test.ts +29 -0
- package/src/collections/lru-cache.ts +109 -0
- package/src/collections.ts +8 -0
- package/src/disposable.ts +285 -0
- package/src/fun.ts +0 -2
- package/src/index.ts +2 -1
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { None, Some } from "@bodil/opt";
|
|
2
|
+
import { LinkedList } from "./linked-list";
|
|
3
|
+
import { assert } from "@bodil/core/assert";
|
|
4
|
+
import { isDisposable } from "../disposable";
|
|
5
|
+
function takeOne(it) {
|
|
6
|
+
if (it === undefined) {
|
|
7
|
+
return None;
|
|
8
|
+
}
|
|
9
|
+
const result = it.next();
|
|
10
|
+
return result.done === true ? None : Some(result.value);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A Least Frequently Used cache.
|
|
14
|
+
*
|
|
15
|
+
* When the cache needs to drop a value to stay within its bounds, it chooses
|
|
16
|
+
* the value that has the lowest access count.
|
|
17
|
+
*/
|
|
18
|
+
export class LFUCache {
|
|
19
|
+
#index = new Map();
|
|
20
|
+
#list = new LinkedList();
|
|
21
|
+
#options;
|
|
22
|
+
constructor(options = {}) {
|
|
23
|
+
this.#options = options;
|
|
24
|
+
}
|
|
25
|
+
[Symbol.dispose]() {
|
|
26
|
+
for (const record of this.#index.values()) {
|
|
27
|
+
this.#disposeValue(record.value);
|
|
28
|
+
}
|
|
29
|
+
this.#index.clear();
|
|
30
|
+
this.#list = new LinkedList();
|
|
31
|
+
}
|
|
32
|
+
#disposeValue(value) {
|
|
33
|
+
if (this.#options.disposeFunction !== undefined) {
|
|
34
|
+
this.#options.disposeFunction(value);
|
|
35
|
+
}
|
|
36
|
+
else if (isDisposable(value)) {
|
|
37
|
+
value[Symbol.dispose]();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Get the current size of the cache.
|
|
42
|
+
*/
|
|
43
|
+
get size() {
|
|
44
|
+
return this.#index.size;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Test if the cache contains a given key.
|
|
48
|
+
*
|
|
49
|
+
* This does not count as an access towards cache invalidation.
|
|
50
|
+
*/
|
|
51
|
+
has(key) {
|
|
52
|
+
return this.#index.has(key);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Get the value associated with the given key, if it exists in the cache.
|
|
56
|
+
*/
|
|
57
|
+
get(key) {
|
|
58
|
+
const record = this.#index.get(key);
|
|
59
|
+
if (record === undefined) {
|
|
60
|
+
return None;
|
|
61
|
+
}
|
|
62
|
+
const node = record.parent;
|
|
63
|
+
let nextNode = node.next;
|
|
64
|
+
if (nextNode === undefined || nextNode.freq !== node.freq + 1) {
|
|
65
|
+
nextNode = this.#list.insert({ freq: node.freq + 1, items: new Set() }, node, nextNode);
|
|
66
|
+
}
|
|
67
|
+
nextNode.items.add(key);
|
|
68
|
+
record.parent = nextNode;
|
|
69
|
+
node.items.delete(key);
|
|
70
|
+
if (node.items.size === 0) {
|
|
71
|
+
this.#list.remove(node);
|
|
72
|
+
}
|
|
73
|
+
return Some(record.value);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Add a key/value pair to the cache.
|
|
77
|
+
*/
|
|
78
|
+
set(key, value) {
|
|
79
|
+
let record = this.#index.get(key);
|
|
80
|
+
if (record !== undefined) {
|
|
81
|
+
if (record.value !== value) {
|
|
82
|
+
this.#disposeValue(record.value);
|
|
83
|
+
record.value = value;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
if (this.#index.size >= (this.#options.maxLength ?? 256)) {
|
|
88
|
+
const head = this.#list.head;
|
|
89
|
+
takeOne(head?.items.keys()).ifSome((key) => {
|
|
90
|
+
const record = this.#index.get(key);
|
|
91
|
+
assert(record !== undefined);
|
|
92
|
+
this.#disposeValue(record.value);
|
|
93
|
+
head.items.delete(key);
|
|
94
|
+
this.#index.delete(key);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
let node = this.#list.head;
|
|
98
|
+
if (node === undefined || node.freq !== 1) {
|
|
99
|
+
node = this.#list.insert({ freq: 1, items: new Set() }, undefined, node);
|
|
100
|
+
}
|
|
101
|
+
record = { value, parent: node };
|
|
102
|
+
node.items.add(key);
|
|
103
|
+
this.#index.set(key, record);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=lfu-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lfu-cache.js","sourceRoot":"","sources":["../../src/collections/lfu-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,IAAI,EAAe,MAAM,YAAY,CAAC;AAErD,OAAO,EAAE,UAAU,EAAiB,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AA4B7C,SAAS,OAAO,CAAI,EAAgB;IAChC,IAAI,EAAE,KAAK,SAAS,EAAE,CAAC;QACnB,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,MAAM,MAAM,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC;IACzB,OAAO,MAAM,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC5D,CAAC;AAED;;;;;GAKG;AACH,MAAM,OAAO,QAAQ;IACR,MAAM,GAAG,IAAI,GAAG,EAAsB,CAAC;IAChD,KAAK,GAAG,IAAI,UAAU,EAAc,CAAC;IAC5B,QAAQ,CAAqB;IAEtC,YAAY,UAA8B,EAAE;QACxC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC5B,CAAC;IAED,CAAC,MAAM,CAAC,OAAO,CAAC;QACZ,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC;YACxC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACrC,CAAC;QACD,IAAI,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AAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBAC5B,CAAC,CAAC,CAAC;YACP,CAAC;YAED,IAAI,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;YAC3B,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;gBACxC,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,IAAI,GAAG,EAAE,EAAE,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;YAC7E,CAAC;YACD,MAAM,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;YACjC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YACpB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QACjC,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "@bodil/opt-vitest";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
import { LFUCache } from "./lfu-cache";
|
|
3
|
+
import "@bodil/opt-vitest";
|
|
4
|
+
test("LFUCache", () => {
|
|
5
|
+
const evicted = [];
|
|
6
|
+
const cache = new LFUCache({
|
|
7
|
+
maxLength: 3,
|
|
8
|
+
disposeFunction: (value) => evicted.push(value),
|
|
9
|
+
});
|
|
10
|
+
expect(cache.size).equal(0);
|
|
11
|
+
cache.set("foo", "FOO");
|
|
12
|
+
cache.set("bar", "BAR");
|
|
13
|
+
cache.set("baz", "BAZ");
|
|
14
|
+
expect(cache.size).equal(3);
|
|
15
|
+
expect(cache.get("foo")).isSome("FOO");
|
|
16
|
+
expect(cache.get("bar")).isSome("BAR");
|
|
17
|
+
expect(cache.get("baz")).isSome("BAZ");
|
|
18
|
+
expect(cache.get("foo")).isSome("FOO");
|
|
19
|
+
expect(cache.get("bar")).isSome("BAR");
|
|
20
|
+
expect(cache.get("wibble")).isNone();
|
|
21
|
+
cache.set("wibble", "WIBBLE");
|
|
22
|
+
expect(cache.size).equal(3);
|
|
23
|
+
expect(cache.get("wibble")).isSome("WIBBLE");
|
|
24
|
+
expect(cache.get("foo")).isSome("FOO");
|
|
25
|
+
expect(cache.get("bar")).isSome("BAR");
|
|
26
|
+
expect(cache.get("baz")).isNone();
|
|
27
|
+
expect(evicted).deep.equal(["BAZ"]);
|
|
28
|
+
});
|
|
29
|
+
//# sourceMappingURL=lfu-cache.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lfu-cache.test.js","sourceRoot":"","sources":["../../src/collections/lfu-cache.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEtC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,mBAAmB,CAAC;AAE3B,IAAI,CAAC,UAAU,EAAE,GAAG,EAAE;IAClB,MAAM,OAAO,GAAkB,EAAE,CAAC;IAClC,MAAM,KAAK,GAAG,IAAI,QAAQ,CAAiB;QACvC,SAAS,EAAE,CAAC;QACZ,eAAe,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC;KAClD,CAAC,CAAC;IACH,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC5B,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACxB,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACxB,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACxB,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC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|
|
1
|
+
export interface ListNode {
|
|
2
|
+
prev?: this;
|
|
3
|
+
next?: this;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* A doubly linked list.
|
|
7
|
+
*
|
|
8
|
+
* You never know when you're going to need one.
|
|
9
|
+
*/
|
|
10
|
+
export declare class LinkedList<Node extends ListNode> implements Iterable<Node> {
|
|
11
|
+
head?: Node;
|
|
12
|
+
tail?: Node;
|
|
13
|
+
/**
|
|
14
|
+
* Remove a node from the list.
|
|
15
|
+
*/
|
|
16
|
+
remove(node: Node): void;
|
|
17
|
+
/**
|
|
18
|
+
* Insert a node into the list between the given nodes.
|
|
19
|
+
*
|
|
20
|
+
* It goes without saying that the `prev`/`next` nodes must be adjacent to
|
|
21
|
+
* each other in the list, or exceptions will be thrown.
|
|
22
|
+
*/
|
|
23
|
+
insert(node: Node, prev?: Node, next?: Node): Node;
|
|
24
|
+
/**
|
|
25
|
+
* Insert a node at the head of the list.
|
|
26
|
+
*/
|
|
27
|
+
pushHead(node: Node): void;
|
|
28
|
+
/**
|
|
29
|
+
* Insert a node at the tail of the list.
|
|
30
|
+
*/
|
|
31
|
+
pushTail(node: Node): void;
|
|
32
|
+
/**
|
|
33
|
+
* Remove the node at the head of the list and return it.
|
|
34
|
+
*/
|
|
35
|
+
popHead(): Node | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Remove the node at the tail of the list and return it.
|
|
38
|
+
*/
|
|
39
|
+
popTail(): Node | undefined;
|
|
40
|
+
[Symbol.iterator]: (this: LinkedList<Node>) => Generator<Node>;
|
|
41
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { assert } from "@bodil/core/assert";
|
|
3
|
+
/**
|
|
4
|
+
* A doubly linked list.
|
|
5
|
+
*
|
|
6
|
+
* You never know when you're going to need one.
|
|
7
|
+
*/
|
|
8
|
+
export class LinkedList {
|
|
9
|
+
constructor() {
|
|
10
|
+
this[_a] = function* () {
|
|
11
|
+
let node = this.head;
|
|
12
|
+
while (node !== undefined) {
|
|
13
|
+
yield node;
|
|
14
|
+
node = node.next;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
static { _a = Symbol.iterator; }
|
|
19
|
+
/**
|
|
20
|
+
* Remove a node from the list.
|
|
21
|
+
*/
|
|
22
|
+
remove(node) {
|
|
23
|
+
if (node.prev !== undefined) {
|
|
24
|
+
node.prev.next = node.next;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
assert(this.head === node);
|
|
28
|
+
this.head = node.next;
|
|
29
|
+
}
|
|
30
|
+
if (node.next !== undefined) {
|
|
31
|
+
node.next.prev = node.prev;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
assert(this.tail === node);
|
|
35
|
+
this.tail = node.prev;
|
|
36
|
+
}
|
|
37
|
+
node.prev = undefined;
|
|
38
|
+
node.next = undefined;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Insert a node into the list between the given nodes.
|
|
42
|
+
*
|
|
43
|
+
* It goes without saying that the `prev`/`next` nodes must be adjacent to
|
|
44
|
+
* each other in the list, or exceptions will be thrown.
|
|
45
|
+
*/
|
|
46
|
+
insert(node, prev, next) {
|
|
47
|
+
assert(node.prev === undefined && node.next === undefined);
|
|
48
|
+
node.prev = prev;
|
|
49
|
+
node.next = next;
|
|
50
|
+
if (next !== undefined) {
|
|
51
|
+
assert(next.prev === prev);
|
|
52
|
+
next.prev = node;
|
|
53
|
+
}
|
|
54
|
+
if (prev !== undefined) {
|
|
55
|
+
assert(prev.next === next);
|
|
56
|
+
prev.next = node;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
this.head = node;
|
|
60
|
+
}
|
|
61
|
+
return node;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Insert a node at the head of the list.
|
|
65
|
+
*/
|
|
66
|
+
pushHead(node) {
|
|
67
|
+
assert(node.prev === undefined && node.next === undefined);
|
|
68
|
+
if (this.head === undefined || this.tail === undefined) {
|
|
69
|
+
this.head = node;
|
|
70
|
+
this.tail = node;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
node.next = this.head;
|
|
74
|
+
this.head.prev = node;
|
|
75
|
+
this.head = node;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Insert a node at the tail of the list.
|
|
80
|
+
*/
|
|
81
|
+
pushTail(node) {
|
|
82
|
+
assert(node.prev === undefined && node.next === undefined);
|
|
83
|
+
if (this.head === undefined || this.tail === undefined) {
|
|
84
|
+
this.head = node;
|
|
85
|
+
this.tail = node;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
node.prev = this.tail;
|
|
89
|
+
this.tail.next = node;
|
|
90
|
+
this.tail = node;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Remove the node at the head of the list and return it.
|
|
95
|
+
*/
|
|
96
|
+
popHead() {
|
|
97
|
+
const node = this.head;
|
|
98
|
+
if (node === undefined) {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
this.head = node.next;
|
|
102
|
+
if (this.head === undefined) {
|
|
103
|
+
this.tail = undefined;
|
|
104
|
+
}
|
|
105
|
+
node.prev = undefined;
|
|
106
|
+
node.next = undefined;
|
|
107
|
+
return node;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Remove the node at the tail of the list and return it.
|
|
111
|
+
*/
|
|
112
|
+
popTail() {
|
|
113
|
+
const node = this.tail;
|
|
114
|
+
if (node === undefined) {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
this.tail = node.prev;
|
|
118
|
+
if (this.tail === undefined) {
|
|
119
|
+
this.head = undefined;
|
|
120
|
+
}
|
|
121
|
+
node.prev = undefined;
|
|
122
|
+
node.next = undefined;
|
|
123
|
+
return node;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL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
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type Option } from "@bodil/opt";
|
|
2
|
+
export type LRUCacheOptions<V> = {
|
|
3
|
+
/**
|
|
4
|
+
* The maximum size of the cache.
|
|
5
|
+
*/
|
|
6
|
+
maxLength?: number;
|
|
7
|
+
/**
|
|
8
|
+
* An optional dispose function to be called when a value is removed from
|
|
9
|
+
* the cache. Defaults to disposing of values which implement
|
|
10
|
+
* {@link Disposable}, and doing nothing otherwise.
|
|
11
|
+
*/
|
|
12
|
+
disposeFunction?: (value: V) => void;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* A Least Recently Used cache.
|
|
16
|
+
*
|
|
17
|
+
* When the cache needs to drop a value to stay within its bounds, it chooses
|
|
18
|
+
* the value that was accessed the least recently.
|
|
19
|
+
*/
|
|
20
|
+
export declare class LRUCache<K, V> implements Disposable {
|
|
21
|
+
#private;
|
|
22
|
+
constructor(options?: LRUCacheOptions<V>);
|
|
23
|
+
[Symbol.dispose](): void;
|
|
24
|
+
/**
|
|
25
|
+
* Get the current size of the cache.
|
|
26
|
+
*/
|
|
27
|
+
get size(): number;
|
|
28
|
+
/**
|
|
29
|
+
* Test if the cache contains a given key.
|
|
30
|
+
*
|
|
31
|
+
* This does not count as an access towards cache invalidation.
|
|
32
|
+
*/
|
|
33
|
+
has(key: K): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Get the value associated with the given key, if it exists in the cache.
|
|
36
|
+
*/
|
|
37
|
+
get(key: K): Option<V>;
|
|
38
|
+
/**
|
|
39
|
+
* Add a key/value pair to the cache.
|
|
40
|
+
*/
|
|
41
|
+
set(key: K, value: V): void;
|
|
42
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { None, Some } from "@bodil/opt";
|
|
2
|
+
import { assert } from "@bodil/core/assert";
|
|
3
|
+
import { LinkedList } from "./linked-list";
|
|
4
|
+
import { isDisposable } from "../disposable";
|
|
5
|
+
/**
|
|
6
|
+
* A Least Recently Used cache.
|
|
7
|
+
*
|
|
8
|
+
* When the cache needs to drop a value to stay within its bounds, it chooses
|
|
9
|
+
* the value that was accessed the least recently.
|
|
10
|
+
*/
|
|
11
|
+
export class LRUCache {
|
|
12
|
+
#index = new Map();
|
|
13
|
+
#list = new LinkedList();
|
|
14
|
+
#options;
|
|
15
|
+
constructor(options = {}) {
|
|
16
|
+
this.#options = options;
|
|
17
|
+
}
|
|
18
|
+
[Symbol.dispose]() {
|
|
19
|
+
for (const node of this.#index.values()) {
|
|
20
|
+
this.#disposeValue(node.value);
|
|
21
|
+
}
|
|
22
|
+
this.#index.clear();
|
|
23
|
+
this.#list = new LinkedList();
|
|
24
|
+
}
|
|
25
|
+
#disposeValue(value) {
|
|
26
|
+
if (this.#options.disposeFunction !== undefined) {
|
|
27
|
+
this.#options.disposeFunction(value);
|
|
28
|
+
}
|
|
29
|
+
else if (isDisposable(value)) {
|
|
30
|
+
value[Symbol.dispose]();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Get the current size of the cache.
|
|
35
|
+
*/
|
|
36
|
+
get size() {
|
|
37
|
+
return this.#index.size;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Test if the cache contains a given key.
|
|
41
|
+
*
|
|
42
|
+
* This does not count as an access towards cache invalidation.
|
|
43
|
+
*/
|
|
44
|
+
has(key) {
|
|
45
|
+
return this.#index.has(key);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get the value associated with the given key, if it exists in the cache.
|
|
49
|
+
*/
|
|
50
|
+
get(key) {
|
|
51
|
+
const node = this.#index.get(key);
|
|
52
|
+
if (node === undefined) {
|
|
53
|
+
return None;
|
|
54
|
+
}
|
|
55
|
+
this.#list.remove(node);
|
|
56
|
+
this.#list.pushHead(node);
|
|
57
|
+
return Some(node.value);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Add a key/value pair to the cache.
|
|
61
|
+
*/
|
|
62
|
+
set(key, value) {
|
|
63
|
+
let node = this.#index.get(key);
|
|
64
|
+
if (node !== undefined) {
|
|
65
|
+
this.#list.remove(node);
|
|
66
|
+
if (node.value !== value) {
|
|
67
|
+
this.#disposeValue(node.value);
|
|
68
|
+
node.value = value;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
node = { key, value };
|
|
73
|
+
this.#index.set(key, node);
|
|
74
|
+
}
|
|
75
|
+
this.#list.pushHead(node);
|
|
76
|
+
while (this.#index.size > (this.#options.maxLength ?? 256)) {
|
|
77
|
+
const expiredNode = this.#list.popTail();
|
|
78
|
+
assert(expiredNode !== undefined);
|
|
79
|
+
this.#index.delete(expiredNode.key);
|
|
80
|
+
this.#disposeValue(expiredNode.value);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=lru-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lru-cache.js","sourceRoot":"","sources":["../../src/collections/lru-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,IAAI,EAAe,MAAM,YAAY,CAAC;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,OAAO,EAAE,UAAU,EAAiB,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAoB7C;;;;;GAKG;AACH,MAAM,OAAO,QAAQ;IACR,MAAM,GAAG,IAAI,GAAG,EAAoB,CAAC;IAC9C,KAAK,GAAG,IAAI,UAAU,EAAiB,CAAC;IAC/B,QAAQ,CAAqB;IAEtC,YAAY,UAA8B,EAAE;QACxC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC5B,CAAC;IAED,CAAC,MAAM,CAAC,OAAO,CAAC;QACZ,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC;YACtC,IAAI,CAAC,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AAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YACxB,IAAI,IAAI,CAAC,KAAK,KAAK,KAAK,EAAE,CAAC;gBACvB,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;gBAC/B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;YACvB,CAAC;QACL,CAAC;aAAM,CAAC;YACJ,IAAI,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;YACtB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QAC/B,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAE1B,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,IAAI,GAAG,CAAC,EAAE,CAAC;YACzD,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;YACzC,MAAM,CAAC,WAAW,KAAK,SAAS,CAAC,CAAC;YAClC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;YACpC,IAAI,CAAC,aAAa,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;QAC1C,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "@bodil/opt-vitest";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
import { LRUCache } from "./lru-cache";
|
|
3
|
+
import "@bodil/opt-vitest";
|
|
4
|
+
test("LRUCache", () => {
|
|
5
|
+
const evicted = [];
|
|
6
|
+
const cache = new LRUCache({
|
|
7
|
+
maxLength: 3,
|
|
8
|
+
disposeFunction: (value) => evicted.push(value),
|
|
9
|
+
});
|
|
10
|
+
expect(cache.size).equal(0);
|
|
11
|
+
cache.set("foo", "FOO");
|
|
12
|
+
cache.set("bar", "BAR");
|
|
13
|
+
cache.set("baz", "BAZ");
|
|
14
|
+
expect(cache.size).equal(3);
|
|
15
|
+
expect(cache.get("baz")).isSome("BAZ");
|
|
16
|
+
expect(cache.get("bar")).isSome("BAR");
|
|
17
|
+
expect(cache.get("foo")).isSome("FOO");
|
|
18
|
+
expect(cache.get("wibble")).isNone();
|
|
19
|
+
cache.set("wibble", "WIBBLE");
|
|
20
|
+
expect(cache.size).equal(3);
|
|
21
|
+
expect(cache.get("wibble")).isSome("WIBBLE");
|
|
22
|
+
expect(cache.get("foo")).isSome("FOO");
|
|
23
|
+
expect(cache.get("bar")).isSome("BAR");
|
|
24
|
+
expect(cache.get("baz")).isNone();
|
|
25
|
+
expect(evicted).deep.equal(["BAZ"]);
|
|
26
|
+
});
|
|
27
|
+
//# sourceMappingURL=lru-cache.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lru-cache.test.js","sourceRoot":"","sources":["../../src/collections/lru-cache.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEtC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,mBAAmB,CAAC;AAE3B,IAAI,CAAC,UAAU,EAAE,GAAG,EAAE;IAClB,MAAM,OAAO,GAAkB,EAAE,CAAC;IAClC,MAAM,KAAK,GAAG,IAAI,QAAQ,CAAiB;QACvC,SAAS,EAAE,CAAC;QACZ,eAAe,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC;KAClD,CAAC,CAAC;IACH,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC5B,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACxB,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACxB,KAAK,C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*/
|
|
5
|
+
export { LinkedList, type ListNode } from "./collections/linked-list";
|
|
6
|
+
export { LFUCache, type LFUCacheOptions } from "./collections/lfu-cache";
|
|
7
|
+
export { LRUCache, type LRUCacheOptions } from "./collections/lru-cache";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collections.js","sourceRoot":"","sources":["../src/collections.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAiB,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAwB,MAAM,yBAAyB,CAAC;AACzE,OAAO,EAAE,QAAQ,EAAwB,MAAM,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tools for working with {@link Disposable}s and things that should have been
|
|
3
|
+
* {@link Disposable}.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import type { Constructor, Unsubscribable } from "type-fest";
|
|
8
|
+
import type { ExtractEventMap } from "@bodil/core/event";
|
|
9
|
+
/**
|
|
10
|
+
* Test whether something implements {@link Disposable}.
|
|
11
|
+
*/
|
|
12
|
+
export declare function isDisposable(value: unknown): value is Disposable;
|
|
13
|
+
/**
|
|
14
|
+
* Type for something that can be turned into a {@link Disposable}.
|
|
15
|
+
*
|
|
16
|
+
* Includes things that already implement {@link Disposable}, objects with an
|
|
17
|
+
* `unsubscribe()` method (an {@link Unsubscribable}), and functions
|
|
18
|
+
* with no arguments or return values which are assumed to be unsubscribe
|
|
19
|
+
* functions, as popularised by React.
|
|
20
|
+
*/
|
|
21
|
+
export type Disposifiable = (() => void) | Unsubscribable | Disposable;
|
|
22
|
+
/**
|
|
23
|
+
* Construct a new {@link Disposable} out of any {@link Disposifiable}.
|
|
24
|
+
*
|
|
25
|
+
* This constructs a new object which implements {@link Disposable}, and nothing
|
|
26
|
+
* else. The new object's dispose method will dispose of the original
|
|
27
|
+
* {@link Disposifiable}, and ensure that this happens only once, regardless of
|
|
28
|
+
* how many times the dispose method is called.
|
|
29
|
+
*/
|
|
30
|
+
export declare function toDisposable(value: Disposifiable): Disposable;
|
|
31
|
+
/**
|
|
32
|
+
* Attach an event listener to an {@link EventTarget} and return a
|
|
33
|
+
* {@link Disposable} which will remove the listener when disposed.
|
|
34
|
+
*/
|
|
35
|
+
export declare function eventListener<T extends EventTarget, M extends ExtractEventMap<T>, K extends keyof M & string>(target: T, type: K, callback: (e: M[K]) => any, options?: AddEventListenerOptions | boolean): Disposable;
|
|
36
|
+
/**
|
|
37
|
+
* Call {@link setTimeout} with the provided callback and return a
|
|
38
|
+
* {@link Disposable} which will cancel the timeout when disposed.
|
|
39
|
+
*/
|
|
40
|
+
export declare function timeout(fn: () => void, timeout: number | Temporal.Duration): Disposable;
|
|
41
|
+
/**
|
|
42
|
+
* Call {@link setInterval} with the provided callback and return a
|
|
43
|
+
* {@link Disposable} which will cancel the interval when disposed.
|
|
44
|
+
*/
|
|
45
|
+
export declare function interval(fn: () => void, interval: number | Temporal.Duration): Disposable;
|
|
46
|
+
/**
|
|
47
|
+
* Call {@link window.requestAnimationFrame} with the provided callback and return a
|
|
48
|
+
* {@link Disposable} which will cancel the request when disposed.
|
|
49
|
+
*/
|
|
50
|
+
export declare function animationFrame(fn: (time: number) => void): Disposable;
|
|
51
|
+
export declare function idleCallback(callback: IdleRequestCallback, options?: IdleRequestOptions): Disposable;
|
|
52
|
+
/**
|
|
53
|
+
* An implementation of {@link AbortController} which also implements
|
|
54
|
+
* {@link Disposable}.
|
|
55
|
+
*
|
|
56
|
+
* Disposing the controller automatically triggers the abort signal.
|
|
57
|
+
*/
|
|
58
|
+
export declare class DisposableAbortController implements AbortController, Disposable {
|
|
59
|
+
#private;
|
|
60
|
+
get signal(): AbortSignal;
|
|
61
|
+
abort(reason?: any): void;
|
|
62
|
+
[Symbol.dispose](): void;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Create an {@link AbortController} which also implements {@link Disposable}.
|
|
66
|
+
*
|
|
67
|
+
* Disposing the controller automatically triggers the abort signal.
|
|
68
|
+
*
|
|
69
|
+
* @returns A {@link DisposableAbortController}.
|
|
70
|
+
*/
|
|
71
|
+
export declare function abortController(): DisposableAbortController;
|
|
72
|
+
/**
|
|
73
|
+
* A reentrant version of {@link DisposableStack}.
|
|
74
|
+
*
|
|
75
|
+
* This maintains a list of {@link Disposable}s, added with the
|
|
76
|
+
* {@link DisposableContext.use} method, which will all be disposed when you
|
|
77
|
+
* dispose of the {@link DisposableContext} itself.
|
|
78
|
+
*
|
|
79
|
+
* Where this differs from {@link DisposableStack} is that disposing it only
|
|
80
|
+
* resets it after disposing of its dependents, and you can reuse it as many
|
|
81
|
+
* times as you like. This is very handy for managing lifecycle resources, eg. a
|
|
82
|
+
* web component can allocate resources in its `connectedCallback` method and
|
|
83
|
+
* during its connected lifecycle phase which can all be disposed in
|
|
84
|
+
* `disconnectedCallback` with a single call to
|
|
85
|
+
* {@link DisposableContext.dispose}, and it's instantly ready to go again on
|
|
86
|
+
* reconnect.
|
|
87
|
+
*/
|
|
88
|
+
export declare class DisposableContext implements Disposable {
|
|
89
|
+
#private;
|
|
90
|
+
dispose: () => void;
|
|
91
|
+
[Symbol.dispose](): void;
|
|
92
|
+
/**
|
|
93
|
+
* Register a {@link Disposifiable} resource with this context, to be
|
|
94
|
+
* disposed along with it.
|
|
95
|
+
*
|
|
96
|
+
* @returns The {@link Disposable} that was registered with the context.
|
|
97
|
+
*/
|
|
98
|
+
use(resource: null): null;
|
|
99
|
+
use(resource: undefined): undefined;
|
|
100
|
+
use(resource: Disposifiable): Disposable;
|
|
101
|
+
use(resource: Disposifiable | undefined): Disposable | undefined;
|
|
102
|
+
use(resource: Disposifiable | null | undefined): Disposable | null | undefined;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* A class mixin which provides a {@link Disposable} implementation.
|
|
106
|
+
*
|
|
107
|
+
* The `use(resource)` method can be used to register {@link Disposifiable}
|
|
108
|
+
* resources which will be disposed when the object's dispose method is called.
|
|
109
|
+
*/
|
|
110
|
+
export declare const AutoDisposable: <C extends Constructor<object>>(superclass: C) => {
|
|
111
|
+
new (...arguments_: any[]): {
|
|
112
|
+
#_context?: DisposableContext;
|
|
113
|
+
use(resource: null): null;
|
|
114
|
+
use(resource: undefined): undefined;
|
|
115
|
+
use(resource: Disposifiable): Disposable;
|
|
116
|
+
use(resource: Disposifiable | undefined): Disposable | undefined;
|
|
117
|
+
use(resource: Disposifiable | null | undefined): Disposable | null | undefined;
|
|
118
|
+
[Symbol.dispose](): void;
|
|
119
|
+
};
|
|
120
|
+
} & C;
|