import { assertGetterDecorator, isDecoratorCall, } from "../common/decorators.js"; type LazyDecorator = ( value: (this: This) => Value, context: ClassGetterDecoratorContext, ) => (this: This) => Value; export function lazy( value: (this: This) => Value, context: ClassGetterDecoratorContext, ): (this: This) => Value; export function lazy(): LazyDecorator; export function lazy(inputOrValue?: unknown, context?: unknown): unknown { const decorate: LazyDecorator = ( value: (this: This) => Value, decoratorContext: ClassGetterDecoratorContext, ): (this: This) => Value => { assertGetterDecorator("lazy", value, decoratorContext as any); const cache = new WeakMap(); return function(this: This): Value { const self = this as object; if (cache.has(self)) { return cache.get(self) as Value; } const result = value.call(this); cache.set(self, result); return result; }; }; if (arguments.length === 2 && isDecoratorCall(context)) { return decorate(inputOrValue as (this: any) => unknown, context as ClassGetterDecoratorContext); } return decorate; }