You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since decorators are called on the class declaration and not instantiation, the same cache instance is being used by all the class instances when using the custom cache option for pMemoizeDecorator.
Here's a failing test that shows what I mean:
test('.pMemoizeDecorator() with custom cache',async(t)=>{letreturnValue=1;classTestClass{
@pMemoizeDecorator({cache: newMap()})asynccounter(){returnreturnValue++;}}constalpha=newTestClass();t.is(awaitalpha.counter(),1);t.is(awaitalpha.counter(),1,'The method should be memoized');constbeta=newTestClass();t.is(awaitbeta.counter(),2,'The method should not be memoized across instances');});
The test output:
.pMemoizeDecorator() with custom cache
The method should not be memoized across instances
Difference:
- 1
+ 2
› file://test.ts:213:4
─
1 test failed
The output shows that the same cache instance is being used for all the TestClass instances, which is the expected behavior because the pMemoizeDecorator is being called once on the class declaration, so the same options object is being passed to pMemoize.
I've created this issue to discuss the options to solve this problem before creating a pull request.
The first solution that comes to my mind is accepting a function for the cache option that returns an instance:
Since decorators are called on the class declaration and not instantiation, the same
cache
instance is being used by all the class instances when using the customcache
option forpMemoizeDecorator
.Here's a failing test that shows what I mean:
The test output:
The output shows that the same
cache
instance is being used for all theTestClass
instances, which is the expected behavior because thepMemoizeDecorator
is being called once on the class declaration, so the sameoptions
object is being passed topMemoize
.I've created this issue to discuss the options to solve this problem before creating a pull request.
The first solution that comes to my mind is accepting a function for the
cache
option that returns an instance:The text was updated successfully, but these errors were encountered: