@bodil/core 0.5.4 → 0.5.6

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 (73) hide show
  1. package/dist/assert.d.ts +93 -0
  2. package/dist/assert.js +217 -0
  3. package/dist/assert.js.map +1 -0
  4. package/dist/assert.test.d.ts +1 -0
  5. package/dist/assert.test.js +51 -0
  6. package/dist/assert.test.js.map +1 -0
  7. package/dist/async/abortable-promise.d.ts +71 -0
  8. package/dist/async/abortable-promise.js +203 -0
  9. package/dist/async/abortable-promise.js.map +1 -0
  10. package/dist/async/abortable-promise.test.d.ts +1 -0
  11. package/dist/async/abortable-promise.test.js +134 -0
  12. package/dist/async/abortable-promise.test.js.map +1 -0
  13. package/dist/async/emitter.d.ts +10 -0
  14. package/dist/async/emitter.js +22 -0
  15. package/dist/async/emitter.js.map +1 -0
  16. package/dist/async/emitter.test.d.ts +1 -0
  17. package/dist/async/emitter.test.js +49 -0
  18. package/dist/async/emitter.test.js.map +1 -0
  19. package/dist/async.d.ts +28 -0
  20. package/dist/async.js +52 -0
  21. package/dist/async.js.map +1 -0
  22. package/dist/collections/lfu-cache.d.ts +45 -0
  23. package/dist/collections/lfu-cache.js +107 -0
  24. package/dist/collections/lfu-cache.js.map +1 -0
  25. package/dist/collections/lfu-cache.test.d.ts +1 -0
  26. package/dist/collections/lfu-cache.test.js +29 -0
  27. package/dist/collections/lfu-cache.test.js.map +1 -0
  28. package/dist/collections/linked-list.d.ts +47 -0
  29. package/dist/collections/linked-list.js +129 -0
  30. package/dist/collections/linked-list.js.map +1 -0
  31. package/dist/collections/lru-cache.d.ts +45 -0
  32. package/dist/collections/lru-cache.js +84 -0
  33. package/dist/collections/lru-cache.js.map +1 -0
  34. package/dist/collections/lru-cache.test.d.ts +1 -0
  35. package/dist/collections/lru-cache.test.js +27 -0
  36. package/dist/collections/lru-cache.test.js.map +1 -0
  37. package/dist/collections.d.ts +7 -0
  38. package/dist/collections.js +8 -0
  39. package/dist/collections.js.map +1 -0
  40. package/dist/decorators.d.ts +12 -0
  41. package/dist/decorators.js +28 -0
  42. package/dist/decorators.js.map +1 -0
  43. package/dist/decorators.test.d.ts +1 -0
  44. package/dist/decorators.test.js +71 -0
  45. package/dist/decorators.test.js.map +1 -0
  46. package/dist/disposable.d.ts +115 -0
  47. package/dist/disposable.js +251 -0
  48. package/dist/disposable.js.map +1 -0
  49. package/dist/disposable.test.d.ts +1 -0
  50. package/dist/disposable.test.js +54 -0
  51. package/dist/disposable.test.js.map +1 -0
  52. package/dist/fun.d.ts +40 -0
  53. package/dist/fun.js +40 -0
  54. package/dist/fun.js.map +1 -0
  55. package/dist/index.d.ts +14 -0
  56. package/dist/index.js +15 -0
  57. package/dist/index.js.map +1 -0
  58. package/dist/order.d.ts +55 -0
  59. package/dist/order.js +98 -0
  60. package/dist/order.js.map +1 -0
  61. package/dist/order.test.d.ts +1 -0
  62. package/dist/order.test.js +63 -0
  63. package/dist/order.test.js.map +1 -0
  64. package/dist/time.d.ts +80 -0
  65. package/dist/time.js +130 -0
  66. package/dist/time.js.map +1 -0
  67. package/dist/time.test.d.ts +1 -0
  68. package/dist/time.test.js +38 -0
  69. package/dist/time.test.js.map +1 -0
  70. package/dist/types.d.ts +17 -0
  71. package/dist/types.js +6 -0
  72. package/dist/types.js.map +1 -0
  73. package/package.json +18 -18
