Hello,
I'm running into an issue memoizing a getter that relies on configuration set in the constructor. It appears that this line in memDecorator
calls the getter immediately in addition to retrieving the getter function:
const input = target[propertyKey]; // eslint-disable-line @typescript-eslint/no-unsafe-assignment
Example:
import { memDecorator } from 'mem'
export class TestMemoizeGetter {
constructorCalled?: boolean
constructor() {
this.constructorCalled = true
}
@memDecorator()
public get someGetter() {
if (!this.constructorCalled) {
throw new Error('constructor has not been called')
}
return Math.random()
}
}
const instance = new TestMemoizeGetter()
assert.doesNotThrow(() => {
const value = instance.someGetter
assert.strictEqual(instance.someGetter, value)
})
Output:
Error: constructor has not been called
at Object.get someGetter (/home/neodon/p/mem-getters/index.ts:14:13)
at file:///home/neodon/p/mem-getters/node_modules/mem/dist/index.js:84:29
at __decorateClass (file:///home/neodon/p/mem-getters/index.ts:1:374)
at <anonymous> (/home/neodon/p/mem-getters/index.ts:12:14)
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
I messed around with the implementation for memDecorator
and had some luck by using descriptor.value
for functions and descripter.get
for getter functions. It created some more problems though around this
bindings. It seems that getter functions have a lot of voodoo baked in.
Let me know if you see value in this getting fixed. I can create a pull request with a failing test, and add some of the partial fix I figured out.
Also, the same issue exists with p-memoize
, but it seemed better to start here since getters are synchronous.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too