diff --git a/src/once.ts b/src/once.ts index a69b646..b9b2975 100644 --- a/src/once.ts +++ b/src/once.ts @@ -1,3 +1,5 @@ +const instances = new WeakMap>(); + /** * A class method decorator that limits a method to be called only once. All * subsequent calls will return the result of the first call. @@ -6,10 +8,9 @@ export function once( fn: (this: This, ...args: Args) => Return, context?: ClassMethodDecoratorContext Return>, ) { - const instances = new WeakMap>(); return function (this: This, ...args: Args): Return { - if (!instances.has(this)) instances.set(this, new WeakMap()); - const map = instances.get(this)!; + let map = instances.get(this); + if (!map) instances.set(this, map = new WeakMap()); if (!map.has(fn)) map.set(fn, fn.apply(this, args)); return map.get(fn); };