utilium 1.9.0 → 1.10.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/misc.d.ts +10 -0
- package/dist/misc.js +17 -0
- package/package.json +1 -1
- package/src/misc.ts +26 -0
package/dist/misc.d.ts
CHANGED
@@ -9,3 +9,13 @@ export declare function canary(error?: Error): () => void;
|
|
9
9
|
* @see https://github.com/tc39/proposal-throw-expressions
|
10
10
|
*/
|
11
11
|
export declare function _throw(e: unknown): never;
|
12
|
+
interface MemoizeMetadata extends DecoratorMetadata {
|
13
|
+
memoized?: Record<PropertyKey, any>;
|
14
|
+
}
|
15
|
+
/**
|
16
|
+
* Decorator for memoizing the result of a getter.
|
17
|
+
*/
|
18
|
+
export declare function memoize<T, This>(get: () => T, context: ClassGetterDecoratorContext<This, T> & {
|
19
|
+
metadata?: MemoizeMetadata;
|
20
|
+
}): (this: This) => T;
|
21
|
+
export {};
|
package/dist/misc.js
CHANGED
@@ -46,3 +46,20 @@ export function canary(error = new Error()) {
|
|
46
46
|
export function _throw(e) {
|
47
47
|
throw e;
|
48
48
|
}
|
49
|
+
/**
|
50
|
+
* Decorator for memoizing the result of a getter.
|
51
|
+
*/
|
52
|
+
export function memoize(get, context) {
|
53
|
+
if (context.kind != 'getter')
|
54
|
+
throw new Error('@memoize can only be used on getters');
|
55
|
+
return function () {
|
56
|
+
context.metadata ??= {};
|
57
|
+
const { memoized = {} } = context.metadata;
|
58
|
+
if (context.name in memoized) {
|
59
|
+
console.log('Using cached value for', context.name, JSON.stringify(memoized[context.name]));
|
60
|
+
return memoized[context.name];
|
61
|
+
}
|
62
|
+
memoized[context.name] = get.call(this);
|
63
|
+
return memoized[context.name];
|
64
|
+
};
|
65
|
+
}
|
package/package.json
CHANGED
package/src/misc.ts
CHANGED
@@ -52,3 +52,29 @@ export function canary(error: Error = new Error()) {
|
|
52
52
|
export function _throw(e: unknown): never {
|
53
53
|
throw e;
|
54
54
|
}
|
55
|
+
|
56
|
+
interface MemoizeMetadata extends DecoratorMetadata {
|
57
|
+
memoized?: Record<PropertyKey, any>;
|
58
|
+
}
|
59
|
+
|
60
|
+
/**
|
61
|
+
* Decorator for memoizing the result of a getter.
|
62
|
+
*/
|
63
|
+
export function memoize<T, This>(
|
64
|
+
get: () => T,
|
65
|
+
context: ClassGetterDecoratorContext<This, T> & { metadata?: MemoizeMetadata }
|
66
|
+
) {
|
67
|
+
if (context.kind != 'getter') throw new Error('@memoize can only be used on getters');
|
68
|
+
|
69
|
+
return function (this: This): T {
|
70
|
+
context.metadata ??= {};
|
71
|
+
const { memoized = {} } = context.metadata;
|
72
|
+
|
73
|
+
if (context.name in memoized) {
|
74
|
+
console.log('Using cached value for', context.name, JSON.stringify(memoized[context.name]));
|
75
|
+
return memoized[context.name];
|
76
|
+
}
|
77
|
+
memoized[context.name] = get.call(this);
|
78
|
+
return memoized[context.name];
|
79
|
+
};
|
80
|
+
}
|