@isograph/react-disposable-state 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CacheItem.d.ts +18 -14
- package/dist/CacheItem.js +65 -61
- package/dist/ParentCache.d.ts +2 -2
- package/dist/ParentCache.js +1 -1
- package/dist/index.d.ts +8 -8
- package/dist/useCachedPrecommitValue.d.ts +2 -2
- package/dist/useCachedPrecommitValue.js +1 -1
- package/dist/useDisposableState.d.ts +3 -3
- package/dist/useDisposableState.js +6 -4
- package/dist/useHasCommittedRef.d.ts +1 -1
- package/dist/useLazyDisposableState.d.ts +1 -1
- package/dist/useLazyDisposableState.js +3 -3
- package/dist/useUpdatableDisposableState.d.ts +1 -1
- package/dist/useUpdatableDisposableState.js +3 -3
- package/docs/managing-complex-state.md +4 -4
- package/package.json +7 -2
- package/src/CacheItem.test.ts +94 -94
- package/src/CacheItem.ts +80 -76
- package/src/ParentCache.test.ts +31 -27
- package/src/ParentCache.ts +3 -3
- package/src/index.ts +8 -8
- package/src/useCachedPrecommitValue.test.tsx +57 -73
- package/src/useCachedPrecommitValue.ts +7 -7
- package/src/useDisposableState.ts +13 -11
- package/src/useHasCommittedRef.ts +1 -1
- package/src/useLazyDisposableState.ts +7 -7
- package/src/useUpdatableDisposableState.test.tsx +334 -327
- package/src/useUpdatableDisposableState.ts +9 -9
package/src/CacheItem.test.ts
CHANGED
@@ -6,19 +6,19 @@ import {
|
|
6
6
|
beforeEach,
|
7
7
|
afterEach,
|
8
8
|
expect,
|
9
|
-
} from
|
9
|
+
} from 'vitest';
|
10
10
|
import {
|
11
11
|
CacheItem,
|
12
12
|
CacheItemState,
|
13
13
|
createTemporarilyRetainedCacheItem,
|
14
|
-
} from
|
15
|
-
import { ItemCleanupPair } from
|
14
|
+
} from './CacheItem';
|
15
|
+
import { ItemCleanupPair } from '@isograph/disposable-types';
|
16
16
|
|
17
17
|
function getState<T>(cacheItem: CacheItem<T>): CacheItemState<T> {
|
18
18
|
return (cacheItem as any).__state as CacheItemState<T>;
|
19
19
|
}
|
20
20
|
|
21
|
-
describe(
|
21
|
+
describe('CacheItem', () => {
|
22
22
|
beforeEach(() => {
|
23
23
|
vi.useFakeTimers();
|
24
24
|
});
|
@@ -26,8 +26,8 @@ describe("CacheItem", () => {
|
|
26
26
|
vi.useRealTimers();
|
27
27
|
});
|
28
28
|
|
29
|
-
describe(
|
30
|
-
test(
|
29
|
+
describe('Item that is temporarily retained once', () => {
|
30
|
+
test('Temporarily retained cache item gets created in the expected state', () => {
|
31
31
|
const removeFromParentCache = vi.fn();
|
32
32
|
const disposeItem = vi.fn();
|
33
33
|
const factory = vi.fn(() => {
|
@@ -37,13 +37,13 @@ describe("CacheItem", () => {
|
|
37
37
|
const [cacheItem, _disposeTemporaryRetain] =
|
38
38
|
createTemporarilyRetainedCacheItem<number>(
|
39
39
|
factory,
|
40
|
-
removeFromParentCache
|
40
|
+
removeFromParentCache,
|
41
41
|
);
|
42
42
|
|
43
43
|
expect(factory.mock.calls.length).toEqual(1);
|
44
44
|
|
45
45
|
const state = getState(cacheItem);
|
46
|
-
assert(state.kind ===
|
46
|
+
assert(state.kind === 'InParentCacheAndNotDisposed');
|
47
47
|
expect(state.value).toEqual(1);
|
48
48
|
expect(state.permanentRetainCount).toEqual(0);
|
49
49
|
expect(state.temporaryRetainCount).toEqual(1);
|
@@ -53,7 +53,7 @@ describe("CacheItem", () => {
|
|
53
53
|
expect(removeFromParentCache.mock.calls.length).toEqual(0);
|
54
54
|
});
|
55
55
|
|
56
|
-
test(
|
56
|
+
test('Disposal of temporarily retained cache item', () => {
|
57
57
|
const removeFromParentCache = vi.fn();
|
58
58
|
const disposeItem = vi.fn();
|
59
59
|
const factory = vi.fn(() => {
|
@@ -63,7 +63,7 @@ describe("CacheItem", () => {
|
|
63
63
|
const [cacheItem, disposeTemporaryRetain] =
|
64
64
|
createTemporarilyRetainedCacheItem<number>(
|
65
65
|
factory,
|
66
|
-
removeFromParentCache
|
66
|
+
removeFromParentCache,
|
67
67
|
);
|
68
68
|
|
69
69
|
expect(factory.mock.calls.length).toEqual(1);
|
@@ -72,12 +72,12 @@ describe("CacheItem", () => {
|
|
72
72
|
|
73
73
|
const state = getState(cacheItem);
|
74
74
|
|
75
|
-
expect(state.kind).toEqual(
|
75
|
+
expect(state.kind).toEqual('NotInParentCacheAndDisposed');
|
76
76
|
expect(removeFromParentCache.mock.calls.length).toEqual(1);
|
77
77
|
expect(disposeItem.mock.calls.length).toEqual(1);
|
78
78
|
});
|
79
79
|
|
80
|
-
test(
|
80
|
+
test('Expiration of temporarily retained cache item', () => {
|
81
81
|
const removeFromParentCache = vi.fn();
|
82
82
|
const disposeItem = vi.fn();
|
83
83
|
|
@@ -88,7 +88,7 @@ describe("CacheItem", () => {
|
|
88
88
|
const [cacheItem, _disposeTemporaryRetain] =
|
89
89
|
createTemporarilyRetainedCacheItem<number>(
|
90
90
|
factory,
|
91
|
-
removeFromParentCache
|
91
|
+
removeFromParentCache,
|
92
92
|
);
|
93
93
|
|
94
94
|
expect(factory.mock.calls.length).toEqual(1);
|
@@ -97,12 +97,12 @@ describe("CacheItem", () => {
|
|
97
97
|
|
98
98
|
const state = getState(cacheItem);
|
99
99
|
|
100
|
-
assert(state.kind ===
|
100
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
101
101
|
expect(removeFromParentCache.mock.calls.length).toEqual(1);
|
102
102
|
expect(disposeItem.mock.calls.length).toEqual(1);
|
103
103
|
});
|
104
104
|
|
105
|
-
test(
|
105
|
+
test('Disposal of expired temporarily retained cache item', () => {
|
106
106
|
const removeFromParentCache = vi.fn();
|
107
107
|
const disposeItem = vi.fn();
|
108
108
|
|
@@ -113,7 +113,7 @@ describe("CacheItem", () => {
|
|
113
113
|
const [cacheItem, disposeTemporaryRetain] =
|
114
114
|
createTemporarilyRetainedCacheItem<number>(
|
115
115
|
factory,
|
116
|
-
removeFromParentCache
|
116
|
+
removeFromParentCache,
|
117
117
|
);
|
118
118
|
|
119
119
|
expect(factory.mock.calls.length).toEqual(1);
|
@@ -122,7 +122,7 @@ describe("CacheItem", () => {
|
|
122
122
|
|
123
123
|
const state = getState(cacheItem);
|
124
124
|
|
125
|
-
assert(state.kind ===
|
125
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
126
126
|
expect(removeFromParentCache.mock.calls.length).toEqual(1);
|
127
127
|
expect(disposeItem.mock.calls.length).toEqual(1);
|
128
128
|
|
@@ -131,7 +131,7 @@ describe("CacheItem", () => {
|
|
131
131
|
}).not.toThrow();
|
132
132
|
});
|
133
133
|
|
134
|
-
test(
|
134
|
+
test('Repeated disposal of temporarily retained cache item', () => {
|
135
135
|
const removeFromParentCache = vi.fn();
|
136
136
|
const disposeItem = vi.fn();
|
137
137
|
|
@@ -142,7 +142,7 @@ describe("CacheItem", () => {
|
|
142
142
|
const [cacheItem, disposeTemporaryRetain] =
|
143
143
|
createTemporarilyRetainedCacheItem<number>(
|
144
144
|
factory,
|
145
|
-
removeFromParentCache
|
145
|
+
removeFromParentCache,
|
146
146
|
);
|
147
147
|
|
148
148
|
expect(factory.mock.calls.length).toEqual(1);
|
@@ -150,7 +150,7 @@ describe("CacheItem", () => {
|
|
150
150
|
disposeTemporaryRetain();
|
151
151
|
const state = getState(cacheItem);
|
152
152
|
|
153
|
-
assert(state.kind ===
|
153
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
154
154
|
expect(removeFromParentCache.mock.calls.length).toEqual(1);
|
155
155
|
expect(disposeItem.mock.calls.length).toEqual(1);
|
156
156
|
|
@@ -160,8 +160,8 @@ describe("CacheItem", () => {
|
|
160
160
|
});
|
161
161
|
});
|
162
162
|
|
163
|
-
describe(
|
164
|
-
test(
|
163
|
+
describe('Item that is temporarily retained once and then permanently retained', () => {
|
164
|
+
test('Permanent retain removes the item from the parent', () => {
|
165
165
|
const removeFromParentCache = vi.fn();
|
166
166
|
const disposeItem = vi.fn();
|
167
167
|
|
@@ -172,7 +172,7 @@ describe("CacheItem", () => {
|
|
172
172
|
const [cacheItem, disposeTemporaryRetain] =
|
173
173
|
createTemporarilyRetainedCacheItem<number>(
|
174
174
|
factory,
|
175
|
-
removeFromParentCache
|
175
|
+
removeFromParentCache,
|
176
176
|
);
|
177
177
|
|
178
178
|
const mockedDisposeTemporaryRetain = vi.fn(disposeTemporaryRetain);
|
@@ -187,7 +187,7 @@ describe("CacheItem", () => {
|
|
187
187
|
|
188
188
|
const state = getState(cacheItem);
|
189
189
|
|
190
|
-
assert(state.kind ===
|
190
|
+
assert(state.kind === 'NotInParentCacheAndNotDisposed');
|
191
191
|
expect(state.disposeValue).toEqual(disposeItem);
|
192
192
|
expect(state.permanentRetainCount).toEqual(1);
|
193
193
|
expect(state.value).toEqual(1);
|
@@ -195,7 +195,7 @@ describe("CacheItem", () => {
|
|
195
195
|
expect(removeFromParentCache.mock.calls.length).toEqual(1);
|
196
196
|
});
|
197
197
|
|
198
|
-
test(
|
198
|
+
test('Disposing the temporary retain after permanent retain throws', () => {
|
199
199
|
const removeFromParentCache = vi.fn();
|
200
200
|
const disposeItem = vi.fn();
|
201
201
|
|
@@ -206,7 +206,7 @@ describe("CacheItem", () => {
|
|
206
206
|
const [cacheItem, disposeTemporaryRetain] =
|
207
207
|
createTemporarilyRetainedCacheItem<number>(
|
208
208
|
factory,
|
209
|
-
removeFromParentCache
|
209
|
+
removeFromParentCache,
|
210
210
|
);
|
211
211
|
|
212
212
|
expect(factory.mock.calls.length).toEqual(1);
|
@@ -219,7 +219,7 @@ describe("CacheItem", () => {
|
|
219
219
|
}).toThrow();
|
220
220
|
});
|
221
221
|
|
222
|
-
test(
|
222
|
+
test('Item is disposed when the permanently retain is disposed', () => {
|
223
223
|
const removeFromParentCache = vi.fn();
|
224
224
|
const disposeItem = vi.fn();
|
225
225
|
|
@@ -230,7 +230,7 @@ describe("CacheItem", () => {
|
|
230
230
|
const [cacheItem, disposeTemporaryRetain] =
|
231
231
|
createTemporarilyRetainedCacheItem<number>(
|
232
232
|
factory,
|
233
|
-
removeFromParentCache
|
233
|
+
removeFromParentCache,
|
234
234
|
);
|
235
235
|
|
236
236
|
const mockedDisposeTemporaryRetain = vi.fn(disposeTemporaryRetain);
|
@@ -243,12 +243,12 @@ describe("CacheItem", () => {
|
|
243
243
|
disposePermanentRetain();
|
244
244
|
|
245
245
|
const state = getState(cacheItem);
|
246
|
-
assert(state.kind ===
|
246
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
247
247
|
expect(removeFromParentCache.mock.calls.length).toEqual(1);
|
248
248
|
expect(disposeItem.mock.calls.length).toEqual(1);
|
249
249
|
});
|
250
250
|
|
251
|
-
test(
|
251
|
+
test('Repeated disposal of permanently retained cache item throws', () => {
|
252
252
|
const removeFromParentCache = vi.fn();
|
253
253
|
const disposeItem = vi.fn();
|
254
254
|
|
@@ -259,7 +259,7 @@ describe("CacheItem", () => {
|
|
259
259
|
const [cacheItem, disposeTemporaryRetain] =
|
260
260
|
createTemporarilyRetainedCacheItem<number>(
|
261
261
|
factory,
|
262
|
-
removeFromParentCache
|
262
|
+
removeFromParentCache,
|
263
263
|
);
|
264
264
|
|
265
265
|
const mockedDisposeTemporaryRetain = vi.fn(disposeTemporaryRetain);
|
@@ -275,7 +275,7 @@ describe("CacheItem", () => {
|
|
275
275
|
}).toThrow();
|
276
276
|
});
|
277
277
|
|
278
|
-
test(
|
278
|
+
test('Permanent retain of cache item whose temporary retain has expired', () => {
|
279
279
|
const removeFromParentCache = vi.fn();
|
280
280
|
const disposeItem = vi.fn();
|
281
281
|
|
@@ -286,7 +286,7 @@ describe("CacheItem", () => {
|
|
286
286
|
const [cacheItem, disposeTemporaryRetain] =
|
287
287
|
createTemporarilyRetainedCacheItem<number>(
|
288
288
|
factory,
|
289
|
-
removeFromParentCache
|
289
|
+
removeFromParentCache,
|
290
290
|
);
|
291
291
|
|
292
292
|
expect(factory.mock.calls.length).toEqual(1);
|
@@ -295,10 +295,10 @@ describe("CacheItem", () => {
|
|
295
295
|
|
296
296
|
const state = getState(cacheItem);
|
297
297
|
|
298
|
-
assert(state.kind ===
|
298
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
299
299
|
|
300
300
|
assert(
|
301
|
-
cacheItem.permanentRetainIfNotDisposed(disposeTemporaryRetain) === null
|
301
|
+
cacheItem.permanentRetainIfNotDisposed(disposeTemporaryRetain) === null,
|
302
302
|
);
|
303
303
|
|
304
304
|
expect(() => {
|
@@ -306,7 +306,7 @@ describe("CacheItem", () => {
|
|
306
306
|
}).toThrow();
|
307
307
|
});
|
308
308
|
|
309
|
-
test(
|
309
|
+
test('It is invalid to temporarily retain after the permanent retain', () => {
|
310
310
|
const removeFromParentCache = vi.fn();
|
311
311
|
const disposeItem = vi.fn();
|
312
312
|
|
@@ -317,7 +317,7 @@ describe("CacheItem", () => {
|
|
317
317
|
const [cacheItem, disposeTemporaryRetain1] =
|
318
318
|
createTemporarilyRetainedCacheItem<number>(
|
319
319
|
factory,
|
320
|
-
removeFromParentCache
|
320
|
+
removeFromParentCache,
|
321
321
|
);
|
322
322
|
|
323
323
|
const [_value, disposeOfPermanentRetain1] =
|
@@ -329,8 +329,8 @@ describe("CacheItem", () => {
|
|
329
329
|
});
|
330
330
|
});
|
331
331
|
|
332
|
-
describe(
|
333
|
-
test(
|
332
|
+
describe('Item that temporarily retained twice', () => {
|
333
|
+
test('Cache item is not disposed after first temporary retain expires', () => {
|
334
334
|
const removeFromParentCache = vi.fn();
|
335
335
|
const disposeItem = vi.fn();
|
336
336
|
|
@@ -341,7 +341,7 @@ describe("CacheItem", () => {
|
|
341
341
|
const [cacheItem, _disposeTemporaryRetain1] =
|
342
342
|
createTemporarilyRetainedCacheItem<number>(
|
343
343
|
factory,
|
344
|
-
removeFromParentCache
|
344
|
+
removeFromParentCache,
|
345
345
|
);
|
346
346
|
|
347
347
|
vi.advanceTimersByTime(1000);
|
@@ -351,7 +351,7 @@ describe("CacheItem", () => {
|
|
351
351
|
vi.advanceTimersToNextTimer();
|
352
352
|
|
353
353
|
const state = getState(cacheItem);
|
354
|
-
assert(state.kind ===
|
354
|
+
assert(state.kind === 'InParentCacheAndNotDisposed');
|
355
355
|
expect(state.value).toEqual(1);
|
356
356
|
expect(state.permanentRetainCount).toEqual(0);
|
357
357
|
expect(state.temporaryRetainCount).toEqual(1);
|
@@ -361,7 +361,7 @@ describe("CacheItem", () => {
|
|
361
361
|
expect(removeFromParentCache.mock.calls.length).toEqual(0);
|
362
362
|
});
|
363
363
|
|
364
|
-
test(
|
364
|
+
test('Cache item is not disposed after first temporary retain is disposed', () => {
|
365
365
|
const removeFromParentCache = vi.fn();
|
366
366
|
const disposeItem = vi.fn();
|
367
367
|
|
@@ -372,7 +372,7 @@ describe("CacheItem", () => {
|
|
372
372
|
const [cacheItem, disposeTemporaryRetain1] =
|
373
373
|
createTemporarilyRetainedCacheItem<number>(
|
374
374
|
factory,
|
375
|
-
removeFromParentCache
|
375
|
+
removeFromParentCache,
|
376
376
|
);
|
377
377
|
|
378
378
|
vi.advanceTimersByTime(1000);
|
@@ -381,7 +381,7 @@ describe("CacheItem", () => {
|
|
381
381
|
disposeTemporaryRetain1();
|
382
382
|
|
383
383
|
const state = getState(cacheItem);
|
384
|
-
assert(state.kind ===
|
384
|
+
assert(state.kind === 'InParentCacheAndNotDisposed');
|
385
385
|
expect(state.value).toEqual(1);
|
386
386
|
expect(state.permanentRetainCount).toEqual(0);
|
387
387
|
expect(state.temporaryRetainCount).toEqual(1);
|
@@ -391,7 +391,7 @@ describe("CacheItem", () => {
|
|
391
391
|
expect(removeFromParentCache.mock.calls.length).toEqual(0);
|
392
392
|
});
|
393
393
|
|
394
|
-
test(
|
394
|
+
test('Disposing the first temporary retain after it expires is a no-op', () => {
|
395
395
|
const removeFromParentCache = vi.fn();
|
396
396
|
const disposeItem = vi.fn();
|
397
397
|
|
@@ -402,7 +402,7 @@ describe("CacheItem", () => {
|
|
402
402
|
const [cacheItem, disposeTemporaryRetain1] =
|
403
403
|
createTemporarilyRetainedCacheItem<number>(
|
404
404
|
factory,
|
405
|
-
removeFromParentCache
|
405
|
+
removeFromParentCache,
|
406
406
|
);
|
407
407
|
|
408
408
|
vi.advanceTimersByTime(1000);
|
@@ -411,11 +411,11 @@ describe("CacheItem", () => {
|
|
411
411
|
disposeTemporaryRetain1();
|
412
412
|
|
413
413
|
const state = getState(cacheItem);
|
414
|
-
assert(state.kind ===
|
414
|
+
assert(state.kind === 'InParentCacheAndNotDisposed');
|
415
415
|
expect(state.temporaryRetainCount).toEqual(1);
|
416
416
|
});
|
417
417
|
|
418
|
-
test(
|
418
|
+
test('Item is not disposed if only the second temporary retain is disposed', () => {
|
419
419
|
const removeFromParentCache = vi.fn();
|
420
420
|
const disposeItem = vi.fn();
|
421
421
|
|
@@ -426,7 +426,7 @@ describe("CacheItem", () => {
|
|
426
426
|
const [cacheItem, disposeTemporaryRetain1] =
|
427
427
|
createTemporarilyRetainedCacheItem<number>(
|
428
428
|
factory,
|
429
|
-
removeFromParentCache
|
429
|
+
removeFromParentCache,
|
430
430
|
);
|
431
431
|
|
432
432
|
vi.advanceTimersByTime(1000);
|
@@ -434,11 +434,11 @@ describe("CacheItem", () => {
|
|
434
434
|
disposeTemporaryRetain2();
|
435
435
|
|
436
436
|
const state = getState(cacheItem);
|
437
|
-
assert(state.kind ===
|
437
|
+
assert(state.kind === 'InParentCacheAndNotDisposed');
|
438
438
|
expect(state.temporaryRetainCount).toEqual(1);
|
439
439
|
});
|
440
440
|
|
441
|
-
test(
|
441
|
+
test('Item is disposed if both temporary retains are disposed', () => {
|
442
442
|
const removeFromParentCache = vi.fn();
|
443
443
|
const disposeItem = vi.fn();
|
444
444
|
|
@@ -449,22 +449,22 @@ describe("CacheItem", () => {
|
|
449
449
|
const [cacheItem, disposeTemporaryRetain1] =
|
450
450
|
createTemporarilyRetainedCacheItem<number>(
|
451
451
|
factory,
|
452
|
-
removeFromParentCache
|
452
|
+
removeFromParentCache,
|
453
453
|
);
|
454
454
|
|
455
455
|
const disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
456
456
|
const state1 = getState(cacheItem);
|
457
|
-
assert(state1.kind ===
|
457
|
+
assert(state1.kind === 'InParentCacheAndNotDisposed');
|
458
458
|
expect(state1.temporaryRetainCount).toEqual(2);
|
459
459
|
disposeTemporaryRetain1();
|
460
460
|
expect(state1.temporaryRetainCount).toEqual(1);
|
461
461
|
disposeTemporaryRetain2();
|
462
462
|
|
463
463
|
const state2 = getState(cacheItem);
|
464
|
-
assert(state2.kind ===
|
464
|
+
assert(state2.kind === 'NotInParentCacheAndDisposed');
|
465
465
|
});
|
466
466
|
|
467
|
-
test(
|
467
|
+
test('Item is disposed if both temporary retains are disposed in reverse order', () => {
|
468
468
|
const removeFromParentCache = vi.fn();
|
469
469
|
const disposeItem = vi.fn();
|
470
470
|
|
@@ -475,22 +475,22 @@ describe("CacheItem", () => {
|
|
475
475
|
const [cacheItem, disposeTemporaryRetain1] =
|
476
476
|
createTemporarilyRetainedCacheItem<number>(
|
477
477
|
factory,
|
478
|
-
removeFromParentCache
|
478
|
+
removeFromParentCache,
|
479
479
|
);
|
480
480
|
|
481
481
|
const disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
482
482
|
const state1 = getState(cacheItem);
|
483
|
-
assert(state1.kind ===
|
483
|
+
assert(state1.kind === 'InParentCacheAndNotDisposed');
|
484
484
|
expect(state1.temporaryRetainCount).toEqual(2);
|
485
485
|
disposeTemporaryRetain2();
|
486
486
|
expect(state1.temporaryRetainCount).toEqual(1);
|
487
487
|
disposeTemporaryRetain1();
|
488
488
|
|
489
489
|
const state2 = getState(cacheItem);
|
490
|
-
assert(state2.kind ===
|
490
|
+
assert(state2.kind === 'NotInParentCacheAndDisposed');
|
491
491
|
});
|
492
492
|
|
493
|
-
test(
|
493
|
+
test('Item is disposed if both temporary retains expire', () => {
|
494
494
|
const removeFromParentCache = vi.fn();
|
495
495
|
const disposeItem = vi.fn();
|
496
496
|
|
@@ -501,14 +501,14 @@ describe("CacheItem", () => {
|
|
501
501
|
const [cacheItem, _disposeTemporaryRetain1] =
|
502
502
|
createTemporarilyRetainedCacheItem<number>(
|
503
503
|
factory,
|
504
|
-
removeFromParentCache
|
504
|
+
removeFromParentCache,
|
505
505
|
);
|
506
506
|
|
507
507
|
vi.advanceTimersByTime(1000);
|
508
508
|
const _disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
509
509
|
|
510
510
|
const state1 = getState(cacheItem);
|
511
|
-
assert(state1.kind ===
|
511
|
+
assert(state1.kind === 'InParentCacheAndNotDisposed');
|
512
512
|
expect(state1.temporaryRetainCount).toEqual(2);
|
513
513
|
|
514
514
|
vi.advanceTimersToNextTimer();
|
@@ -516,14 +516,14 @@ describe("CacheItem", () => {
|
|
516
516
|
|
517
517
|
vi.advanceTimersToNextTimer();
|
518
518
|
const state = getState(cacheItem);
|
519
|
-
assert(state.kind ===
|
519
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
520
520
|
expect(disposeItem.mock.calls.length).toEqual(1);
|
521
521
|
expect(removeFromParentCache.mock.calls.length).toEqual(1);
|
522
522
|
});
|
523
523
|
});
|
524
524
|
|
525
|
-
describe(
|
526
|
-
test(
|
525
|
+
describe('Item that is temporarily retained twice and then permanently retained', () => {
|
526
|
+
test('Item is not removed from the parent cache when the permanent retain is created', () => {
|
527
527
|
const removeFromParentCache = vi.fn();
|
528
528
|
const disposeItem = vi.fn();
|
529
529
|
|
@@ -534,7 +534,7 @@ describe("CacheItem", () => {
|
|
534
534
|
const [cacheItem, disposeTemporaryRetain1] =
|
535
535
|
createTemporarilyRetainedCacheItem<number>(
|
536
536
|
factory,
|
537
|
-
removeFromParentCache
|
537
|
+
removeFromParentCache,
|
538
538
|
);
|
539
539
|
|
540
540
|
vi.advanceTimersByTime(1000);
|
@@ -545,13 +545,13 @@ describe("CacheItem", () => {
|
|
545
545
|
|
546
546
|
const state = getState(cacheItem);
|
547
547
|
|
548
|
-
assert(state.kind ===
|
548
|
+
assert(state.kind === 'InParentCacheAndNotDisposed');
|
549
549
|
expect(state.value).toEqual(1);
|
550
550
|
expect(state.permanentRetainCount).toEqual(1);
|
551
551
|
expect(state.temporaryRetainCount).toEqual(1);
|
552
552
|
});
|
553
553
|
|
554
|
-
test(
|
554
|
+
test('Item is removed from the parent cache when the second temporary retain is disposed', () => {
|
555
555
|
const removeFromParentCache = vi.fn();
|
556
556
|
const disposeItem = vi.fn();
|
557
557
|
|
@@ -562,7 +562,7 @@ describe("CacheItem", () => {
|
|
562
562
|
const [cacheItem, disposeTemporaryRetain1] =
|
563
563
|
createTemporarilyRetainedCacheItem<number>(
|
564
564
|
factory,
|
565
|
-
removeFromParentCache
|
565
|
+
removeFromParentCache,
|
566
566
|
);
|
567
567
|
|
568
568
|
vi.advanceTimersByTime(1000);
|
@@ -572,18 +572,18 @@ describe("CacheItem", () => {
|
|
572
572
|
cacheItem.permanentRetainIfNotDisposed(disposeTemporaryRetain1)!;
|
573
573
|
|
574
574
|
const state1 = getState(cacheItem);
|
575
|
-
assert(state1.kind ===
|
575
|
+
assert(state1.kind === 'InParentCacheAndNotDisposed');
|
576
576
|
expect(state1.permanentRetainCount).toEqual(1);
|
577
577
|
expect(state1.temporaryRetainCount).toEqual(1);
|
578
578
|
|
579
579
|
disposeTemporaryRetain2();
|
580
580
|
|
581
581
|
const state2 = getState(cacheItem);
|
582
|
-
assert(state2.kind ===
|
582
|
+
assert(state2.kind === 'NotInParentCacheAndNotDisposed');
|
583
583
|
expect(state2.permanentRetainCount).toEqual(1);
|
584
584
|
});
|
585
585
|
|
586
|
-
test(
|
586
|
+
test('Item is removed from the parent cache when the second temporary retain expires', () => {
|
587
587
|
const removeFromParentCache = vi.fn();
|
588
588
|
const disposeItem = vi.fn();
|
589
589
|
|
@@ -594,7 +594,7 @@ describe("CacheItem", () => {
|
|
594
594
|
const [cacheItem, disposeTemporaryRetain1] =
|
595
595
|
createTemporarilyRetainedCacheItem<number>(
|
596
596
|
factory,
|
597
|
-
removeFromParentCache
|
597
|
+
removeFromParentCache,
|
598
598
|
);
|
599
599
|
|
600
600
|
vi.advanceTimersByTime(1000);
|
@@ -604,18 +604,18 @@ describe("CacheItem", () => {
|
|
604
604
|
cacheItem.permanentRetainIfNotDisposed(disposeTemporaryRetain1)!;
|
605
605
|
|
606
606
|
const state1 = getState(cacheItem);
|
607
|
-
assert(state1.kind ===
|
607
|
+
assert(state1.kind === 'InParentCacheAndNotDisposed');
|
608
608
|
expect(state1.permanentRetainCount).toEqual(1);
|
609
609
|
expect(state1.temporaryRetainCount).toEqual(1);
|
610
610
|
|
611
611
|
vi.advanceTimersToNextTimer();
|
612
612
|
|
613
613
|
const state2 = getState(cacheItem);
|
614
|
-
assert(state2.kind ===
|
614
|
+
assert(state2.kind === 'NotInParentCacheAndNotDisposed');
|
615
615
|
expect(state2.permanentRetainCount).toEqual(1);
|
616
616
|
});
|
617
617
|
|
618
|
-
test(
|
618
|
+
test('Item is not removed from the parent cache when the permanent retain is disposed', () => {
|
619
619
|
const removeFromParentCache = vi.fn();
|
620
620
|
const disposeItem = vi.fn();
|
621
621
|
|
@@ -626,7 +626,7 @@ describe("CacheItem", () => {
|
|
626
626
|
const [cacheItem, disposeTemporaryRetain1] =
|
627
627
|
createTemporarilyRetainedCacheItem<number>(
|
628
628
|
factory,
|
629
|
-
removeFromParentCache
|
629
|
+
removeFromParentCache,
|
630
630
|
);
|
631
631
|
|
632
632
|
vi.advanceTimersByTime(1000);
|
@@ -638,12 +638,12 @@ describe("CacheItem", () => {
|
|
638
638
|
disposeOfPermanentRetain();
|
639
639
|
|
640
640
|
const state = getState(cacheItem);
|
641
|
-
assert(state.kind ===
|
641
|
+
assert(state.kind === 'InParentCacheAndNotDisposed');
|
642
642
|
expect(state.permanentRetainCount).toEqual(0);
|
643
643
|
expect(state.temporaryRetainCount).toEqual(1);
|
644
644
|
});
|
645
645
|
|
646
|
-
test(
|
646
|
+
test('Item is disposed the permanent retain is disposed and the second temporary retain expires', () => {
|
647
647
|
const removeFromParentCache = vi.fn();
|
648
648
|
const disposeItem = vi.fn();
|
649
649
|
|
@@ -654,7 +654,7 @@ describe("CacheItem", () => {
|
|
654
654
|
const [cacheItem, disposeTemporaryRetain1] =
|
655
655
|
createTemporarilyRetainedCacheItem<number>(
|
656
656
|
factory,
|
657
|
-
removeFromParentCache
|
657
|
+
removeFromParentCache,
|
658
658
|
);
|
659
659
|
|
660
660
|
vi.advanceTimersByTime(1000);
|
@@ -667,10 +667,10 @@ describe("CacheItem", () => {
|
|
667
667
|
vi.advanceTimersToNextTimer();
|
668
668
|
|
669
669
|
const state = getState(cacheItem);
|
670
|
-
assert(state.kind ===
|
670
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
671
671
|
});
|
672
672
|
|
673
|
-
test(
|
673
|
+
test('Item is disposed when the permanent retain is disposed and the second temporary retain is disposed', () => {
|
674
674
|
const removeFromParentCache = vi.fn();
|
675
675
|
const disposeItem = vi.fn();
|
676
676
|
|
@@ -681,7 +681,7 @@ describe("CacheItem", () => {
|
|
681
681
|
const [cacheItem, disposeTemporaryRetain1] =
|
682
682
|
createTemporarilyRetainedCacheItem<number>(
|
683
683
|
factory,
|
684
|
-
removeFromParentCache
|
684
|
+
removeFromParentCache,
|
685
685
|
);
|
686
686
|
|
687
687
|
vi.advanceTimersByTime(1000);
|
@@ -694,10 +694,10 @@ describe("CacheItem", () => {
|
|
694
694
|
disposeTemporaryRetain2();
|
695
695
|
|
696
696
|
const state = getState(cacheItem);
|
697
|
-
assert(state.kind ===
|
697
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
698
698
|
});
|
699
699
|
|
700
|
-
test(
|
700
|
+
test('Item is disposed when the second temporary is disposed and the permanent retain is disposed', () => {
|
701
701
|
const removeFromParentCache = vi.fn();
|
702
702
|
const disposeItem = vi.fn();
|
703
703
|
|
@@ -708,7 +708,7 @@ describe("CacheItem", () => {
|
|
708
708
|
const [cacheItem, disposeTemporaryRetain1] =
|
709
709
|
createTemporarilyRetainedCacheItem<number>(
|
710
710
|
factory,
|
711
|
-
removeFromParentCache
|
711
|
+
removeFromParentCache,
|
712
712
|
);
|
713
713
|
|
714
714
|
vi.advanceTimersByTime(1000);
|
@@ -721,10 +721,10 @@ describe("CacheItem", () => {
|
|
721
721
|
disposeOfPermanentRetain();
|
722
722
|
|
723
723
|
const state = getState(cacheItem);
|
724
|
-
assert(state.kind ===
|
724
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
725
725
|
});
|
726
726
|
|
727
|
-
test(
|
727
|
+
test('Item is disposed when the second temporary expires and the permanent retain is disposed', () => {
|
728
728
|
const removeFromParentCache = vi.fn();
|
729
729
|
const disposeItem = vi.fn();
|
730
730
|
|
@@ -735,7 +735,7 @@ describe("CacheItem", () => {
|
|
735
735
|
const [cacheItem, disposeTemporaryRetain1] =
|
736
736
|
createTemporarilyRetainedCacheItem<number>(
|
737
737
|
factory,
|
738
|
-
removeFromParentCache
|
738
|
+
removeFromParentCache,
|
739
739
|
);
|
740
740
|
|
741
741
|
vi.advanceTimersByTime(1000);
|
@@ -748,12 +748,12 @@ describe("CacheItem", () => {
|
|
748
748
|
disposeOfPermanentRetain();
|
749
749
|
|
750
750
|
const state = getState(cacheItem);
|
751
|
-
assert(state.kind ===
|
751
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
752
752
|
});
|
753
753
|
});
|
754
754
|
|
755
|
-
describe(
|
756
|
-
test(
|
755
|
+
describe('Multiple permanent retains', () => {
|
756
|
+
test('Item is only disposed when second permanent retain is disposed', () => {
|
757
757
|
const removeFromParentCache = vi.fn();
|
758
758
|
const disposeItem = vi.fn();
|
759
759
|
|
@@ -764,7 +764,7 @@ describe("CacheItem", () => {
|
|
764
764
|
const [cacheItem, disposeTemporaryRetain1] =
|
765
765
|
createTemporarilyRetainedCacheItem<number>(
|
766
766
|
factory,
|
767
|
-
removeFromParentCache
|
767
|
+
removeFromParentCache,
|
768
768
|
);
|
769
769
|
|
770
770
|
vi.advanceTimersByTime(1000);
|
@@ -777,12 +777,12 @@ describe("CacheItem", () => {
|
|
777
777
|
|
778
778
|
disposeOfPermanentRetain1();
|
779
779
|
const state = getState(cacheItem);
|
780
|
-
assert(state.kind ===
|
780
|
+
assert(state.kind === 'NotInParentCacheAndNotDisposed');
|
781
781
|
expect(state.permanentRetainCount).toEqual(1);
|
782
782
|
|
783
783
|
disposeOfPermanentRetain2();
|
784
784
|
const state2 = getState(cacheItem);
|
785
|
-
assert(state2.kind ===
|
785
|
+
assert(state2.kind === 'NotInParentCacheAndDisposed');
|
786
786
|
});
|
787
787
|
});
|
788
788
|
});
|