@@ -0,0 +1,129 @@
1
+ var _a;
2
+ import { assert } from "../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
+ /**
11
+ * Iterate over the nodes in the list from head to tail.
12
+ */
13
+ this[_a] = function* () {
14
+ let node = this.head;
15
+ while (node !== undefined) {
16
+ yield node;
17
+ node = node.next;
18
+ }
19
+ };
20
+ }
21
+ static { _a = Symbol.iterator; }
22
+ /**
23
+ * Remove a node from the list.
24
+ */
25
+ remove(node) {
26
+ if (node.prev !== undefined) {
27
+ node.prev.next = node.next;
28
+ }
29
+ else {
30
+ assert(this.head === node);
31
+ this.head = node.next;
32
+ }
33
+ if (node.next !== undefined) {
34
+ node.next.prev = node.prev;
35
+ }
36
+ else {
37
+ assert(this.tail === node);
38
+ this.tail = node.prev;
39
+ }
40
+ node.prev = undefined;
41
+ node.next = undefined;
42
+ }
43
+ /**
44
+ * Insert a node into the list between the given nodes.
45
+ *
46
+ * It goes without saying that the `prev`/`next` nodes must be adjacent to
47
+ * each other in the list, or exceptions will be thrown.
48
+ */
49
+ insert(node, prev, next) {
50
+ assert(node.prev === undefined && node.next === undefined);
51
+ node.prev = prev;
52
+ node.next = next;
53
+ if (next !== undefined) {
54
+ assert(next.prev === prev);
55
+ next.prev = node;
56
+ }
57
+ if (prev !== undefined) {
58
+ assert(prev.next === next);
59
+ prev.next = node;
60
+ }
61
+ else {
62
+ this.head = node;
63
+ }
64
+ return node;
65
+ }
66
+ /**
67
+ * Insert a node at the head of the list.
68
+ */
69
+ pushHead(node) {
70
+ assert(node.prev === undefined && node.next === undefined);
71
+ if (this.head === undefined || this.tail === undefined) {
72
+ this.head = node;
73
+ this.tail = node;
74
+ }
75
+ else {
76
+ node.next = this.head;
77
+ this.head.prev = node;
78
+ this.head = node;
79
+ }
80
+ }
81
+ /**
82
+ * Insert a node at the tail of the list.
83
+ */
84
+ pushTail(node) {
85
+ assert(node.prev === undefined && node.next === undefined);
86
+ if (this.head === undefined || this.tail === undefined) {
87
+ this.head = node;
88
+ this.tail = node;
89
+ }
90
+ else {
91
+ node.prev = this.tail;
92
+ this.tail.next = node;
93
+ this.tail = node;
94
+ }
95
+ }
96
+ /**
97
+ * Remove the node at the head of the list and return it.
98
+ */
99
+ popHead() {
100
+ const node = this.head;
101
+ if (node === undefined) {
102
+ return undefined;
103
+ }
104
+ this.head = node.next;
105
+ if (this.head === undefined) {
106
+ this.tail = undefined;
107
+ }
108
+ node.prev = undefined;
109
+ node.next = undefined;
110
+ return node;
111
+ }
112
+ /**
113
+ * Remove the node at the tail of the list and return it.
114
+ */
115
+ popTail() {
116
+ const node = this.tail;
117
+ if (node === undefined) {
118
+ return undefined;
119
+ }
120
+ this.tail = node.prev;
121
+ if (this.tail === undefined) {
122
+ this.head = undefined;
123
+ }
124
+ node.prev = undefined;
125
+ node.next = undefined;
126
+ return node;
127
+ }
128
+ }
129
+ //# sourceMappingURL=linked-list.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"linked-list.js","sourceRoot":"","sources":["../../src/collections/linked-list.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAUnC;;;;GAIG;AACH,MAAM,OAAO,UAAU;IAAvB;QA+GI;;WAEG;QACH,QAAiB,GAAiC,QAAQ,CAAC;YAGvD,IAAI,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;YACrB,OAAO,IAAI,KAAK,SAAS,EAAE,CAAC;gBACxB,MAAM,IAAI,CAAC;gBACX,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;YACrB,CAAC;QACL,CAAC,CAAC;IACN,CAAC;kBATI,MAAM,CAAC,QAAQ;IA9GhB;;OAEG;IACH,MAAM,CAAC,IAAU;QACb,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YAC1B,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QAC/B,CAAC;aAAM,CAAC;YACJ,MAAM,CAAC,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;YAC3B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QAC1B,CAAC;QACD,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YAC1B,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QAC/B,CAAC;aAAM,CAAC;YACJ,MAAM,CAAC,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;YAC3B,IAAI,CAAC,IAAI,GAAG,IAAI,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AAS,EAAE,CAAC;YAC1B,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;QAC1B,CAAC;QACD,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;QACtB,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;QACtB,OAAO,IAAI,CAAC;IAChB,CAAC;CAcJ"}
@@ -0,0 +1,45 @@
1
+ import { type Option } from "@bodil/opt";
2
+ /**
3
+ * Configuration for the {@link LRUCache}.
4
+ */
5
+ export type LRUCacheOptions<V> = {
6
+ /**
7
+ * The maximum size of the cache.
8
+ */
9
+ maxLength?: number;
10
+ /**
11
+ * An optional dispose function to be called when a value is removed from
12
+ * the cache. Defaults to disposing of values which implement
13
+ * {@link Disposable}, and doing nothing otherwise.
14
+ */
15
+ disposeFunction?: (value: V) => void;
16
+ };
17
+ /**
18
+ * A Least Recently Used cache.
19
+ *
20
+ * When the cache needs to drop a value to stay within its bounds, it chooses
21
+ * the value that was accessed the least recently.
22
+ */
23
+ export declare class LRUCache<K, V> implements Disposable {
24
+ #private;
25
+ constructor(options?: LRUCacheOptions<V>);
26
+ [Symbol.dispose](): void;
27
+ /**
28
+ * Get the current size of the cache.
29
+ */
30
+ get size(): number;
31
+ /**
32
+ * Test if the cache contains a given key.
33
+ *
34
+ * This does not count as an access towards cache invalidation.
35
+ */
36
+ has(key: K): boolean;
37
+ /**
38
+ * Get the value associated with the given key, if it exists in the cache.
39
+ */
40
+ get(key: K): Option<V>;
41
+ /**
42
+ * Add a key/value pair to the cache.
43
+ */
44
+ set(key: K, value: V): void;
45
+ }
@@ -0,0 +1,84 @@
1
+ import { None, Some } from "@bodil/opt";
2
+ import { assert } from "../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,CA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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,KAA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M,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IAClC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;AACxC,CAAC,CAAC,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Some useful data structures.
3
+ * @module
4
+ */
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,8 @@
1
+ /**
2
+ * Some useful data structures.
3
+ * @module
4
+ */
5
+ export { LinkedList } from "./collections/linked-list";
6
+ export { LFUCache } from "./collections/lfu-cache";
7
+ export { LRUCache } from "./collections/lru-cache";
8
+ //# sourceMappingURL=collections.js.map
@@ -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,12 @@
1
+ /**
2
+ * Assorted decorators.
3
+ * @module
4
+ */
5
+ /**
6
+ * Decorate a getter to cache its result.
7
+ *
8
+ * Useful for deferring initialisation of a property until first access.
9
+ * Instead of `myProperty = new Thing();`,
10
+ * use `@cache get myProperty() { return new Thing(); }`.
11
+ */
12
+ export declare function cache<C extends object, T>(getter: (this: C) => T, context: ClassGetterDecoratorContext<C, T>): (this: C) => T;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Assorted decorators.
3
+ * @module
4
+ */
5
+ const cachedValues = new WeakMap();
6
+ /**
7
+ * Decorate a getter to cache its result.
8
+ *
9
+ * Useful for deferring initialisation of a property until first access.
10
+ * Instead of `myProperty = new Thing();`,
11
+ * use `@cache get myProperty() { return new Thing(); }`.
12
+ */
13
+ export function cache(getter, context) {
14
+ return function () {
15
+ let meta = cachedValues.get(this);
16
+ if (meta === undefined) {
17
+ meta = {};
18
+ cachedValues.set(this, meta);
19
+ }
20
+ if (Object.hasOwn(meta, context.name)) {
21
+ return meta[context.name];
22
+ }
23
+ const value = getter.call(this);
24
+ meta[context.name] = value;
25
+ return value;
26
+ };
27
+ }
28
+ //# sourceMappingURL=decorators.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decorators.js","sourceRoot":"","sources":["../src/decorators.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,YAAY,GAAG,IAAI,OAAO,EAA6C,CAAC;AAE9E;;;;;;GAMG;AACH,MAAM,UAAU,KAAK,CACjB,MAAsB,EACtB,OAA0C;IAE1C,OAAO;QACH,IAAI,IAAI,GAAG,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAClC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACrB,IAAI,GAAG,EAAE,CAAC;YACV,YAAY,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACjC,CAAC;QACD,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YACpC,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAM,CAAC;QACnC,CAAC;QACD,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAChC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;QAC3B,OAAO,KAAK,CAAC;IACjB,CAAC,CAAC;AACN,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,71 @@
1
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
2
+ var useValue = arguments.length > 2;
3
+ for (var i = 0; i < initializers.length; i++) {
4
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
5
+ }
6
+ return useValue ? value : void 0;
7
+ };
8
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
9
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
10
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
11
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
12
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
13
+ var _, done = false;
14
+ for (var i = decorators.length - 1; i >= 0; i--) {
15
+ var context = {};
16
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
17
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
18
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
19
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
20
+ if (kind === "accessor") {
21
+ if (result === void 0) continue;
22
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
23
+ if (_ = accept(result.get)) descriptor.get = _;
24
+ if (_ = accept(result.set)) descriptor.set = _;
25
+ if (_ = accept(result.init)) initializers.unshift(_);
26
+ }
27
+ else if (_ = accept(result)) {
28
+ if (kind === "field") initializers.unshift(_);
29
+ else descriptor[key] = _;
30
+ }
31
+ }
32
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
33
+ done = true;
34
+ };
35
+ import { expect, test } from "vitest";
36
+ import { cache } from "./decorators";
37
+ test("@cache", () => {
38
+ let called = 0;
39
+ let CacheTest = (() => {
40
+ let _instanceExtraInitializers = [];
41
+ let _get_value_decorators;
42
+ return class CacheTest {
43
+ static {
44
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
45
+ _get_value_decorators = [cache];
46
+ __esDecorate(this, null, _get_value_decorators, { kind: "getter", name: "value", static: false, private: false, access: { has: obj => "value" in obj, get: obj => obj.value }, metadata: _metadata }, null, _instanceExtraInitializers);
47
+ if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
48
+ }
49
+ get value() {
50
+ called++;
51
+ return 5;
52
+ }
53
+ constructor() {
54
+ __runInitializers(this, _instanceExtraInitializers);
55
+ }
56
+ };
57
+ })();
58
+ const t = new CacheTest();
59
+ // should not be called at construction
60
+ expect(called).toBe(0);
61
+ // should be called the first time the getter is accessed
62
+ expect(t.value).toBe(5);
63
+ expect(called).toBe(1);
64
+ // should not be called the second time the getter is accessed
65
+ expect(t.value).toBe(5);
66
+ expect(called).toBe(1);
67
+ // or the third, for good measure
68
+ expect(t.value).toBe(5);
69
+ expect(called).toBe(1);
70
+ });
71
+ //# sourceMappingURL=decorators.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decorators.test.js","sourceRoot":"","sources":["../src/decorators.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAErC,IAAI,CAAC,QAAQ,EAAE,GAAG,EAAE;IAChB,IAAI,MAAM,GAAG,CAAC,CAAC;QACT,SAAS;;;qBAAT,SAAS;;;yCACV,KAAK;gBAAC,sKAAI,KAAK,6DAGf;;;YAHM,IAAI,KAAK;gBACZ,MAAM,EAAE,CAAC;gBACT,OAAO,CAAC,CAAC;YACb,CAAC;;gBAJC,mDAAS;;;;IAMf,MAAM,CAAC,GAAG,IAAI,SAAS,EAAE,CAAC;IAC1B,uCAAuC;IACvC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACvB,yDAAyD;IACzD,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACxB,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACvB,8DAA8D;IAC9D,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACxB,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACvB,iCAAiC;IACjC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACxB,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC3B,CAAC,CAAC,CAAC"}
@@ -0,0 +1,115 @@
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 } from "type-fest";
8
+ import type { Unsubscribable } from "type-fest/globals";
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
+ * Call {@link setTimeout} with the provided callback and return a
33
+ * {@link Disposable} which will cancel the timeout when disposed.
34
+ */
35
+ export declare function timeout(fn: () => void, timeout: number | Temporal.Duration): Disposable;
36
+ /**
37
+ * Call {@link setInterval} with the provided callback and return a
38
+ * {@link Disposable} which will cancel the interval when disposed.
39
+ */
40
+ export declare function interval(fn: () => void, interval: number | Temporal.Duration): Disposable;
41
+ export declare function idleCallback(callback: IdleRequestCallback, options?: IdleRequestOptions): Disposable;
42
+ /**
43
+ * An implementation of {@link AbortController} which also implements
44
+ * {@link Disposable}.
45
+ *
46
+ * Disposing the controller automatically triggers the abort signal.
47
+ */
48
+ export declare class DisposableAbortController implements AbortController, Disposable {
49
+ #private;
50
+ get signal(): AbortSignal;
51
+ abort(reason?: any): void;
52
+ [Symbol.dispose](): void;
53
+ }
54
+ /**
55
+ * Create an {@link AbortController} which also implements {@link Disposable}.
56
+ *
57
+ * Disposing the controller automatically triggers the abort signal.
58
+ *
59
+ * @returns A {@link DisposableAbortController}.
60
+ */
61
+ export declare function abortController(): DisposableAbortController;
62
+ export interface DisposableContext extends Disposable {
63
+ /**
64
+ * Disposes the resources currently held by this context.
65
+ */
66
+ dispose(): void;
67
+ /**
68
+ * Register a {@link Disposifiable} resource with this context, to be
69
+ * disposed along with it.
70
+ *
71
+ * @returns The {@link Disposable} that was registered with the context.
72
+ */
73
+ use(resource: null): null;
74
+ use(resource: undefined): undefined;
75
+ use(resource: Disposifiable): Disposable;
76
+ use(resource: Disposifiable | undefined): Disposable | undefined;
77
+ use(resource: Disposifiable | null | undefined): Disposable | null | undefined;
78
+ adopt<A>(value: A, onDispose: (value: A) => void): A;
79
+ }
80
+ /**
81
+ * A reentrant version of {@link DisposableStack}.
82
+ *
83
+ * This maintains a list of {@link Disposable}s, added with the
84
+ * {@link DisposableContext.use} method, which will all be disposed when you
85
+ * dispose of the {@link DisposableContext} itself.
86
+ *
87
+ * Where this differs from {@link DisposableStack} is that disposing it only
88
+ * resets it after disposing of its dependents, and you can reuse it as many
89
+ * times as you like. This is very handy for managing lifecycle resources, eg. a
90
+ * web component can allocate resources in its `connectedCallback` method and
91
+ * during its connected lifecycle phase which can all be disposed in
92
+ * `disconnectedCallback` with a single call to
93
+ * {@link DisposableContext.dispose}, and it's instantly ready to go again on
94
+ * reconnect.
95
+ *
96
+ * @class
97
+ */
98
+ export declare const DisposableContext: Constructor<DisposableContext>;
99
+ /**
100
+ * A class mixin which provides a {@link Disposable} implementation.
101
+ *
102
+ * The `use(resource)` method can be used to register {@link Disposifiable}
103
+ * resources which will be disposed when the object's dispose method is called.
104
+ */
105
+ export declare const AutoDisposable: <C extends Constructor<object>>(superclass: C) => {
106
+ new (...arguments_: any[]): {
107
+ __context?: DisposableContext;
108
+ use(resource: null): null;
109
+ use(resource: undefined): undefined;
110
+ use(resource: Disposifiable): Disposable;
111
+ use(resource: Disposifiable | undefined): Disposable | undefined;
112
+ use(resource: Disposifiable | null | undefined): Disposable | null | undefined;
113
+ [Symbol.dispose](): void;
114
+ };
115
+ } & C;