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.
This commit is contained in:
Glen Whitney 2022-07-22 15:06:56 -07:00
parent 4a2d53a68a
commit e6699ce642
1 changed files with 6 additions and 15 deletions

View File

@ -91,9 +91,10 @@ export default class PocomathInstance {
*/ */
_invalidate(name) { _invalidate(name) {
const self = this const self = this
this[name] = function () { Object.defineProperty(this, name, {
return self._bundle(name).apply(self, arguments) configurable: true,
} get: () => self._bundle(name)
})
if (!(name in this._imps)) { if (!(name in this._imps)) {
this._imps[name] = {} this._imps[name] = {}
} }
@ -130,7 +131,7 @@ export default class PocomathInstance {
if (dep === 'self') { if (dep === 'self') {
self_referential = true self_referential = true
} else { } else {
refs[dep] = this._ensureBundle(dep) // assume acyclic for now refs[dep] = this[dep] // assume acyclic for now
} }
} }
if (self_referential) { if (self_referential) {
@ -144,20 +145,10 @@ export default class PocomathInstance {
} }
} }
const tf = this._typed(name, tf_imps) const tf = this._typed(name, tf_imps)
this[name] = tf Object.defineProperty(this, name, {configurable: true, value: tf})
return 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 * Ensure that all of the requested types and conversions are actually
* in the typed-function universe: * in the typed-function universe: