@@ -12,11 +12,11 @@ import {
12
12
stub ,
13
13
} from "jsr:@std/[email protected] /mock" ;
14
14
import { FakeTime } from "jsr:@std/[email protected] /time" ;
15
- import { rememberPromise } from "./mod.ts" ;
15
+ import { rememberPromise , type RememberPromiseOptions } from "./mod.ts" ;
16
16
17
17
const createMockPromiseFn = ( ) => {
18
18
let callCount = 1 ;
19
- return spy ( ( ) => Promise . resolve ( `call-${ callCount ++ } ` ) ) ;
19
+ return spy ( ( ... _args : unknown [ ] ) => Promise . resolve ( `call-${ callCount ++ } ` ) ) ;
20
20
} ;
21
21
22
22
let time : FakeTime ;
@@ -31,6 +31,7 @@ afterEach(() => {
31
31
32
32
it ( "should throttle calls to promiseFn" , async ( ) => {
33
33
const promiseFn = createMockPromiseFn ( ) ;
34
+
34
35
const cachedPromiseFn = rememberPromise ( promiseFn ) ;
35
36
36
37
assertEquals ( await Promise . all ( [ cachedPromiseFn ( ) , cachedPromiseFn ( ) ] ) , [
@@ -43,6 +44,7 @@ it("should throttle calls to promiseFn", async () => {
43
44
describe ( "ttl" , ( ) => {
44
45
it ( "should never call promiseFn again if ttl is not set" , async ( ) => {
45
46
const promiseFn = createMockPromiseFn ( ) ;
47
+
46
48
const cachedPromiseFn = rememberPromise ( promiseFn ) ;
47
49
48
50
await cachedPromiseFn ( ) ;
@@ -54,6 +56,7 @@ describe("ttl", () => {
54
56
55
57
it ( "should call promiseFn again if ttl is set and expired" , async ( ) => {
56
58
const promiseFn = createMockPromiseFn ( ) ;
59
+
57
60
const cachedPromiseFn = rememberPromise ( promiseFn , { ttl : 30_000 } ) ;
58
61
59
62
await cachedPromiseFn ( ) ;
@@ -65,6 +68,7 @@ describe("ttl", () => {
65
68
66
69
it ( "should not call promiseFn again if ttl is set and not expired" , async ( ) => {
67
70
const promiseFn = createMockPromiseFn ( ) ;
71
+
68
72
const cachedPromiseFn = rememberPromise ( promiseFn , { ttl : 30_000 } ) ;
69
73
70
74
await cachedPromiseFn ( ) ;
@@ -78,6 +82,7 @@ describe("ttl", () => {
78
82
describe ( "allowStale" , ( ) => {
79
83
it ( "should return previous cached result while updating cache after ttl expired and allowStale is true" , async ( ) => {
80
84
const promiseFn = createMockPromiseFn ( ) ;
85
+
81
86
const cachedPromiseFn = rememberPromise ( promiseFn , {
82
87
allowStale : true ,
83
88
ttl : 30_000 ,
@@ -92,6 +97,7 @@ describe("allowStale", () => {
92
97
93
98
it ( "should not return previous cached result while updating cache after ttl expired and allowStale is false" , async ( ) => {
94
99
const promiseFn = createMockPromiseFn ( ) ;
100
+
95
101
const cachedPromiseFn = rememberPromise ( promiseFn , {
96
102
allowStale : false ,
97
103
ttl : 30_000 ,
@@ -106,9 +112,10 @@ describe("allowStale", () => {
106
112
107
113
describe ( "cache" , ( ) => {
108
114
it ( "should not call onCacheUpdateError or shouldIgnoreResult if cache is set to false" , async ( ) => {
115
+ const promiseFn = createMockPromiseFn ( ) ;
109
116
const onCacheUpdateErrorSpy = spy ( ( ) => { } ) ;
110
117
const shouldIgnoreResultSpy = spy ( ( ) => false ) ;
111
- const promiseFn = createMockPromiseFn ( ) ;
118
+
112
119
const cachedPromiseFn = rememberPromise ( promiseFn , {
113
120
cache : false ,
114
121
onCacheUpdateError : onCacheUpdateErrorSpy ,
@@ -125,6 +132,7 @@ describe("cache", () => {
125
132
describe ( "getCacheKey" , ( ) => {
126
133
it ( "should use the same cached result if getCacheKey returns the same key" , async ( ) => {
127
134
const promiseFn = createMockPromiseFn ( ) ;
135
+
128
136
const cachedPromiseFn = rememberPromise ( promiseFn , {
129
137
getCacheKey : ( ) => "" ,
130
138
} ) ;
@@ -136,6 +144,7 @@ describe("getCacheKey", () => {
136
144
137
145
it ( "should not use the same cached result if getCacheKey returns a different key" , async ( ) => {
138
146
const promiseFn = createMockPromiseFn ( ) ;
147
+
139
148
const cachedPromiseFn = rememberPromise ( promiseFn , {
140
149
getCacheKey : stub (
141
150
{ getCacheKey : ( ) => "default" } ,
@@ -153,6 +162,7 @@ describe("getCacheKey", () => {
153
162
describe ( "onCacheUpdateError" , ( ) => {
154
163
describe ( "onCacheUpdateError is undefined" , ( ) => {
155
164
it ( "should throw unhandled rejection if errors occurred while updating cache" , async ( ) => {
165
+ const promiseFn = createMockPromiseFn ( ) ;
156
166
const cache = new Map ( ) ;
157
167
const cacheSetStub = stub ( cache , "set" , ( ) => {
158
168
throw new Error ( "test" ) ;
@@ -165,7 +175,7 @@ describe("onCacheUpdateError", () => {
165
175
} ;
166
176
globalThis . addEventListener ( "unhandledrejection" , listener ) ;
167
177
} ) ;
168
- const promiseFn = createMockPromiseFn ( ) ;
178
+
169
179
const cachedPromiseFn = rememberPromise ( promiseFn , { cache } ) ;
170
180
171
181
assertEquals ( await cachedPromiseFn ( ) , "call-1" ) ;
@@ -175,6 +185,7 @@ describe("onCacheUpdateError", () => {
175
185
} ) ;
176
186
177
187
it ( "should throw unhandled rejection if errors occurred while calling shouldIgnoreResult" , async ( ) => {
188
+ const promiseFn = createMockPromiseFn ( ) ;
178
189
const shouldIgnoreResultSpy = spy ( ( ) => {
179
190
throw new Error ( "test" ) ;
180
191
} ) ;
@@ -186,7 +197,7 @@ describe("onCacheUpdateError", () => {
186
197
} ;
187
198
globalThis . addEventListener ( "unhandledrejection" , listener ) ;
188
199
} ) ;
189
- const promiseFn = createMockPromiseFn ( ) ;
200
+
190
201
const cachedPromiseFn = rememberPromise ( promiseFn , {
191
202
shouldIgnoreResult : shouldIgnoreResultSpy ,
192
203
} ) ;
@@ -200,47 +211,60 @@ describe("onCacheUpdateError", () => {
200
211
201
212
describe ( "onCacheUpdateError is defined" , ( ) => {
202
213
it ( "should call onCacheUpdateError if errors occurred while updating cache" , async ( ) => {
214
+ const promiseFn = createMockPromiseFn ( ) ;
203
215
const cache = new Map ( ) ;
204
216
const cacheSetStub = stub ( cache , "set" , ( ) => {
205
217
throw new Error ( "test" ) ;
206
218
} ) ;
207
- const onCacheUpdateErrorSpy = spy ( ( _error : unknown ) => { } ) ;
208
- const promiseFn = createMockPromiseFn ( ) ;
219
+ const onCacheUpdateError : RememberPromiseOptions <
220
+ typeof promiseFn
221
+ > [ "onCacheUpdateError" ] = ( ) => { } ;
222
+ const onCacheUpdateErrorSpy = spy ( onCacheUpdateError ) ;
223
+
209
224
const cachedPromiseFn = rememberPromise ( promiseFn , {
210
225
cache,
211
226
onCacheUpdateError : onCacheUpdateErrorSpy ,
212
227
} ) ;
213
228
214
- assertEquals ( await cachedPromiseFn ( ) , "call-1" ) ;
229
+ assertEquals ( await cachedPromiseFn ( "arg-1" ) , "call-1" ) ;
215
230
assertSpyCalls ( promiseFn , 1 ) ;
216
231
assertSpyCalls ( cacheSetStub , 1 ) ;
217
232
assertSpyCalls ( onCacheUpdateErrorSpy , 1 ) ;
218
233
assertIsError ( onCacheUpdateErrorSpy . calls [ 0 ] . args [ 0 ] , Error , "test" ) ;
234
+ assertEquals ( onCacheUpdateErrorSpy . calls [ 0 ] . args [ 1 ] , "call-1" ) ;
235
+ assertEquals ( onCacheUpdateErrorSpy . calls [ 0 ] . args [ 2 ] , [ "arg-1" ] ) ;
219
236
} ) ;
220
237
221
238
it ( "should call onCacheUpdateError if errors occurred while calling shouldIgnoreResult" , async ( ) => {
239
+ const promiseFn = createMockPromiseFn ( ) ;
222
240
const shouldIgnoreResultSpy = spy ( ( ) => {
223
241
throw new Error ( "test" ) ;
224
242
} ) ;
225
- const onCacheUpdateErrorSpy = spy ( ( _error : unknown ) => { } ) ;
226
- const promiseFn = createMockPromiseFn ( ) ;
243
+ const onCacheUpdateError : RememberPromiseOptions <
244
+ typeof promiseFn
245
+ > [ "onCacheUpdateError" ] = ( ) => { } ;
246
+ const onCacheUpdateErrorSpy = spy ( onCacheUpdateError ) ;
247
+
227
248
const cachedPromiseFn = rememberPromise ( promiseFn , {
228
249
shouldIgnoreResult : shouldIgnoreResultSpy ,
229
250
onCacheUpdateError : onCacheUpdateErrorSpy ,
230
251
} ) ;
231
252
232
- assertEquals ( await cachedPromiseFn ( ) , "call-1" ) ;
253
+ assertEquals ( await cachedPromiseFn ( "arg-1" ) , "call-1" ) ;
233
254
assertSpyCalls ( promiseFn , 1 ) ;
234
255
assertSpyCalls ( shouldIgnoreResultSpy , 1 ) ;
235
256
assertSpyCalls ( onCacheUpdateErrorSpy , 1 ) ;
236
257
assertIsError ( onCacheUpdateErrorSpy . calls [ 0 ] . args [ 0 ] , Error , "test" ) ;
258
+ assertEquals ( onCacheUpdateErrorSpy . calls [ 0 ] . args [ 1 ] , "call-1" ) ;
259
+ assertEquals ( onCacheUpdateErrorSpy . calls [ 0 ] . args [ 2 ] , [ "arg-1" ] ) ;
237
260
} ) ;
238
261
} ) ;
239
262
} ) ;
240
263
241
264
describe ( "shouldIgnoreResult" , ( ) => {
242
265
it ( "should update cache if shouldIgnoreResult returns false" , async ( ) => {
243
266
const promiseFn = createMockPromiseFn ( ) ;
267
+
244
268
const cachedPromiseFn = rememberPromise ( promiseFn , {
245
269
shouldIgnoreResult : ( result ) => result !== "call-1" ,
246
270
ttl : 0 ,
@@ -253,6 +277,7 @@ describe("shouldIgnoreResult", () => {
253
277
254
278
it ( "should not update cache if shouldIgnoreResult returns true" , async ( ) => {
255
279
const promiseFn = createMockPromiseFn ( ) ;
280
+
256
281
const cachedPromiseFn = rememberPromise ( promiseFn , {
257
282
shouldIgnoreResult : ( result ) => result === "call-1" ,
258
283
ttl : 0 ,
0 commit comments