utilium 2.8.3 → 2.8.5

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 CHANGED
@@ -9,13 +9,11 @@ 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 {};
12
+ export declare function memoize<T, This extends object>(get: () => T, context: ClassGetterDecoratorContext<This, T>): () => T;
13
+ export declare function memoize<T, This extends object>(value: {
14
+ get(): T;
15
+ set(value: T): void;
16
+ }, context: ClassGetterDecoratorContext<This, T>): {
17
+ get(): T;
18
+ set(value: T): void;
19
+ };
package/dist/misc.js CHANGED
@@ -51,20 +51,21 @@ export function _throw(e) {
51
51
  Error?.captureStackTrace(e, _throw);
52
52
  throw e;
53
53
  }
54
- /**
55
- * Decorator for memoizing the result of a getter.
56
- */
57
- export function memoize(get, context) {
58
- if (context.kind != 'getter')
59
- throw new Error('@memoize can only be used on getters');
60
- return function () {
61
- context.metadata ??= {};
62
- const { memoized = {} } = context.metadata;
63
- if (context.name in memoized) {
64
- console.log('Using cached value for', context.name, JSON.stringify(memoized[context.name]));
65
- return memoized[context.name];
66
- }
67
- memoized[context.name] = get.call(this);
68
- return memoized[context.name];
69
- };
54
+ export function memoize(value, context) {
55
+ if (!['getter', 'accessor'].includes(context.kind))
56
+ throw new Error('@memoize can only be used on getters and auto-accessors');
57
+ const cache = new WeakMap();
58
+ function get() {
59
+ if (cache.has(this))
60
+ return cache.get(this);
61
+ const result = context.access.get(this);
62
+ cache.set(this, result);
63
+ return result;
64
+ }
65
+ switch (context.kind) {
66
+ case 'getter':
67
+ return get;
68
+ case 'accessor':
69
+ return { ...value, get };
70
+ }
70
71
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "2.8.3",
3
+ "version": "2.8.5",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",