refactor: Use Object.defineProperty with a getter to invalidate #17

Merged
glen merged 1 commits from no_invalid into main 2022-07-22 22:09:18 +00:00

View File

@ -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: