From e6699ce6428c045f084b36520ccfb94d5f91ea9e Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Fri, 22 Jul 2022 15:06:56 -0700 Subject: [PATCH] refactor: Use Object.defineProperty with a getter to invalidate Rather than setting a dummy function; this avoids anyone ever storing a dummy function. Resolves #8. --- src/core/PocomathInstance.mjs | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/core/PocomathInstance.mjs b/src/core/PocomathInstance.mjs index 2436032..dd4a6cc 100644 --- a/src/core/PocomathInstance.mjs +++ b/src/core/PocomathInstance.mjs @@ -91,9 +91,10 @@ export default class PocomathInstance { */ _invalidate(name) { const self = this - this[name] = function () { - return self._bundle(name).apply(self, arguments) - } + Object.defineProperty(this, name, { + configurable: true, + get: () => self._bundle(name) + }) if (!(name in this._imps)) { this._imps[name] = {} } @@ -130,7 +131,7 @@ export default class PocomathInstance { if (dep === 'self') { self_referential = true } else { - refs[dep] = this._ensureBundle(dep) // assume acyclic for now + refs[dep] = this[dep] // assume acyclic for now } } if (self_referential) { @@ -144,20 +145,10 @@ export default class PocomathInstance { } } const tf = this._typed(name, tf_imps) - this[name] = tf + Object.defineProperty(this, name, {configurable: true, value: tf}) return tf } - /** - * Ensure that the generated typed function is assigned to the given - * name and return it - */ - _ensureBundle(name) { - const maybe = this[name] - if (this._typed.isTypedFunction(maybe)) return maybe - return this._bundle(name) - } - /** * Ensure that all of the requested types and conversions are actually * in the typed-function universe: -- 2.34.1