@isograph/react-disposable-state 0.0.0-main-1cd3db6d → 0.0.0-main-2c275831
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 +4 -4
- package/dist/CacheItem.js +51 -51
- package/dist/ParentCache.d.ts +2 -2
- 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 +5 -7
- 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/package.json +2 -2
- package/src/CacheItem.test.ts +184 -222
- package/src/CacheItem.ts +64 -81
- package/src/ParentCache.test.ts +9 -10
- package/src/ParentCache.ts +7 -9
- package/src/index.ts +8 -8
- package/src/useCachedPrecommitValue.test.tsx +47 -77
- package/src/useCachedPrecommitValue.ts +9 -13
- package/src/useDisposableState.ts +13 -16
- package/src/useHasCommittedRef.ts +1 -1
- package/src/useLazyDisposableState.ts +7 -7
- package/src/useUpdatableDisposableState.test.tsx +24 -33
- package/src/useUpdatableDisposableState.ts +9 -11
package/src/CacheItem.test.ts
CHANGED
@@ -1,24 +1,12 @@
|
|
1
|
-
import {
|
2
|
-
|
3
|
-
|
4
|
-
test,
|
5
|
-
vi,
|
6
|
-
beforeEach,
|
7
|
-
afterEach,
|
8
|
-
expect,
|
9
|
-
} from "vitest";
|
10
|
-
import {
|
11
|
-
CacheItem,
|
12
|
-
CacheItemState,
|
13
|
-
createTemporarilyRetainedCacheItem,
|
14
|
-
} from "./CacheItem";
|
15
|
-
import { ItemCleanupPair } from "@isograph/disposable-types";
|
1
|
+
import { describe, assert, test, vi, beforeEach, afterEach, expect } from 'vitest';
|
2
|
+
import { CacheItem, CacheItemState, createTemporarilyRetainedCacheItem } from './CacheItem';
|
3
|
+
import { ItemCleanupPair } from '@isograph/disposable-types';
|
16
4
|
|
17
5
|
function getState<T>(cacheItem: CacheItem<T>): CacheItemState<T> {
|
18
6
|
return (cacheItem as any).__state as CacheItemState<T>;
|
19
7
|
}
|
20
8
|
|
21
|
-
describe(
|
9
|
+
describe('CacheItem', () => {
|
22
10
|
beforeEach(() => {
|
23
11
|
vi.useFakeTimers();
|
24
12
|
});
|
@@ -26,24 +14,23 @@ describe("CacheItem", () => {
|
|
26
14
|
vi.useRealTimers();
|
27
15
|
});
|
28
16
|
|
29
|
-
describe(
|
30
|
-
test(
|
17
|
+
describe('Item that is temporarily retained once', () => {
|
18
|
+
test('Temporarily retained cache item gets created in the expected state', () => {
|
31
19
|
const removeFromParentCache = vi.fn();
|
32
20
|
const disposeItem = vi.fn();
|
33
21
|
const factory = vi.fn(() => {
|
34
22
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
35
23
|
return ret;
|
36
24
|
});
|
37
|
-
const [cacheItem, _disposeTemporaryRetain] =
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
);
|
25
|
+
const [cacheItem, _disposeTemporaryRetain] = createTemporarilyRetainedCacheItem<number>(
|
26
|
+
factory,
|
27
|
+
removeFromParentCache,
|
28
|
+
);
|
42
29
|
|
43
30
|
expect(factory.mock.calls.length).toEqual(1);
|
44
31
|
|
45
32
|
const state = getState(cacheItem);
|
46
|
-
assert(state.kind ===
|
33
|
+
assert(state.kind === 'InParentCacheAndNotDisposed');
|
47
34
|
expect(state.value).toEqual(1);
|
48
35
|
expect(state.permanentRetainCount).toEqual(0);
|
49
36
|
expect(state.temporaryRetainCount).toEqual(1);
|
@@ -53,18 +40,17 @@ describe("CacheItem", () => {
|
|
53
40
|
expect(removeFromParentCache.mock.calls.length).toEqual(0);
|
54
41
|
});
|
55
42
|
|
56
|
-
test(
|
43
|
+
test('Disposal of temporarily retained cache item', () => {
|
57
44
|
const removeFromParentCache = vi.fn();
|
58
45
|
const disposeItem = vi.fn();
|
59
46
|
const factory = vi.fn(() => {
|
60
47
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
61
48
|
return ret;
|
62
49
|
});
|
63
|
-
const [cacheItem, disposeTemporaryRetain] =
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
);
|
50
|
+
const [cacheItem, disposeTemporaryRetain] = createTemporarilyRetainedCacheItem<number>(
|
51
|
+
factory,
|
52
|
+
removeFromParentCache,
|
53
|
+
);
|
68
54
|
|
69
55
|
expect(factory.mock.calls.length).toEqual(1);
|
70
56
|
|
@@ -72,12 +58,12 @@ describe("CacheItem", () => {
|
|
72
58
|
|
73
59
|
const state = getState(cacheItem);
|
74
60
|
|
75
|
-
expect(state.kind).toEqual(
|
61
|
+
expect(state.kind).toEqual('NotInParentCacheAndDisposed');
|
76
62
|
expect(removeFromParentCache.mock.calls.length).toEqual(1);
|
77
63
|
expect(disposeItem.mock.calls.length).toEqual(1);
|
78
64
|
});
|
79
65
|
|
80
|
-
test(
|
66
|
+
test('Expiration of temporarily retained cache item', () => {
|
81
67
|
const removeFromParentCache = vi.fn();
|
82
68
|
const disposeItem = vi.fn();
|
83
69
|
|
@@ -85,11 +71,10 @@ describe("CacheItem", () => {
|
|
85
71
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
86
72
|
return ret;
|
87
73
|
});
|
88
|
-
const [cacheItem, _disposeTemporaryRetain] =
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
);
|
74
|
+
const [cacheItem, _disposeTemporaryRetain] = createTemporarilyRetainedCacheItem<number>(
|
75
|
+
factory,
|
76
|
+
removeFromParentCache,
|
77
|
+
);
|
93
78
|
|
94
79
|
expect(factory.mock.calls.length).toEqual(1);
|
95
80
|
|
@@ -97,12 +82,12 @@ describe("CacheItem", () => {
|
|
97
82
|
|
98
83
|
const state = getState(cacheItem);
|
99
84
|
|
100
|
-
assert(state.kind ===
|
85
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
101
86
|
expect(removeFromParentCache.mock.calls.length).toEqual(1);
|
102
87
|
expect(disposeItem.mock.calls.length).toEqual(1);
|
103
88
|
});
|
104
89
|
|
105
|
-
test(
|
90
|
+
test('Disposal of expired temporarily retained cache item', () => {
|
106
91
|
const removeFromParentCache = vi.fn();
|
107
92
|
const disposeItem = vi.fn();
|
108
93
|
|
@@ -110,11 +95,10 @@ describe("CacheItem", () => {
|
|
110
95
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
111
96
|
return ret;
|
112
97
|
});
|
113
|
-
const [cacheItem, disposeTemporaryRetain] =
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
);
|
98
|
+
const [cacheItem, disposeTemporaryRetain] = createTemporarilyRetainedCacheItem<number>(
|
99
|
+
factory,
|
100
|
+
removeFromParentCache,
|
101
|
+
);
|
118
102
|
|
119
103
|
expect(factory.mock.calls.length).toEqual(1);
|
120
104
|
|
@@ -122,7 +106,7 @@ describe("CacheItem", () => {
|
|
122
106
|
|
123
107
|
const state = getState(cacheItem);
|
124
108
|
|
125
|
-
assert(state.kind ===
|
109
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
126
110
|
expect(removeFromParentCache.mock.calls.length).toEqual(1);
|
127
111
|
expect(disposeItem.mock.calls.length).toEqual(1);
|
128
112
|
|
@@ -131,7 +115,7 @@ describe("CacheItem", () => {
|
|
131
115
|
}).not.toThrow();
|
132
116
|
});
|
133
117
|
|
134
|
-
test(
|
118
|
+
test('Repeated disposal of temporarily retained cache item', () => {
|
135
119
|
const removeFromParentCache = vi.fn();
|
136
120
|
const disposeItem = vi.fn();
|
137
121
|
|
@@ -139,18 +123,17 @@ describe("CacheItem", () => {
|
|
139
123
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
140
124
|
return ret;
|
141
125
|
});
|
142
|
-
const [cacheItem, disposeTemporaryRetain] =
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
);
|
126
|
+
const [cacheItem, disposeTemporaryRetain] = createTemporarilyRetainedCacheItem<number>(
|
127
|
+
factory,
|
128
|
+
removeFromParentCache,
|
129
|
+
);
|
147
130
|
|
148
131
|
expect(factory.mock.calls.length).toEqual(1);
|
149
132
|
|
150
133
|
disposeTemporaryRetain();
|
151
134
|
const state = getState(cacheItem);
|
152
135
|
|
153
|
-
assert(state.kind ===
|
136
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
154
137
|
expect(removeFromParentCache.mock.calls.length).toEqual(1);
|
155
138
|
expect(disposeItem.mock.calls.length).toEqual(1);
|
156
139
|
|
@@ -160,8 +143,8 @@ describe("CacheItem", () => {
|
|
160
143
|
});
|
161
144
|
});
|
162
145
|
|
163
|
-
describe(
|
164
|
-
test(
|
146
|
+
describe('Item that is temporarily retained once and then permanently retained', () => {
|
147
|
+
test('Permanent retain removes the item from the parent', () => {
|
165
148
|
const removeFromParentCache = vi.fn();
|
166
149
|
const disposeItem = vi.fn();
|
167
150
|
|
@@ -169,25 +152,25 @@ describe("CacheItem", () => {
|
|
169
152
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
170
153
|
return ret;
|
171
154
|
});
|
172
|
-
const [cacheItem, disposeTemporaryRetain] =
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
);
|
155
|
+
const [cacheItem, disposeTemporaryRetain] = createTemporarilyRetainedCacheItem<number>(
|
156
|
+
factory,
|
157
|
+
removeFromParentCache,
|
158
|
+
);
|
177
159
|
|
178
160
|
const mockedDisposeTemporaryRetain = vi.fn(disposeTemporaryRetain);
|
179
161
|
|
180
162
|
expect(factory.mock.calls.length).toEqual(1);
|
181
163
|
|
182
|
-
const [item, _disposePermanentRetain] =
|
183
|
-
|
164
|
+
const [item, _disposePermanentRetain] = cacheItem.permanentRetainIfNotDisposed(
|
165
|
+
mockedDisposeTemporaryRetain,
|
166
|
+
)!;
|
184
167
|
|
185
168
|
expect(item).toEqual(1);
|
186
169
|
expect(mockedDisposeTemporaryRetain.mock.calls.length).toEqual(1);
|
187
170
|
|
188
171
|
const state = getState(cacheItem);
|
189
172
|
|
190
|
-
assert(state.kind ===
|
173
|
+
assert(state.kind === 'NotInParentCacheAndNotDisposed');
|
191
174
|
expect(state.disposeValue).toEqual(disposeItem);
|
192
175
|
expect(state.permanentRetainCount).toEqual(1);
|
193
176
|
expect(state.value).toEqual(1);
|
@@ -195,7 +178,7 @@ describe("CacheItem", () => {
|
|
195
178
|
expect(removeFromParentCache.mock.calls.length).toEqual(1);
|
196
179
|
});
|
197
180
|
|
198
|
-
test(
|
181
|
+
test('Disposing the temporary retain after permanent retain throws', () => {
|
199
182
|
const removeFromParentCache = vi.fn();
|
200
183
|
const disposeItem = vi.fn();
|
201
184
|
|
@@ -203,11 +186,10 @@ describe("CacheItem", () => {
|
|
203
186
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
204
187
|
return ret;
|
205
188
|
});
|
206
|
-
const [cacheItem, disposeTemporaryRetain] =
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
);
|
189
|
+
const [cacheItem, disposeTemporaryRetain] = createTemporarilyRetainedCacheItem<number>(
|
190
|
+
factory,
|
191
|
+
removeFromParentCache,
|
192
|
+
);
|
211
193
|
|
212
194
|
expect(factory.mock.calls.length).toEqual(1);
|
213
195
|
|
@@ -219,7 +201,7 @@ describe("CacheItem", () => {
|
|
219
201
|
}).toThrow();
|
220
202
|
});
|
221
203
|
|
222
|
-
test(
|
204
|
+
test('Item is disposed when the permanently retain is disposed', () => {
|
223
205
|
const removeFromParentCache = vi.fn();
|
224
206
|
const disposeItem = vi.fn();
|
225
207
|
|
@@ -227,28 +209,28 @@ describe("CacheItem", () => {
|
|
227
209
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
228
210
|
return ret;
|
229
211
|
});
|
230
|
-
const [cacheItem, disposeTemporaryRetain] =
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
);
|
212
|
+
const [cacheItem, disposeTemporaryRetain] = createTemporarilyRetainedCacheItem<number>(
|
213
|
+
factory,
|
214
|
+
removeFromParentCache,
|
215
|
+
);
|
235
216
|
|
236
217
|
const mockedDisposeTemporaryRetain = vi.fn(disposeTemporaryRetain);
|
237
218
|
|
238
219
|
expect(factory.mock.calls.length).toEqual(1);
|
239
220
|
|
240
|
-
const [item, disposePermanentRetain] =
|
241
|
-
|
221
|
+
const [item, disposePermanentRetain] = cacheItem.permanentRetainIfNotDisposed(
|
222
|
+
mockedDisposeTemporaryRetain,
|
223
|
+
)!;
|
242
224
|
|
243
225
|
disposePermanentRetain();
|
244
226
|
|
245
227
|
const state = getState(cacheItem);
|
246
|
-
assert(state.kind ===
|
228
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
247
229
|
expect(removeFromParentCache.mock.calls.length).toEqual(1);
|
248
230
|
expect(disposeItem.mock.calls.length).toEqual(1);
|
249
231
|
});
|
250
232
|
|
251
|
-
test(
|
233
|
+
test('Repeated disposal of permanently retained cache item throws', () => {
|
252
234
|
const removeFromParentCache = vi.fn();
|
253
235
|
const disposeItem = vi.fn();
|
254
236
|
|
@@ -256,18 +238,18 @@ describe("CacheItem", () => {
|
|
256
238
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
257
239
|
return ret;
|
258
240
|
});
|
259
|
-
const [cacheItem, disposeTemporaryRetain] =
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
);
|
241
|
+
const [cacheItem, disposeTemporaryRetain] = createTemporarilyRetainedCacheItem<number>(
|
242
|
+
factory,
|
243
|
+
removeFromParentCache,
|
244
|
+
);
|
264
245
|
|
265
246
|
const mockedDisposeTemporaryRetain = vi.fn(disposeTemporaryRetain);
|
266
247
|
|
267
248
|
expect(factory.mock.calls.length).toEqual(1);
|
268
249
|
|
269
|
-
const [item, disposePermanentRetain] =
|
270
|
-
|
250
|
+
const [item, disposePermanentRetain] = cacheItem.permanentRetainIfNotDisposed(
|
251
|
+
mockedDisposeTemporaryRetain,
|
252
|
+
)!;
|
271
253
|
|
272
254
|
disposePermanentRetain();
|
273
255
|
expect(() => {
|
@@ -275,7 +257,7 @@ describe("CacheItem", () => {
|
|
275
257
|
}).toThrow();
|
276
258
|
});
|
277
259
|
|
278
|
-
test(
|
260
|
+
test('Permanent retain of cache item whose temporary retain has expired', () => {
|
279
261
|
const removeFromParentCache = vi.fn();
|
280
262
|
const disposeItem = vi.fn();
|
281
263
|
|
@@ -283,11 +265,10 @@ describe("CacheItem", () => {
|
|
283
265
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
284
266
|
return ret;
|
285
267
|
});
|
286
|
-
const [cacheItem, disposeTemporaryRetain] =
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
);
|
268
|
+
const [cacheItem, disposeTemporaryRetain] = createTemporarilyRetainedCacheItem<number>(
|
269
|
+
factory,
|
270
|
+
removeFromParentCache,
|
271
|
+
);
|
291
272
|
|
292
273
|
expect(factory.mock.calls.length).toEqual(1);
|
293
274
|
|
@@ -295,18 +276,16 @@ describe("CacheItem", () => {
|
|
295
276
|
|
296
277
|
const state = getState(cacheItem);
|
297
278
|
|
298
|
-
assert(state.kind ===
|
279
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
299
280
|
|
300
|
-
assert(
|
301
|
-
cacheItem.permanentRetainIfNotDisposed(disposeTemporaryRetain) === null
|
302
|
-
);
|
281
|
+
assert(cacheItem.permanentRetainIfNotDisposed(disposeTemporaryRetain) === null);
|
303
282
|
|
304
283
|
expect(() => {
|
305
284
|
cacheItem.permanentRetain();
|
306
285
|
}).toThrow();
|
307
286
|
});
|
308
287
|
|
309
|
-
test(
|
288
|
+
test('It is invalid to temporarily retain after the permanent retain', () => {
|
310
289
|
const removeFromParentCache = vi.fn();
|
311
290
|
const disposeItem = vi.fn();
|
312
291
|
|
@@ -314,11 +293,10 @@ describe("CacheItem", () => {
|
|
314
293
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
315
294
|
return ret;
|
316
295
|
});
|
317
|
-
const [cacheItem, disposeTemporaryRetain1] =
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
);
|
296
|
+
const [cacheItem, disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
297
|
+
factory,
|
298
|
+
removeFromParentCache,
|
299
|
+
);
|
322
300
|
|
323
301
|
const [_value, disposeOfPermanentRetain1] =
|
324
302
|
cacheItem.permanentRetainIfNotDisposed(disposeTemporaryRetain1)!;
|
@@ -329,8 +307,8 @@ describe("CacheItem", () => {
|
|
329
307
|
});
|
330
308
|
});
|
331
309
|
|
332
|
-
describe(
|
333
|
-
test(
|
310
|
+
describe('Item that temporarily retained twice', () => {
|
311
|
+
test('Cache item is not disposed after first temporary retain expires', () => {
|
334
312
|
const removeFromParentCache = vi.fn();
|
335
313
|
const disposeItem = vi.fn();
|
336
314
|
|
@@ -338,11 +316,10 @@ describe("CacheItem", () => {
|
|
338
316
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
339
317
|
return ret;
|
340
318
|
});
|
341
|
-
const [cacheItem, _disposeTemporaryRetain1] =
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
);
|
319
|
+
const [cacheItem, _disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
320
|
+
factory,
|
321
|
+
removeFromParentCache,
|
322
|
+
);
|
346
323
|
|
347
324
|
vi.advanceTimersByTime(1000);
|
348
325
|
|
@@ -351,7 +328,7 @@ describe("CacheItem", () => {
|
|
351
328
|
vi.advanceTimersToNextTimer();
|
352
329
|
|
353
330
|
const state = getState(cacheItem);
|
354
|
-
assert(state.kind ===
|
331
|
+
assert(state.kind === 'InParentCacheAndNotDisposed');
|
355
332
|
expect(state.value).toEqual(1);
|
356
333
|
expect(state.permanentRetainCount).toEqual(0);
|
357
334
|
expect(state.temporaryRetainCount).toEqual(1);
|
@@ -361,7 +338,7 @@ describe("CacheItem", () => {
|
|
361
338
|
expect(removeFromParentCache.mock.calls.length).toEqual(0);
|
362
339
|
});
|
363
340
|
|
364
|
-
test(
|
341
|
+
test('Cache item is not disposed after first temporary retain is disposed', () => {
|
365
342
|
const removeFromParentCache = vi.fn();
|
366
343
|
const disposeItem = vi.fn();
|
367
344
|
|
@@ -369,11 +346,10 @@ describe("CacheItem", () => {
|
|
369
346
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
370
347
|
return ret;
|
371
348
|
});
|
372
|
-
const [cacheItem, disposeTemporaryRetain1] =
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
);
|
349
|
+
const [cacheItem, disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
350
|
+
factory,
|
351
|
+
removeFromParentCache,
|
352
|
+
);
|
377
353
|
|
378
354
|
vi.advanceTimersByTime(1000);
|
379
355
|
const _disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
@@ -381,7 +357,7 @@ describe("CacheItem", () => {
|
|
381
357
|
disposeTemporaryRetain1();
|
382
358
|
|
383
359
|
const state = getState(cacheItem);
|
384
|
-
assert(state.kind ===
|
360
|
+
assert(state.kind === 'InParentCacheAndNotDisposed');
|
385
361
|
expect(state.value).toEqual(1);
|
386
362
|
expect(state.permanentRetainCount).toEqual(0);
|
387
363
|
expect(state.temporaryRetainCount).toEqual(1);
|
@@ -391,7 +367,7 @@ describe("CacheItem", () => {
|
|
391
367
|
expect(removeFromParentCache.mock.calls.length).toEqual(0);
|
392
368
|
});
|
393
369
|
|
394
|
-
test(
|
370
|
+
test('Disposing the first temporary retain after it expires is a no-op', () => {
|
395
371
|
const removeFromParentCache = vi.fn();
|
396
372
|
const disposeItem = vi.fn();
|
397
373
|
|
@@ -399,11 +375,10 @@ describe("CacheItem", () => {
|
|
399
375
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
400
376
|
return ret;
|
401
377
|
});
|
402
|
-
const [cacheItem, disposeTemporaryRetain1] =
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
);
|
378
|
+
const [cacheItem, disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
379
|
+
factory,
|
380
|
+
removeFromParentCache,
|
381
|
+
);
|
407
382
|
|
408
383
|
vi.advanceTimersByTime(1000);
|
409
384
|
const _disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
@@ -411,11 +386,11 @@ describe("CacheItem", () => {
|
|
411
386
|
disposeTemporaryRetain1();
|
412
387
|
|
413
388
|
const state = getState(cacheItem);
|
414
|
-
assert(state.kind ===
|
389
|
+
assert(state.kind === 'InParentCacheAndNotDisposed');
|
415
390
|
expect(state.temporaryRetainCount).toEqual(1);
|
416
391
|
});
|
417
392
|
|
418
|
-
test(
|
393
|
+
test('Item is not disposed if only the second temporary retain is disposed', () => {
|
419
394
|
const removeFromParentCache = vi.fn();
|
420
395
|
const disposeItem = vi.fn();
|
421
396
|
|
@@ -423,22 +398,21 @@ describe("CacheItem", () => {
|
|
423
398
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
424
399
|
return ret;
|
425
400
|
});
|
426
|
-
const [cacheItem, disposeTemporaryRetain1] =
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
);
|
401
|
+
const [cacheItem, disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
402
|
+
factory,
|
403
|
+
removeFromParentCache,
|
404
|
+
);
|
431
405
|
|
432
406
|
vi.advanceTimersByTime(1000);
|
433
407
|
const disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
434
408
|
disposeTemporaryRetain2();
|
435
409
|
|
436
410
|
const state = getState(cacheItem);
|
437
|
-
assert(state.kind ===
|
411
|
+
assert(state.kind === 'InParentCacheAndNotDisposed');
|
438
412
|
expect(state.temporaryRetainCount).toEqual(1);
|
439
413
|
});
|
440
414
|
|
441
|
-
test(
|
415
|
+
test('Item is disposed if both temporary retains are disposed', () => {
|
442
416
|
const removeFromParentCache = vi.fn();
|
443
417
|
const disposeItem = vi.fn();
|
444
418
|
|
@@ -446,25 +420,24 @@ describe("CacheItem", () => {
|
|
446
420
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
447
421
|
return ret;
|
448
422
|
});
|
449
|
-
const [cacheItem, disposeTemporaryRetain1] =
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
);
|
423
|
+
const [cacheItem, disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
424
|
+
factory,
|
425
|
+
removeFromParentCache,
|
426
|
+
);
|
454
427
|
|
455
428
|
const disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
456
429
|
const state1 = getState(cacheItem);
|
457
|
-
assert(state1.kind ===
|
430
|
+
assert(state1.kind === 'InParentCacheAndNotDisposed');
|
458
431
|
expect(state1.temporaryRetainCount).toEqual(2);
|
459
432
|
disposeTemporaryRetain1();
|
460
433
|
expect(state1.temporaryRetainCount).toEqual(1);
|
461
434
|
disposeTemporaryRetain2();
|
462
435
|
|
463
436
|
const state2 = getState(cacheItem);
|
464
|
-
assert(state2.kind ===
|
437
|
+
assert(state2.kind === 'NotInParentCacheAndDisposed');
|
465
438
|
});
|
466
439
|
|
467
|
-
test(
|
440
|
+
test('Item is disposed if both temporary retains are disposed in reverse order', () => {
|
468
441
|
const removeFromParentCache = vi.fn();
|
469
442
|
const disposeItem = vi.fn();
|
470
443
|
|
@@ -472,25 +445,24 @@ describe("CacheItem", () => {
|
|
472
445
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
473
446
|
return ret;
|
474
447
|
});
|
475
|
-
const [cacheItem, disposeTemporaryRetain1] =
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
);
|
448
|
+
const [cacheItem, disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
449
|
+
factory,
|
450
|
+
removeFromParentCache,
|
451
|
+
);
|
480
452
|
|
481
453
|
const disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
482
454
|
const state1 = getState(cacheItem);
|
483
|
-
assert(state1.kind ===
|
455
|
+
assert(state1.kind === 'InParentCacheAndNotDisposed');
|
484
456
|
expect(state1.temporaryRetainCount).toEqual(2);
|
485
457
|
disposeTemporaryRetain2();
|
486
458
|
expect(state1.temporaryRetainCount).toEqual(1);
|
487
459
|
disposeTemporaryRetain1();
|
488
460
|
|
489
461
|
const state2 = getState(cacheItem);
|
490
|
-
assert(state2.kind ===
|
462
|
+
assert(state2.kind === 'NotInParentCacheAndDisposed');
|
491
463
|
});
|
492
464
|
|
493
|
-
test(
|
465
|
+
test('Item is disposed if both temporary retains expire', () => {
|
494
466
|
const removeFromParentCache = vi.fn();
|
495
467
|
const disposeItem = vi.fn();
|
496
468
|
|
@@ -498,17 +470,16 @@ describe("CacheItem", () => {
|
|
498
470
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
499
471
|
return ret;
|
500
472
|
});
|
501
|
-
const [cacheItem, _disposeTemporaryRetain1] =
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
);
|
473
|
+
const [cacheItem, _disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
474
|
+
factory,
|
475
|
+
removeFromParentCache,
|
476
|
+
);
|
506
477
|
|
507
478
|
vi.advanceTimersByTime(1000);
|
508
479
|
const _disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
509
480
|
|
510
481
|
const state1 = getState(cacheItem);
|
511
|
-
assert(state1.kind ===
|
482
|
+
assert(state1.kind === 'InParentCacheAndNotDisposed');
|
512
483
|
expect(state1.temporaryRetainCount).toEqual(2);
|
513
484
|
|
514
485
|
vi.advanceTimersToNextTimer();
|
@@ -516,14 +487,14 @@ describe("CacheItem", () => {
|
|
516
487
|
|
517
488
|
vi.advanceTimersToNextTimer();
|
518
489
|
const state = getState(cacheItem);
|
519
|
-
assert(state.kind ===
|
490
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
520
491
|
expect(disposeItem.mock.calls.length).toEqual(1);
|
521
492
|
expect(removeFromParentCache.mock.calls.length).toEqual(1);
|
522
493
|
});
|
523
494
|
});
|
524
495
|
|
525
|
-
describe(
|
526
|
-
test(
|
496
|
+
describe('Item that is temporarily retained twice and then permanently retained', () => {
|
497
|
+
test('Item is not removed from the parent cache when the permanent retain is created', () => {
|
527
498
|
const removeFromParentCache = vi.fn();
|
528
499
|
const disposeItem = vi.fn();
|
529
500
|
|
@@ -531,11 +502,10 @@ describe("CacheItem", () => {
|
|
531
502
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
532
503
|
return ret;
|
533
504
|
});
|
534
|
-
const [cacheItem, disposeTemporaryRetain1] =
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
);
|
505
|
+
const [cacheItem, disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
506
|
+
factory,
|
507
|
+
removeFromParentCache,
|
508
|
+
);
|
539
509
|
|
540
510
|
vi.advanceTimersByTime(1000);
|
541
511
|
const _disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
@@ -545,13 +515,13 @@ describe("CacheItem", () => {
|
|
545
515
|
|
546
516
|
const state = getState(cacheItem);
|
547
517
|
|
548
|
-
assert(state.kind ===
|
518
|
+
assert(state.kind === 'InParentCacheAndNotDisposed');
|
549
519
|
expect(state.value).toEqual(1);
|
550
520
|
expect(state.permanentRetainCount).toEqual(1);
|
551
521
|
expect(state.temporaryRetainCount).toEqual(1);
|
552
522
|
});
|
553
523
|
|
554
|
-
test(
|
524
|
+
test('Item is removed from the parent cache when the second temporary retain is disposed', () => {
|
555
525
|
const removeFromParentCache = vi.fn();
|
556
526
|
const disposeItem = vi.fn();
|
557
527
|
|
@@ -559,11 +529,10 @@ describe("CacheItem", () => {
|
|
559
529
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
560
530
|
return ret;
|
561
531
|
});
|
562
|
-
const [cacheItem, disposeTemporaryRetain1] =
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
);
|
532
|
+
const [cacheItem, disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
533
|
+
factory,
|
534
|
+
removeFromParentCache,
|
535
|
+
);
|
567
536
|
|
568
537
|
vi.advanceTimersByTime(1000);
|
569
538
|
const disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
@@ -572,18 +541,18 @@ describe("CacheItem", () => {
|
|
572
541
|
cacheItem.permanentRetainIfNotDisposed(disposeTemporaryRetain1)!;
|
573
542
|
|
574
543
|
const state1 = getState(cacheItem);
|
575
|
-
assert(state1.kind ===
|
544
|
+
assert(state1.kind === 'InParentCacheAndNotDisposed');
|
576
545
|
expect(state1.permanentRetainCount).toEqual(1);
|
577
546
|
expect(state1.temporaryRetainCount).toEqual(1);
|
578
547
|
|
579
548
|
disposeTemporaryRetain2();
|
580
549
|
|
581
550
|
const state2 = getState(cacheItem);
|
582
|
-
assert(state2.kind ===
|
551
|
+
assert(state2.kind === 'NotInParentCacheAndNotDisposed');
|
583
552
|
expect(state2.permanentRetainCount).toEqual(1);
|
584
553
|
});
|
585
554
|
|
586
|
-
test(
|
555
|
+
test('Item is removed from the parent cache when the second temporary retain expires', () => {
|
587
556
|
const removeFromParentCache = vi.fn();
|
588
557
|
const disposeItem = vi.fn();
|
589
558
|
|
@@ -591,11 +560,10 @@ describe("CacheItem", () => {
|
|
591
560
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
592
561
|
return ret;
|
593
562
|
});
|
594
|
-
const [cacheItem, disposeTemporaryRetain1] =
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
);
|
563
|
+
const [cacheItem, disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
564
|
+
factory,
|
565
|
+
removeFromParentCache,
|
566
|
+
);
|
599
567
|
|
600
568
|
vi.advanceTimersByTime(1000);
|
601
569
|
const _disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
@@ -604,18 +572,18 @@ describe("CacheItem", () => {
|
|
604
572
|
cacheItem.permanentRetainIfNotDisposed(disposeTemporaryRetain1)!;
|
605
573
|
|
606
574
|
const state1 = getState(cacheItem);
|
607
|
-
assert(state1.kind ===
|
575
|
+
assert(state1.kind === 'InParentCacheAndNotDisposed');
|
608
576
|
expect(state1.permanentRetainCount).toEqual(1);
|
609
577
|
expect(state1.temporaryRetainCount).toEqual(1);
|
610
578
|
|
611
579
|
vi.advanceTimersToNextTimer();
|
612
580
|
|
613
581
|
const state2 = getState(cacheItem);
|
614
|
-
assert(state2.kind ===
|
582
|
+
assert(state2.kind === 'NotInParentCacheAndNotDisposed');
|
615
583
|
expect(state2.permanentRetainCount).toEqual(1);
|
616
584
|
});
|
617
585
|
|
618
|
-
test(
|
586
|
+
test('Item is not removed from the parent cache when the permanent retain is disposed', () => {
|
619
587
|
const removeFromParentCache = vi.fn();
|
620
588
|
const disposeItem = vi.fn();
|
621
589
|
|
@@ -623,11 +591,10 @@ describe("CacheItem", () => {
|
|
623
591
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
624
592
|
return ret;
|
625
593
|
});
|
626
|
-
const [cacheItem, disposeTemporaryRetain1] =
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
);
|
594
|
+
const [cacheItem, disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
595
|
+
factory,
|
596
|
+
removeFromParentCache,
|
597
|
+
);
|
631
598
|
|
632
599
|
vi.advanceTimersByTime(1000);
|
633
600
|
const _disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
@@ -638,12 +605,12 @@ describe("CacheItem", () => {
|
|
638
605
|
disposeOfPermanentRetain();
|
639
606
|
|
640
607
|
const state = getState(cacheItem);
|
641
|
-
assert(state.kind ===
|
608
|
+
assert(state.kind === 'InParentCacheAndNotDisposed');
|
642
609
|
expect(state.permanentRetainCount).toEqual(0);
|
643
610
|
expect(state.temporaryRetainCount).toEqual(1);
|
644
611
|
});
|
645
612
|
|
646
|
-
test(
|
613
|
+
test('Item is disposed the permanent retain is disposed and the second temporary retain expires', () => {
|
647
614
|
const removeFromParentCache = vi.fn();
|
648
615
|
const disposeItem = vi.fn();
|
649
616
|
|
@@ -651,11 +618,10 @@ describe("CacheItem", () => {
|
|
651
618
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
652
619
|
return ret;
|
653
620
|
});
|
654
|
-
const [cacheItem, disposeTemporaryRetain1] =
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
);
|
621
|
+
const [cacheItem, disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
622
|
+
factory,
|
623
|
+
removeFromParentCache,
|
624
|
+
);
|
659
625
|
|
660
626
|
vi.advanceTimersByTime(1000);
|
661
627
|
const _disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
@@ -667,10 +633,10 @@ describe("CacheItem", () => {
|
|
667
633
|
vi.advanceTimersToNextTimer();
|
668
634
|
|
669
635
|
const state = getState(cacheItem);
|
670
|
-
assert(state.kind ===
|
636
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
671
637
|
});
|
672
638
|
|
673
|
-
test(
|
639
|
+
test('Item is disposed when the permanent retain is disposed and the second temporary retain is disposed', () => {
|
674
640
|
const removeFromParentCache = vi.fn();
|
675
641
|
const disposeItem = vi.fn();
|
676
642
|
|
@@ -678,11 +644,10 @@ describe("CacheItem", () => {
|
|
678
644
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
679
645
|
return ret;
|
680
646
|
});
|
681
|
-
const [cacheItem, disposeTemporaryRetain1] =
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
);
|
647
|
+
const [cacheItem, disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
648
|
+
factory,
|
649
|
+
removeFromParentCache,
|
650
|
+
);
|
686
651
|
|
687
652
|
vi.advanceTimersByTime(1000);
|
688
653
|
const disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
@@ -694,10 +659,10 @@ describe("CacheItem", () => {
|
|
694
659
|
disposeTemporaryRetain2();
|
695
660
|
|
696
661
|
const state = getState(cacheItem);
|
697
|
-
assert(state.kind ===
|
662
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
698
663
|
});
|
699
664
|
|
700
|
-
test(
|
665
|
+
test('Item is disposed when the second temporary is disposed and the permanent retain is disposed', () => {
|
701
666
|
const removeFromParentCache = vi.fn();
|
702
667
|
const disposeItem = vi.fn();
|
703
668
|
|
@@ -705,11 +670,10 @@ describe("CacheItem", () => {
|
|
705
670
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
706
671
|
return ret;
|
707
672
|
});
|
708
|
-
const [cacheItem, disposeTemporaryRetain1] =
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
);
|
673
|
+
const [cacheItem, disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
674
|
+
factory,
|
675
|
+
removeFromParentCache,
|
676
|
+
);
|
713
677
|
|
714
678
|
vi.advanceTimersByTime(1000);
|
715
679
|
const disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
@@ -721,10 +685,10 @@ describe("CacheItem", () => {
|
|
721
685
|
disposeOfPermanentRetain();
|
722
686
|
|
723
687
|
const state = getState(cacheItem);
|
724
|
-
assert(state.kind ===
|
688
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
725
689
|
});
|
726
690
|
|
727
|
-
test(
|
691
|
+
test('Item is disposed when the second temporary expires and the permanent retain is disposed', () => {
|
728
692
|
const removeFromParentCache = vi.fn();
|
729
693
|
const disposeItem = vi.fn();
|
730
694
|
|
@@ -732,11 +696,10 @@ describe("CacheItem", () => {
|
|
732
696
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
733
697
|
return ret;
|
734
698
|
});
|
735
|
-
const [cacheItem, disposeTemporaryRetain1] =
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
);
|
699
|
+
const [cacheItem, disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
700
|
+
factory,
|
701
|
+
removeFromParentCache,
|
702
|
+
);
|
740
703
|
|
741
704
|
vi.advanceTimersByTime(1000);
|
742
705
|
const _disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
@@ -748,12 +711,12 @@ describe("CacheItem", () => {
|
|
748
711
|
disposeOfPermanentRetain();
|
749
712
|
|
750
713
|
const state = getState(cacheItem);
|
751
|
-
assert(state.kind ===
|
714
|
+
assert(state.kind === 'NotInParentCacheAndDisposed');
|
752
715
|
});
|
753
716
|
});
|
754
717
|
|
755
|
-
describe(
|
756
|
-
test(
|
718
|
+
describe('Multiple permanent retains', () => {
|
719
|
+
test('Item is only disposed when second permanent retain is disposed', () => {
|
757
720
|
const removeFromParentCache = vi.fn();
|
758
721
|
const disposeItem = vi.fn();
|
759
722
|
|
@@ -761,11 +724,10 @@ describe("CacheItem", () => {
|
|
761
724
|
const ret: ItemCleanupPair<number> = [1, disposeItem];
|
762
725
|
return ret;
|
763
726
|
});
|
764
|
-
const [cacheItem, disposeTemporaryRetain1] =
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
);
|
727
|
+
const [cacheItem, disposeTemporaryRetain1] = createTemporarilyRetainedCacheItem<number>(
|
728
|
+
factory,
|
729
|
+
removeFromParentCache,
|
730
|
+
);
|
769
731
|
|
770
732
|
vi.advanceTimersByTime(1000);
|
771
733
|
const disposeTemporaryRetain2 = cacheItem.temporaryRetain();
|
@@ -777,12 +739,12 @@ describe("CacheItem", () => {
|
|
777
739
|
|
778
740
|
disposeOfPermanentRetain1();
|
779
741
|
const state = getState(cacheItem);
|
780
|
-
assert(state.kind ===
|
742
|
+
assert(state.kind === 'NotInParentCacheAndNotDisposed');
|
781
743
|
expect(state.permanentRetainCount).toEqual(1);
|
782
744
|
|
783
745
|
disposeOfPermanentRetain2();
|
784
746
|
const state2 = getState(cacheItem);
|
785
|
-
assert(state2.kind ===
|
747
|
+
assert(state2.kind === 'NotInParentCacheAndDisposed');
|
786
748
|
});
|
787
749
|
});
|
788
750
|
});
|