Skip to content

Commit ad4c505

Browse files
fix: revert CACHE_DROP to .dropCache()
1 parent 8dc0842 commit ad4c505

File tree

5 files changed

+24
-36
lines changed

5 files changed

+24
-36
lines changed

src/decorators/asyncMemo.decorator.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { _AsyncMemo } from './asyncMemo.decorator'
2-
import { CACHE_DROP } from './memo.decorator'
32
import { MapMemoCache } from './memo.util'
43

54
class A {
@@ -37,7 +36,7 @@ test('memo a', async () => {
3736
expect(a.func).toMatchSnapshot()
3837

3938
// cleanup for the next tests
40-
await (a.a as any)(CACHE_DROP)
39+
await (a.a as any).dropCache()
4140
})
4241

4342
test('MEMO_DROP_CACHE', async () => {
@@ -48,7 +47,7 @@ test('MEMO_DROP_CACHE', async () => {
4847
await a.a(2, 3)
4948

5049
// drop cache
51-
await (a.a as any)(CACHE_DROP)
50+
await (a.a as any).dropCache()
5251

5352
// second call
5453
await a.a(2, 3)

src/decorators/asyncMemo.decorator.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { _since } from '../time/time.util'
22
import { Merge } from '../typeFest'
33
import { AnyObject } from '../types'
44
import { _getArgsSignature, _getMethodSignature, _getTargetMethodSignature } from './decorator.util'
5-
import { CACHE_DROP, MemoOptions } from './memo.decorator'
5+
import { MemoOptions } from './memo.decorator'
66
import { AsyncMemoCache, jsonMemoSerializer } from './memo.util'
77

88
export type AsyncMemoOptions = Merge<
@@ -65,19 +65,6 @@ export const _AsyncMemo =
6565
// UPD: no! AsyncMemo supports "persistent caches" (e.g Database-backed cache)
6666
}
6767

68-
if (args.length === 1 && args[0] === CACHE_DROP) {
69-
// Special event - CACHE_DROP
70-
// Function will return undefined
71-
logger.log(`${methodSignature} @_AsyncMemo.dropCache()`)
72-
try {
73-
await Promise.all(cache.get(ctx)!.map(c => c.clear()))
74-
} catch (err) {
75-
logger.error(err)
76-
}
77-
78-
return
79-
}
80-
8168
let value: any
8269

8370
try {
@@ -146,6 +133,15 @@ export const _AsyncMemo =
146133
}
147134
}
148135
} as any
136+
;(descriptor.value as any).dropCache = async () => {
137+
logger.log(`${methodSignature} @_AsyncMemo.dropCache()`)
138+
try {
139+
await Promise.all([...cache.values()].flatMap(c => c.map(c => c.clear())))
140+
cache.clear()
141+
} catch (err) {
142+
logger.error(err)
143+
}
144+
}
149145

150146
return descriptor
151147
}

src/decorators/memo.decorator.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { _Memo, CACHE_DROP } from './memo.decorator'
1+
import { _Memo } from './memo.decorator'
22

33
class A {
44
func(n: number): void {
@@ -33,7 +33,7 @@ test('memo a', () => {
3333
expect(a.func).toMatchSnapshot()
3434

3535
// cleanup for the next tests
36-
;(a.a as any)(CACHE_DROP)
36+
;(a.a as any).dropCache()
3737
})
3838

3939
test('MEMO_DROP_CACHE', () => {
@@ -44,7 +44,7 @@ test('MEMO_DROP_CACHE', () => {
4444
a.a(2, 3)
4545

4646
// drop cache
47-
;(a.a as any)(CACHE_DROP)
47+
;(a.a as any).dropCache()
4848

4949
// second call
5050
a.a(2, 3)

src/decorators/memo.decorator.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ import { AnyObject } from '../types'
1010
import { _getArgsSignature, _getMethodSignature, _getTargetMethodSignature } from './decorator.util'
1111
import { jsonMemoSerializer, MapMemoCache, MemoCache } from './memo.util'
1212

13-
/**
14-
* Symbol to indicate that the Cache should be dropped.
15-
*/
16-
export const CACHE_DROP = Symbol('CACHE_DROP')
17-
1813
export interface MemoOptions {
1914
/**
2015
* Default to false
@@ -111,13 +106,6 @@ export const _Memo =
111106
descriptor.value = function (this: typeof target, ...args: any[]): any {
112107
const ctx = this
113108

114-
if (args.length === 1 && args[0] === CACHE_DROP) {
115-
// Special event - CACHE_DROP
116-
// Function will return undefined
117-
logger.log(`${methodSignature} @_Memo.CACHE_DROP`)
118-
return cache.get(ctx)?.clear()
119-
}
120-
121109
const cacheKey = cacheKeyFn(args)
122110

123111
if (!cache.has(ctx)) {
@@ -195,6 +183,11 @@ export const _Memo =
195183
return res
196184
}
197185
} as any
186+
;(descriptor.value as any).dropCache = () => {
187+
logger.log(`${methodSignature} @_Memo.dropCache()`)
188+
cache.forEach(memoCache => memoCache.clear())
189+
cache.clear()
190+
}
198191

199192
return descriptor
200193
}

src/decorators/memoPromise.decorator.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { _Memo, CACHE_DROP } from './memo.decorator'
1+
import { _Memo } from './memo.decorator'
22

33
class A {
44
counter = 0
@@ -53,7 +53,7 @@ test('memo a', async () => {
5353
expect(a.func).toMatchSnapshot()
5454

5555
// cleanup for the next tests
56-
;(a.a as any)(CACHE_DROP)
56+
;(a.a as any).dropCache()
5757
})
5858

5959
test('MEMO_DROP_CACHE', async () => {
@@ -64,7 +64,7 @@ test('MEMO_DROP_CACHE', async () => {
6464
await a.a(2, 3)
6565

6666
// drop cache
67-
;(a.a as any)(CACHE_DROP)
67+
;(a.a as any).dropCache()
6868

6969
// second call
7070
await a.a(2, 3)
@@ -92,5 +92,5 @@ test('memo b', async () => {
9292
expect(a.func).toHaveBeenCalledTimes(1)
9393

9494
// cleanup for the next tests
95-
;(a.b as any)(CACHE_DROP)
95+
;(a.b as any).dropCache()
9696
})

0 commit comments

Comments
 (0)