@isograph/react-disposable-state 0.5.0 → 0.5.1
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/.turbo/turbo-compile-libs.log +9 -2
- package/dist/CacheItem.d.mts +63 -0
- package/dist/CacheItem.d.mts.map +1 -0
- package/dist/CacheItem.d.ts +33 -29
- package/dist/CacheItem.d.ts.map +1 -1
- package/dist/CacheItem.js +191 -266
- package/dist/CacheItem.mjs +193 -0
- package/dist/CacheItem.mjs.map +1 -0
- package/dist/ParentCache.d.mts +45 -0
- package/dist/ParentCache.d.mts.map +1 -0
- package/dist/ParentCache.d.ts +27 -22
- package/dist/ParentCache.d.ts.map +1 -1
- package/dist/ParentCache.js +69 -85
- package/dist/ParentCache.mjs +73 -0
- package/dist/ParentCache.mjs.map +1 -0
- package/dist/_virtual/rolldown_runtime.js +25 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.d.ts +9 -9
- package/dist/index.js +24 -24
- package/dist/index.mjs +11 -0
- package/dist/useCachedResponsivePrecommitValue.d.mts +43 -0
- package/dist/useCachedResponsivePrecommitValue.d.mts.map +1 -0
- package/dist/useCachedResponsivePrecommitValue.d.ts +9 -4
- package/dist/useCachedResponsivePrecommitValue.d.ts.map +1 -1
- package/dist/useCachedResponsivePrecommitValue.js +55 -90
- package/dist/useCachedResponsivePrecommitValue.mjs +57 -0
- package/dist/useCachedResponsivePrecommitValue.mjs.map +1 -0
- package/dist/useDisposableState.d.mts +13 -0
- package/dist/useDisposableState.d.mts.map +1 -0
- package/dist/useDisposableState.d.ts +10 -7
- package/dist/useDisposableState.d.ts.map +1 -1
- package/dist/useDisposableState.js +36 -69
- package/dist/useDisposableState.mjs +37 -0
- package/dist/useDisposableState.mjs.map +1 -0
- package/dist/useHasCommittedRef.d.mts +11 -0
- package/dist/useHasCommittedRef.d.mts.map +1 -0
- package/dist/useHasCommittedRef.d.ts +7 -2
- package/dist/useHasCommittedRef.d.ts.map +1 -1
- package/dist/useHasCommittedRef.js +15 -11
- package/dist/useHasCommittedRef.mjs +17 -0
- package/dist/useHasCommittedRef.mjs.map +1 -0
- package/dist/useLazyDisposableState.d.mts +20 -0
- package/dist/useLazyDisposableState.d.mts.map +1 -0
- package/dist/useLazyDisposableState.d.ts +9 -4
- package/dist/useLazyDisposableState.d.ts.map +1 -1
- package/dist/useLazyDisposableState.js +32 -39
- package/dist/useLazyDisposableState.mjs +34 -0
- package/dist/useLazyDisposableState.mjs.map +1 -0
- package/dist/useUpdatableDisposableState.d.mts +43 -0
- package/dist/useUpdatableDisposableState.d.mts.map +1 -0
- package/dist/useUpdatableDisposableState.d.ts +10 -7
- package/dist/useUpdatableDisposableState.d.ts.map +1 -1
- package/dist/useUpdatableDisposableState.js +73 -89
- package/dist/useUpdatableDisposableState.mjs +74 -0
- package/dist/useUpdatableDisposableState.mjs.map +1 -0
- package/package.json +14 -6
- package/src/CacheItem.test.ts +4 -7
- package/src/CacheItem.ts +16 -11
- package/src/ParentCache.test.ts +5 -5
- package/src/ParentCache.ts +5 -4
- package/src/useCachedResponsivePrecommitValue.test.tsx +15 -14
- package/src/useCachedResponsivePrecommitValue.ts +4 -4
- package/src/useDisposableState.ts +21 -16
- package/src/useHasCommittedRef.ts +1 -1
- package/src/useLazyDisposableState.test.tsx +2 -2
- package/src/useLazyDisposableState.ts +1 -1
- package/src/useUpdatableDisposableState.test.tsx +19 -5
- package/src/useUpdatableDisposableState.ts +1 -1
- package/dist/index.d.ts.map +0 -1
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
../.. | WARN Unsupported engine: wanted: {"node":"22.9.0"} (current: {"node":"v22.21.1","pnpm":"10.15.0"})
|
|
2
2
|
|
|
3
|
-
> @isograph/react-disposable-state@0.5.
|
|
4
|
-
>
|
|
3
|
+
> @isograph/react-disposable-state@0.5.1 compile-libs /home/runner/work/isograph/isograph/libs/isograph-react-disposable-state
|
|
4
|
+
> tsdown
|
|
5
5
|
|
|
6
|
+
[34mℹ[39m tsdown [2mv0.15.10[22m powered by rolldown [2mv1.0.0-beta.44[22m
|
|
7
|
+
[34mℹ[39m Using tsdown config: [4m/home/runner/work/isograph/isograph/tsdown.config.ts[24m
|
|
8
|
+
(node:2983) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/runner/work/isograph/isograph/tsdown.config.ts is not specified and it doesn't parse as CommonJS.
|
|
9
|
+
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
|
|
10
|
+
To eliminate this warning, add "type": "module" to /home/runner/work/isograph/isograph/package.json.
|
|
11
|
+
(Use `node --trace-warnings ...` to show where the warning was created)
|
|
12
|
+
[34mℹ[39m Build start
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { CleanupFn, Factory, ItemCleanupPair } from "@isograph/disposable-types";
|
|
2
|
+
|
|
3
|
+
//#region src/CacheItem.d.ts
|
|
4
|
+
type NotInParentCacheAndDisposed = {
|
|
5
|
+
kind: 'NotInParentCacheAndDisposed';
|
|
6
|
+
};
|
|
7
|
+
type NotInParentCacheAndNotDisposed<T> = {
|
|
8
|
+
kind: 'NotInParentCacheAndNotDisposed';
|
|
9
|
+
value: T;
|
|
10
|
+
disposeValue: () => void;
|
|
11
|
+
permanentRetainCount: number;
|
|
12
|
+
};
|
|
13
|
+
type InParentCacheAndNotDisposed<T> = {
|
|
14
|
+
kind: 'InParentCacheAndNotDisposed';
|
|
15
|
+
value: T;
|
|
16
|
+
disposeValue: () => void;
|
|
17
|
+
removeFromParentCache: () => void;
|
|
18
|
+
temporaryRetainCount: number;
|
|
19
|
+
permanentRetainCount: number;
|
|
20
|
+
};
|
|
21
|
+
type CacheItemState<T> = InParentCacheAndNotDisposed<T> | NotInParentCacheAndNotDisposed<T> | NotInParentCacheAndDisposed;
|
|
22
|
+
type CacheItemOptions = {
|
|
23
|
+
temporaryRetainTime: number;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* CacheItem:
|
|
27
|
+
*
|
|
28
|
+
* Terminology:
|
|
29
|
+
* - TRC = Temporary Retain Count
|
|
30
|
+
* - PRC = Permanent Retain Count
|
|
31
|
+
*
|
|
32
|
+
* A CacheItem<T> can be in three states:
|
|
33
|
+
* In parent cache? | Item disposed? | TRC | PRC | Name
|
|
34
|
+
* -----------------+----------------+-----+-----+-------------------------------
|
|
35
|
+
* In parent cache | Not disposed | >0 | >=0 | InParentCacheAndNotDisposed
|
|
36
|
+
* Removed | Not disposed | 0 | >0 | NotInParentCacheAndNotDisposed
|
|
37
|
+
* Removed | Disposed | 0 | 0 | NotInParentCacheAndNotDisposed
|
|
38
|
+
*
|
|
39
|
+
* A cache item can only move down rows. As in, if its in the parent cache,
|
|
40
|
+
* it can be removed. It can never be replaced in the parent cache. (If a
|
|
41
|
+
* parent cache becomes full again, it will contain a new CacheItem.) The
|
|
42
|
+
* contained item can be disposed, but never un-disposed.
|
|
43
|
+
*
|
|
44
|
+
* So, the valid transitions are:
|
|
45
|
+
* - InParentCacheAndNotDisposed => NotInParentCacheAndNotDisposed
|
|
46
|
+
* - InParentCacheAndNotDisposed => NotInParentCacheAndDisposed
|
|
47
|
+
* - NotInParentCacheAndNotDisposed => NotInParentCacheAndDisposed
|
|
48
|
+
*/
|
|
49
|
+
declare class CacheItem<T> {
|
|
50
|
+
private __state;
|
|
51
|
+
private __options;
|
|
52
|
+
constructor(factory: Factory<T>, removeFromParentCache: CleanupFn, options: CacheItemOptions | void);
|
|
53
|
+
getValue(): T;
|
|
54
|
+
permanentRetainIfNotDisposed(disposeOfTemporaryRetain: CleanupFn): ItemCleanupPair<T> | null;
|
|
55
|
+
temporaryRetain(): CleanupFn;
|
|
56
|
+
permanentRetain(): CleanupFn;
|
|
57
|
+
private __maybeExitInParentCacheAndNotDisposedState;
|
|
58
|
+
private __maybeExitNotInParentCacheAndNotDisposedState;
|
|
59
|
+
}
|
|
60
|
+
declare function createTemporarilyRetainedCacheItem<T>(factory: Factory<T>, removeFromParentCache: CleanupFn, options: CacheItemOptions | void): [CacheItem<T>, CleanupFn];
|
|
61
|
+
//#endregion
|
|
62
|
+
export { CacheItem, CacheItemOptions, CacheItemState, InParentCacheAndNotDisposed, NotInParentCacheAndDisposed, NotInParentCacheAndNotDisposed, createTemporarilyRetainedCacheItem };
|
|
63
|
+
//# sourceMappingURL=CacheItem.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CacheItem.d.mts","names":[],"sources":["../src/CacheItem.ts"],"sourcesContent":[],"mappings":";;;KAQY,2BAAA;;AAAZ,CAAA;AAGY,KAAA,8BAA8B,CAAA,CAAA,CAAA,GAAA;EAQ9B,IAAA,EAAA,gCAA2B;EAa3B,KAAA,EAnBH,CAmBG;EACoB,YAAA,EAAA,GAAA,GAAA,IAAA;EAA5B,oBAAA,EAAA,MAAA;CAC+B;AAA/B,KAfQ,2BAeR,CAAA,CAAA,CAAA,GAAA;EACA,IAAA,EAAA,6BAAA;EAA2B,KAAA,EAdtB,CAcsB;EAEnB,YAAA,EAAA,GAAA,GAAgB,IAAA;EA+Bf,qBAAS,EAAA,GAAA,GAAA,IAAA;EAUD,oBAAA,EAAA,MAAA;EAAR,oBAAA,EAAA,MAAA;CACc;AACd,KAhDD,cAgDC,CAAA,CAAA,CAAA,GA/CT,2BA+CS,CA/CmB,CA+CnB,CAAA,GA9CT,8BA8CS,CA9CsB,CA8CtB,CAAA,GA7CT,2BA6CS;AAgBC,KA3DF,gBAAA,GA2DE;EAkBgB,mBAAA,EAAA,MAAA;CACT;;;;;AAyPrB;;;;;;;;;;;;;;;;;;;;cAxSa;;;uBAUA,QAAQ,2BACM,oBACd;cAgBC;yDAkBgB,YACzB,gBAAgB;qBA+EA;qBAqEA;;;;iBAqGL,+CACL,QAAQ,2BACM,oBACd,2BACP,UAAU,IAAI"}
|
package/dist/CacheItem.d.ts
CHANGED
|
@@ -1,24 +1,26 @@
|
|
|
1
|
-
import { CleanupFn, Factory, ItemCleanupPair } from
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { CleanupFn, Factory, ItemCleanupPair } from "@isograph/disposable-types";
|
|
2
|
+
|
|
3
|
+
//#region src/CacheItem.d.ts
|
|
4
|
+
type NotInParentCacheAndDisposed = {
|
|
5
|
+
kind: 'NotInParentCacheAndDisposed';
|
|
4
6
|
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
type NotInParentCacheAndNotDisposed<T> = {
|
|
8
|
+
kind: 'NotInParentCacheAndNotDisposed';
|
|
9
|
+
value: T;
|
|
10
|
+
disposeValue: () => void;
|
|
11
|
+
permanentRetainCount: number;
|
|
10
12
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
type InParentCacheAndNotDisposed<T> = {
|
|
14
|
+
kind: 'InParentCacheAndNotDisposed';
|
|
15
|
+
value: T;
|
|
16
|
+
disposeValue: () => void;
|
|
17
|
+
removeFromParentCache: () => void;
|
|
18
|
+
temporaryRetainCount: number;
|
|
19
|
+
permanentRetainCount: number;
|
|
18
20
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
type CacheItemState<T> = InParentCacheAndNotDisposed<T> | NotInParentCacheAndNotDisposed<T> | NotInParentCacheAndDisposed;
|
|
22
|
+
type CacheItemOptions = {
|
|
23
|
+
temporaryRetainTime: number;
|
|
22
24
|
};
|
|
23
25
|
/**
|
|
24
26
|
* CacheItem:
|
|
@@ -44,16 +46,18 @@ export type CacheItemOptions = {
|
|
|
44
46
|
* - InParentCacheAndNotDisposed => NotInParentCacheAndDisposed
|
|
45
47
|
* - NotInParentCacheAndNotDisposed => NotInParentCacheAndDisposed
|
|
46
48
|
*/
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
49
|
+
declare class CacheItem<T> {
|
|
50
|
+
private __state;
|
|
51
|
+
private __options;
|
|
52
|
+
constructor(factory: Factory<T>, removeFromParentCache: CleanupFn, options: CacheItemOptions | void);
|
|
53
|
+
getValue(): T;
|
|
54
|
+
permanentRetainIfNotDisposed(disposeOfTemporaryRetain: CleanupFn): ItemCleanupPair<T> | null;
|
|
55
|
+
temporaryRetain(): CleanupFn;
|
|
56
|
+
permanentRetain(): CleanupFn;
|
|
57
|
+
private __maybeExitInParentCacheAndNotDisposedState;
|
|
58
|
+
private __maybeExitNotInParentCacheAndNotDisposedState;
|
|
57
59
|
}
|
|
58
|
-
|
|
60
|
+
declare function createTemporarilyRetainedCacheItem<T>(factory: Factory<T>, removeFromParentCache: CleanupFn, options: CacheItemOptions | void): [CacheItem<T>, CleanupFn];
|
|
61
|
+
//#endregion
|
|
62
|
+
export { CacheItem, CacheItemOptions, CacheItemState, InParentCacheAndNotDisposed, NotInParentCacheAndDisposed, NotInParentCacheAndNotDisposed, createTemporarilyRetainedCacheItem };
|
|
59
63
|
//# sourceMappingURL=CacheItem.d.ts.map
|
package/dist/CacheItem.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CacheItem.d.ts","
|
|
1
|
+
{"version":3,"file":"CacheItem.d.ts","names":[],"sources":["../src/CacheItem.ts"],"sourcesContent":[],"mappings":";;;KAQY,2BAAA;;AAAZ,CAAA;AAGY,KAAA,8BAA8B,CAAA,CAAA,CAAA,GAAA;EAQ9B,IAAA,EAAA,gCAA2B;EAa3B,KAAA,EAnBH,CAmBG;EACoB,YAAA,EAAA,GAAA,GAAA,IAAA;EAA5B,oBAAA,EAAA,MAAA;CAC+B;AAA/B,KAfQ,2BAeR,CAAA,CAAA,CAAA,GAAA;EACA,IAAA,EAAA,6BAAA;EAA2B,KAAA,EAdtB,CAcsB;EAEnB,YAAA,EAAA,GAAA,GAAgB,IAAA;EA+Bf,qBAAS,EAAA,GAAA,GAAA,IAAA;EAUD,oBAAA,EAAA,MAAA;EAAR,oBAAA,EAAA,MAAA;CACc;AACd,KAhDD,cAgDC,CAAA,CAAA,CAAA,GA/CT,2BA+CS,CA/CmB,CA+CnB,CAAA,GA9CT,8BA8CS,CA9CsB,CA8CtB,CAAA,GA7CT,2BA6CS;AAgBC,KA3DF,gBAAA,GA2DE;EAkBgB,mBAAA,EAAA,MAAA;CACT;;;;;AAyPrB;;;;;;;;;;;;;;;;;;;;cAxSa;;;uBAUA,QAAQ,2BACM,oBACd;cAgBC;yDAkBgB,YACzB,gBAAgB;qBA+EA;qBAqEA;;;;iBAqGL,+CACL,QAAQ,2BACM,oBACd,2BACP,UAAU,IAAI"}
|
package/dist/CacheItem.js
CHANGED
|
@@ -1,269 +1,194 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exports.createTemporarilyRetainedCacheItem = createTemporarilyRetainedCacheItem;
|
|
5
|
-
const DEFAULT_TEMPORARY_RETAIN_TIME = 5000;
|
|
6
|
-
// TODO don't export this class, only export type (interface) instead
|
|
7
|
-
// TODO convert cacheitem impl to a getter and setter and free functions
|
|
1
|
+
|
|
2
|
+
//#region src/CacheItem.ts
|
|
3
|
+
const DEFAULT_TEMPORARY_RETAIN_TIME = 5e3;
|
|
8
4
|
/**
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
switch (this.__state.kind) {
|
|
192
|
-
case 'InParentCacheAndNotDisposed': {
|
|
193
|
-
this.__state.permanentRetainCount--;
|
|
194
|
-
this.__maybeExitInParentCacheAndNotDisposedState(this.__state);
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
case 'NotInParentCacheAndNotDisposed': {
|
|
198
|
-
this.__state.permanentRetainCount--;
|
|
199
|
-
this.__maybeExitNotInParentCacheAndNotDisposedState(this.__state);
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
default: {
|
|
203
|
-
throw new Error('CacheItem was in a disposed state, but there existed a permanent retain. ' +
|
|
204
|
-
'This indicates a bug in react-disposable-state.');
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
case 'NotInParentCacheAndNotDisposed': {
|
|
210
|
-
let cleared = false;
|
|
211
|
-
this.__state.permanentRetainCount++;
|
|
212
|
-
return () => {
|
|
213
|
-
if (cleared) {
|
|
214
|
-
throw new Error('A permanent retain should only be cleared once. ' +
|
|
215
|
-
'This indicates a bug in react-disposable-state.');
|
|
216
|
-
}
|
|
217
|
-
cleared = true;
|
|
218
|
-
switch (this.__state.kind) {
|
|
219
|
-
case 'NotInParentCacheAndNotDisposed': {
|
|
220
|
-
this.__state.permanentRetainCount--;
|
|
221
|
-
this.__maybeExitNotInParentCacheAndNotDisposedState(this.__state);
|
|
222
|
-
return;
|
|
223
|
-
}
|
|
224
|
-
default: {
|
|
225
|
-
throw new Error('CacheItem was in an unexpected state. ' +
|
|
226
|
-
'This indicates a bug in react-disposable-state.');
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
default: {
|
|
232
|
-
throw new Error('permanentRetain was called, but the CacheItem is in an invalid state. ' +
|
|
233
|
-
'This indicates a bug in react-disposable-state.');
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
__maybeExitInParentCacheAndNotDisposedState(state) {
|
|
238
|
-
if (state.temporaryRetainCount === 0 && state.permanentRetainCount === 0) {
|
|
239
|
-
state.removeFromParentCache();
|
|
240
|
-
state.disposeValue();
|
|
241
|
-
this.__state = {
|
|
242
|
-
kind: 'NotInParentCacheAndDisposed',
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
else if (state.temporaryRetainCount === 0) {
|
|
246
|
-
state.removeFromParentCache();
|
|
247
|
-
this.__state = {
|
|
248
|
-
kind: 'NotInParentCacheAndNotDisposed',
|
|
249
|
-
value: state.value,
|
|
250
|
-
disposeValue: state.disposeValue,
|
|
251
|
-
permanentRetainCount: state.permanentRetainCount,
|
|
252
|
-
};
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
__maybeExitNotInParentCacheAndNotDisposedState(state) {
|
|
256
|
-
if (state.permanentRetainCount === 0) {
|
|
257
|
-
state.disposeValue();
|
|
258
|
-
this.__state = {
|
|
259
|
-
kind: 'NotInParentCacheAndDisposed',
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
exports.CacheItem = CacheItem;
|
|
5
|
+
* CacheItem:
|
|
6
|
+
*
|
|
7
|
+
* Terminology:
|
|
8
|
+
* - TRC = Temporary Retain Count
|
|
9
|
+
* - PRC = Permanent Retain Count
|
|
10
|
+
*
|
|
11
|
+
* A CacheItem<T> can be in three states:
|
|
12
|
+
* In parent cache? | Item disposed? | TRC | PRC | Name
|
|
13
|
+
* -----------------+----------------+-----+-----+-------------------------------
|
|
14
|
+
* In parent cache | Not disposed | >0 | >=0 | InParentCacheAndNotDisposed
|
|
15
|
+
* Removed | Not disposed | 0 | >0 | NotInParentCacheAndNotDisposed
|
|
16
|
+
* Removed | Disposed | 0 | 0 | NotInParentCacheAndNotDisposed
|
|
17
|
+
*
|
|
18
|
+
* A cache item can only move down rows. As in, if its in the parent cache,
|
|
19
|
+
* it can be removed. It can never be replaced in the parent cache. (If a
|
|
20
|
+
* parent cache becomes full again, it will contain a new CacheItem.) The
|
|
21
|
+
* contained item can be disposed, but never un-disposed.
|
|
22
|
+
*
|
|
23
|
+
* So, the valid transitions are:
|
|
24
|
+
* - InParentCacheAndNotDisposed => NotInParentCacheAndNotDisposed
|
|
25
|
+
* - InParentCacheAndNotDisposed => NotInParentCacheAndDisposed
|
|
26
|
+
* - NotInParentCacheAndNotDisposed => NotInParentCacheAndDisposed
|
|
27
|
+
*/
|
|
28
|
+
var CacheItem = class {
|
|
29
|
+
constructor(factory, removeFromParentCache, options) {
|
|
30
|
+
this.__options = options ?? null;
|
|
31
|
+
const [value, disposeValue] = factory();
|
|
32
|
+
this.__state = {
|
|
33
|
+
kind: "InParentCacheAndNotDisposed",
|
|
34
|
+
value,
|
|
35
|
+
disposeValue,
|
|
36
|
+
removeFromParentCache,
|
|
37
|
+
temporaryRetainCount: 0,
|
|
38
|
+
permanentRetainCount: 0
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
getValue() {
|
|
42
|
+
switch (this.__state.kind) {
|
|
43
|
+
case "InParentCacheAndNotDisposed": return this.__state.value;
|
|
44
|
+
case "NotInParentCacheAndNotDisposed": return this.__state.value;
|
|
45
|
+
case "NotInParentCacheAndDisposed": throw new Error("Attempted to access disposed value from CacheItem. This indicates a bug in react-disposable-state.");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
permanentRetainIfNotDisposed(disposeOfTemporaryRetain) {
|
|
49
|
+
switch (this.__state.kind) {
|
|
50
|
+
case "InParentCacheAndNotDisposed": {
|
|
51
|
+
let cleared = false;
|
|
52
|
+
this.__state.permanentRetainCount++;
|
|
53
|
+
disposeOfTemporaryRetain();
|
|
54
|
+
return [this.__state.value, () => {
|
|
55
|
+
if (cleared) throw new Error("A permanent retain should only be cleared once. This indicates a bug in react-disposable-state.");
|
|
56
|
+
cleared = true;
|
|
57
|
+
switch (this.__state.kind) {
|
|
58
|
+
case "InParentCacheAndNotDisposed":
|
|
59
|
+
this.__state.permanentRetainCount--;
|
|
60
|
+
this.__maybeExitInParentCacheAndNotDisposedState(this.__state);
|
|
61
|
+
return;
|
|
62
|
+
case "NotInParentCacheAndNotDisposed":
|
|
63
|
+
this.__state.permanentRetainCount--;
|
|
64
|
+
this.__maybeExitNotInParentCacheAndNotDisposedState(this.__state);
|
|
65
|
+
return;
|
|
66
|
+
case "NotInParentCacheAndDisposed": throw new Error("CacheItem was in a disposed state, but there existed a permanent retain. This indicates a bug in react-disposable-state.");
|
|
67
|
+
}
|
|
68
|
+
}];
|
|
69
|
+
}
|
|
70
|
+
case "NotInParentCacheAndNotDisposed": {
|
|
71
|
+
let cleared = false;
|
|
72
|
+
this.__state.permanentRetainCount++;
|
|
73
|
+
disposeOfTemporaryRetain();
|
|
74
|
+
return [this.__state.value, () => {
|
|
75
|
+
if (cleared) throw new Error("A permanent retain should only be cleared once. This indicates a bug in react-disposable-state.");
|
|
76
|
+
cleared = true;
|
|
77
|
+
switch (this.__state.kind) {
|
|
78
|
+
case "NotInParentCacheAndNotDisposed":
|
|
79
|
+
this.__state.permanentRetainCount--;
|
|
80
|
+
this.__maybeExitNotInParentCacheAndNotDisposedState(this.__state);
|
|
81
|
+
return;
|
|
82
|
+
case "InParentCacheAndNotDisposed":
|
|
83
|
+
case "NotInParentCacheAndDisposed": throw new Error("CacheItem was in an unexpected state. This indicates a bug in react-disposable-state.");
|
|
84
|
+
}
|
|
85
|
+
}];
|
|
86
|
+
}
|
|
87
|
+
case "NotInParentCacheAndDisposed": return null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
temporaryRetain() {
|
|
91
|
+
switch (this.__state.kind) {
|
|
92
|
+
case "InParentCacheAndNotDisposed": {
|
|
93
|
+
let status = "Uncleared";
|
|
94
|
+
this.__state.temporaryRetainCount++;
|
|
95
|
+
const clearTemporaryRetainByCallack = () => {
|
|
96
|
+
if (status === "ClearedByCallback") throw new Error("A temporary retain should only be cleared once. This indicates a bug in react-disposable-state.");
|
|
97
|
+
else if (status === "Uncleared") switch (this.__state.kind) {
|
|
98
|
+
case "InParentCacheAndNotDisposed":
|
|
99
|
+
this.__state.temporaryRetainCount--;
|
|
100
|
+
this.__maybeExitInParentCacheAndNotDisposedState(this.__state);
|
|
101
|
+
clearTimeout(timeoutId);
|
|
102
|
+
return;
|
|
103
|
+
case "NotInParentCacheAndDisposed":
|
|
104
|
+
case "NotInParentCacheAndNotDisposed": throw new Error("A temporary retain was cleared, for which the CacheItem is in an invalid state. This indicates a bug in react-disposable-state.");
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const clearTemporaryRetainByTimeout = () => {
|
|
108
|
+
status = "ClearedByTimeout";
|
|
109
|
+
switch (this.__state.kind) {
|
|
110
|
+
case "InParentCacheAndNotDisposed":
|
|
111
|
+
this.__state.temporaryRetainCount--;
|
|
112
|
+
this.__maybeExitInParentCacheAndNotDisposedState(this.__state);
|
|
113
|
+
return;
|
|
114
|
+
case "NotInParentCacheAndDisposed":
|
|
115
|
+
case "NotInParentCacheAndNotDisposed": throw new Error("A temporary retain was cleared, for which the CacheItem is in an invalid state. This indicates a bug in react-disposable-state.");
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const timeoutId = setTimeout(clearTemporaryRetainByTimeout, this.__options?.temporaryRetainTime ?? DEFAULT_TEMPORARY_RETAIN_TIME);
|
|
119
|
+
return clearTemporaryRetainByCallack;
|
|
120
|
+
}
|
|
121
|
+
case "NotInParentCacheAndDisposed":
|
|
122
|
+
case "NotInParentCacheAndNotDisposed": throw new Error("temporaryRetain was called, for which the CacheItem is in an invalid state. This indicates a bug in react-disposable-state.");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
permanentRetain() {
|
|
126
|
+
switch (this.__state.kind) {
|
|
127
|
+
case "InParentCacheAndNotDisposed": {
|
|
128
|
+
let cleared = false;
|
|
129
|
+
this.__state.permanentRetainCount++;
|
|
130
|
+
return () => {
|
|
131
|
+
if (cleared) throw new Error("A permanent retain should only be cleared once. This indicates a bug in react-disposable-state.");
|
|
132
|
+
cleared = true;
|
|
133
|
+
switch (this.__state.kind) {
|
|
134
|
+
case "InParentCacheAndNotDisposed":
|
|
135
|
+
this.__state.permanentRetainCount--;
|
|
136
|
+
this.__maybeExitInParentCacheAndNotDisposedState(this.__state);
|
|
137
|
+
return;
|
|
138
|
+
case "NotInParentCacheAndNotDisposed":
|
|
139
|
+
this.__state.permanentRetainCount--;
|
|
140
|
+
this.__maybeExitNotInParentCacheAndNotDisposedState(this.__state);
|
|
141
|
+
return;
|
|
142
|
+
case "NotInParentCacheAndDisposed": throw new Error("CacheItem was in a disposed state, but there existed a permanent retain. This indicates a bug in react-disposable-state.");
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
case "NotInParentCacheAndNotDisposed": {
|
|
147
|
+
let cleared = false;
|
|
148
|
+
this.__state.permanentRetainCount++;
|
|
149
|
+
return () => {
|
|
150
|
+
if (cleared) throw new Error("A permanent retain should only be cleared once. This indicates a bug in react-disposable-state.");
|
|
151
|
+
cleared = true;
|
|
152
|
+
switch (this.__state.kind) {
|
|
153
|
+
case "NotInParentCacheAndNotDisposed":
|
|
154
|
+
this.__state.permanentRetainCount--;
|
|
155
|
+
this.__maybeExitNotInParentCacheAndNotDisposedState(this.__state);
|
|
156
|
+
return;
|
|
157
|
+
case "InParentCacheAndNotDisposed":
|
|
158
|
+
case "NotInParentCacheAndDisposed": throw new Error("CacheItem was in an unexpected state. This indicates a bug in react-disposable-state.");
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
case "NotInParentCacheAndDisposed": throw new Error("permanentRetain was called, but the CacheItem is in an invalid state. This indicates a bug in react-disposable-state.");
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
__maybeExitInParentCacheAndNotDisposedState(state) {
|
|
166
|
+
if (state.temporaryRetainCount === 0 && state.permanentRetainCount === 0) {
|
|
167
|
+
state.removeFromParentCache();
|
|
168
|
+
state.disposeValue();
|
|
169
|
+
this.__state = { kind: "NotInParentCacheAndDisposed" };
|
|
170
|
+
} else if (state.temporaryRetainCount === 0) {
|
|
171
|
+
state.removeFromParentCache();
|
|
172
|
+
this.__state = {
|
|
173
|
+
kind: "NotInParentCacheAndNotDisposed",
|
|
174
|
+
value: state.value,
|
|
175
|
+
disposeValue: state.disposeValue,
|
|
176
|
+
permanentRetainCount: state.permanentRetainCount
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
__maybeExitNotInParentCacheAndNotDisposedState(state) {
|
|
181
|
+
if (state.permanentRetainCount === 0) {
|
|
182
|
+
state.disposeValue();
|
|
183
|
+
this.__state = { kind: "NotInParentCacheAndDisposed" };
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
};
|
|
265
187
|
function createTemporarilyRetainedCacheItem(factory, removeFromParentCache, options) {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
return [cacheItem, disposeTemporaryRetain];
|
|
188
|
+
const cacheItem = new CacheItem(factory, removeFromParentCache, options);
|
|
189
|
+
return [cacheItem, cacheItem.temporaryRetain()];
|
|
269
190
|
}
|
|
191
|
+
|
|
192
|
+
//#endregion
|
|
193
|
+
exports.CacheItem = CacheItem;
|
|
194
|
+
exports.createTemporarilyRetainedCacheItem = createTemporarilyRetainedCacheItem;
|