Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7f7ca0a

Browse files
authored
Fix using multiple decorators on the same instance (#78)
1 parent3b1b8a7 commit7f7ca0a

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

‎index.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import mapAgeCleaner from 'map-age-cleaner';
33

44
typeAnyFunction=(...arguments_:any)=>any;
55

6-
constdecoratorInstanceMap=newWeakMap();
7-
86
constcacheStore=newWeakMap<AnyFunction,CacheStorage<any,any>>();
97

108
interfaceCacheStorageContent<ValueType>{
@@ -169,6 +167,8 @@ export function memDecorator<
169167
>(
170168
options:Options<FunctionToMemoize,CacheKeyType>={},
171169
){
170+
constinstanceMap=newWeakMap();
171+
172172
return(
173173
target:any,
174174
propertyKey:string,
@@ -184,13 +184,13 @@ export function memDecorator<
184184
deletedescriptor.writable;
185185

186186
descriptor.get=function(){
187-
if(!decoratorInstanceMap.has(this)){
187+
if(!instanceMap.has(this)){
188188
constvalue=mem(input,options)asFunctionToMemoize;
189-
decoratorInstanceMap.set(this,value);
189+
instanceMap.set(this,value);
190190
returnvalue;
191191
}
192192

193-
returndecoratorInstanceMap.get(this)asFunctionToMemoize;
193+
returninstanceMap.get(this)asFunctionToMemoize;
194194
};
195195
};
196196
}

‎test.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,24 @@ test('prototype support', t => {
239239

240240
test('.decorator()',t=>{
241241
letreturnValue=1;
242+
constreturnValue2=101;
242243

243244
classTestClass{
244245
@memDecorator()
245246
counter(){
246-
returnreturnValue;
247+
returnreturnValue++;
248+
}
249+
250+
@memDecorator()
251+
counter2(){
252+
returnreturnValue2;
247253
}
248254
}
249255

250256
constalpha=newTestClass();
251257
t.is(alpha.counter(),1);
252258
t.is(alpha.counter(),1,'The method should be memoized');
253-
254-
returnValue++;
259+
t.is(alpha.counter2(),101,'The method should be memoized separately from the other one');
255260

256261
constbeta=newTestClass();
257262
t.is(beta.counter(),2,'The method should not be memoized across instances');

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp