|
| 1 | +import { DEBUG } from '@glimmer/env'; |
| 2 | +import { createCache, getValue } from '@glimmer/validator'; |
| 3 | + |
| 4 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 5 | +export const cached: PropertyDecorator = (...args: any[]) => { |
| 6 | + const [target, key, descriptor] = args; |
| 7 | + |
| 8 | + // Error on `@cached()`, `@cached(...args)`, and `@cached propName = value;` |
| 9 | + if (DEBUG && target === undefined) throwCachedExtraneousParens(); |
| 10 | + if ( |
| 11 | + DEBUG && |
| 12 | + (typeof target !== 'object' || |
| 13 | + typeof key !== 'string' || |
| 14 | + typeof descriptor !== 'object' || |
| 15 | + args.length !== 3) |
| 16 | + ) { |
| 17 | + throwCachedInvalidArgsError(args); |
| 18 | + } |
| 19 | + if (DEBUG && 'get' in descriptor && typeof descriptor.get !== 'function') |
| 20 | + throwCachedGetterOnlyError(key); |
| 21 | + |
| 22 | + const caches = new WeakMap(); |
| 23 | + const getter = descriptor.get; |
| 24 | + descriptor.get = function (): unknown { |
| 25 | + if (!caches.has(this)) caches.set(this, createCache(getter.bind(this))); |
| 26 | + return getValue(caches.get(this)); |
| 27 | + }; |
| 28 | +}; |
| 29 | + |
| 30 | +function throwCachedExtraneousParens(): never { |
| 31 | + throw new Error( |
| 32 | + 'You attempted to use @cached(), which is not necessary nor supported. Remove the parentheses and you will be good to go!' |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +function throwCachedGetterOnlyError(key: string): never { |
| 37 | + throw new Error(`The @cached decorator must be applied to getters. '${key}' is not a getter.`); |
| 38 | +} |
| 39 | + |
| 40 | +function throwCachedInvalidArgsError(args: unknown[]): never { |
| 41 | + throw new Error( |
| 42 | + `You attempted to use @cached on with ${ |
| 43 | + args.length > 1 ? 'arguments' : 'an argument' |
| 44 | + } ( @cached(${args |
| 45 | + .map((d) => `'${d}'`) |
| 46 | + .join( |
| 47 | + ', ' |
| 48 | + )}), which is not supported. Dependencies are automatically tracked, so you can just use ${'`@cached`'}` |
| 49 | + ); |
| 50 | +} |
0 commit comments