@isograph/react-disposable-state 0.5.1 → 0.5.4
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 +5 -5
- package/.turbo/turbo-tsc.log +5 -0
- package/README.md +19 -0
- package/dist/{CacheItem.js → CacheItem.cjs} +11 -8
- package/dist/{CacheItem.d.ts → CacheItem.d.cts} +1 -1
- package/dist/CacheItem.d.cts.map +1 -0
- package/dist/CacheItem.d.mts.map +1 -1
- package/dist/CacheItem.mjs +11 -8
- package/dist/CacheItem.mjs.map +1 -1
- package/dist/{ParentCache.js → ParentCache.cjs} +4 -3
- package/dist/{ParentCache.d.ts → ParentCache.d.cts} +4 -4
- package/dist/ParentCache.d.cts.map +1 -0
- package/dist/ParentCache.d.mts +3 -3
- package/dist/ParentCache.d.mts.map +1 -1
- package/dist/ParentCache.mjs +3 -2
- package/dist/ParentCache.mjs.map +1 -1
- package/dist/{index.js → index.cjs} +14 -10
- package/dist/{index.d.ts → index.d.cts} +10 -8
- package/dist/index.d.mts +3 -1
- package/dist/index.mjs +3 -1
- package/dist/{useCachedResponsivePrecommitValue.js → useCachedResponsivePrecommitValue.cjs} +15 -9
- package/dist/{useCachedResponsivePrecommitValue.d.ts → useCachedResponsivePrecommitValue.d.cts} +2 -3
- package/dist/useCachedResponsivePrecommitValue.d.cts.map +1 -0
- package/dist/useCachedResponsivePrecommitValue.d.mts +0 -1
- package/dist/useCachedResponsivePrecommitValue.d.mts.map +1 -1
- package/dist/useCachedResponsivePrecommitValue.mjs +13 -7
- package/dist/useCachedResponsivePrecommitValue.mjs.map +1 -1
- package/dist/{useDisposableState.js → useDisposableState.cjs} +9 -10
- package/dist/{useDisposableState.d.ts → useDisposableState.d.cts} +3 -3
- package/dist/useDisposableState.d.cts.map +1 -0
- package/dist/useDisposableState.d.mts.map +1 -1
- package/dist/useDisposableState.mjs +7 -6
- package/dist/useDisposableState.mjs.map +1 -1
- package/dist/useEffectsRerunWithoutRender.cjs +27 -0
- package/dist/useEffectsRerunWithoutRender.d.cts +19 -0
- package/dist/useEffectsRerunWithoutRender.d.cts.map +1 -0
- package/dist/useEffectsRerunWithoutRender.d.mts +19 -0
- package/dist/useEffectsRerunWithoutRender.d.mts.map +1 -0
- package/dist/useEffectsRerunWithoutRender.mjs +28 -0
- package/dist/useEffectsRerunWithoutRender.mjs.map +1 -0
- package/dist/{useHasCommittedRef.js → useHasCommittedRef.cjs} +0 -2
- package/dist/{useHasCommittedRef.d.ts → useHasCommittedRef.d.cts} +1 -2
- package/dist/useHasCommittedRef.d.cts.map +1 -0
- package/dist/useHasCommittedRef.d.mts +0 -1
- package/dist/useHasCommittedRef.d.mts.map +1 -1
- package/dist/{useLazyDisposableState.js → useLazyDisposableState.cjs} +6 -6
- package/dist/{useLazyDisposableState.d.ts → useLazyDisposableState.d.cts} +3 -4
- package/dist/useLazyDisposableState.d.cts.map +1 -0
- package/dist/useLazyDisposableState.d.mts +0 -1
- package/dist/useLazyDisposableState.d.mts.map +1 -1
- package/dist/useLazyDisposableState.mjs +3 -3
- package/dist/useLazyDisposableState.mjs.map +1 -1
- package/dist/useUpdatableDisposableClearableState.cjs +36 -0
- package/dist/useUpdatableDisposableClearableState.d.cts +26 -0
- package/dist/useUpdatableDisposableClearableState.d.cts.map +1 -0
- package/dist/useUpdatableDisposableClearableState.d.mts +26 -0
- package/dist/useUpdatableDisposableClearableState.d.mts.map +1 -0
- package/dist/useUpdatableDisposableClearableState.mjs +37 -0
- package/dist/useUpdatableDisposableClearableState.mjs.map +1 -0
- package/dist/{useUpdatableDisposableState.js → useUpdatableDisposableState.cjs} +6 -4
- package/dist/{useUpdatableDisposableState.d.ts → useUpdatableDisposableState.d.cts} +1 -1
- package/dist/useUpdatableDisposableState.d.cts.map +1 -0
- package/dist/useUpdatableDisposableState.d.mts.map +1 -1
- package/dist/useUpdatableDisposableState.mjs +5 -1
- package/dist/useUpdatableDisposableState.mjs.map +1 -1
- package/package.json +9 -6
- package/src/CacheItem.test.ts +40 -2
- package/src/CacheItem.ts +1 -0
- package/src/ParentCache.ts +26 -21
- package/src/index.ts +2 -0
- package/src/useCachedResponsivePrecommitValue.test.tsx +8 -3
- package/src/useCachedResponsivePrecommitValue.ts +32 -20
- package/src/useDisposableState.ts +13 -15
- package/src/useEffectsRerunWithoutRender.test.tsx +54 -0
- package/src/useEffectsRerunWithoutRender.ts +23 -0
- package/src/useLazyDisposableState.test.tsx +73 -34
- package/src/useLazyDisposableState.ts +4 -5
- package/src/useUpdatableDisposableClearableState.ts +67 -0
- package/src/useUpdatableDisposableState.test.tsx +3 -2
- package/src/useUpdatableDisposableState.ts +2 -0
- package/tsconfig.json +2 -4
- package/vitest.config.ts +8 -0
- package/dist/CacheItem.d.ts.map +0 -1
- package/dist/ParentCache.d.ts.map +0 -1
- package/dist/_virtual/rolldown_runtime.js +0 -25
- package/dist/useCachedResponsivePrecommitValue.d.ts.map +0 -1
- package/dist/useDisposableState.d.ts.map +0 -1
- package/dist/useHasCommittedRef.d.ts.map +0 -1
- package/dist/useLazyDisposableState.d.ts.map +0 -1
- package/dist/useUpdatableDisposableState.d.ts.map +0 -1
- package/tsconfig.pkg.json +0 -9
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
../.. | WARN Unsupported engine: wanted: {"node":"
|
|
1
|
+
../.. | WARN Unsupported engine: wanted: {"node":"24.12.0"} (current: {"node":"v22.23.2","pnpm":"10.15.0"})
|
|
2
2
|
|
|
3
|
-
> @isograph/react-disposable-state@0.5.
|
|
3
|
+
> @isograph/react-disposable-state@0.5.4 compile-libs /home/runner/work/isograph/isograph/libs/isograph-react-disposable-state
|
|
4
4
|
> tsdown
|
|
5
5
|
|
|
6
|
-
[34mℹ[39m tsdown [2mv0.
|
|
7
|
-
[34mℹ[39m
|
|
8
|
-
(node:
|
|
6
|
+
[34mℹ[39m tsdown [2mv0.20.1[22m powered by rolldown [2mv1.0.0-rc.1[22m
|
|
7
|
+
[34mℹ[39m config file: [4m/home/runner/work/isograph/isograph/tsdown.config.ts[24m
|
|
8
|
+
(node:2709) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/runner/work/isograph/isograph/tsdown.config.ts?no-cache=cb34fd7c-d7fd-4eb6-92b4-94989215fe3c is not specified and it doesn't parse as CommonJS.
|
|
9
9
|
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
|
|
10
10
|
To eliminate this warning, add "type": "module" to /home/runner/work/isograph/isograph/package.json.
|
|
11
11
|
(Use `node --trace-warnings ...` to show where the warning was created)
|
package/README.md
CHANGED
|
@@ -110,6 +110,25 @@ const {
|
|
|
110
110
|
} = useUpdatableDisposableState<T>(options);
|
|
111
111
|
```
|
|
112
112
|
|
|
113
|
+
### `useUpdatableDisposableClearableState`
|
|
114
|
+
|
|
115
|
+
`useUpdatableDisposableState` with a third member, `clearState`:
|
|
116
|
+
|
|
117
|
+
- `clearState` returns the state to `UNASSIGNED_STATE`. The item that was in state is disposed on the next commit, exactly as if `setState` had superseded it.
|
|
118
|
+
- `clearState` throws if called before the initial commit, as `setState` does.
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
const {
|
|
122
|
+
state,
|
|
123
|
+
setState,
|
|
124
|
+
clearState,
|
|
125
|
+
}: {
|
|
126
|
+
state: T | UnassignedState;
|
|
127
|
+
setState: (pair: ItemCleanupPair<T>) => void;
|
|
128
|
+
clearState: () => void;
|
|
129
|
+
} = useUpdatableDisposableClearableState<T>();
|
|
130
|
+
```
|
|
131
|
+
|
|
113
132
|
### `useDisposableState`
|
|
114
133
|
|
|
115
134
|
> This could properly be called `useLazyUpdatableDisposableState`, but that's quite long!
|
|
@@ -94,14 +94,17 @@ var CacheItem = class {
|
|
|
94
94
|
this.__state.temporaryRetainCount++;
|
|
95
95
|
const clearTemporaryRetainByCallack = () => {
|
|
96
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")
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
97
|
+
else if (status === "Uncleared") {
|
|
98
|
+
status = "ClearedByCallback";
|
|
99
|
+
switch (this.__state.kind) {
|
|
100
|
+
case "InParentCacheAndNotDisposed":
|
|
101
|
+
this.__state.temporaryRetainCount--;
|
|
102
|
+
this.__maybeExitInParentCacheAndNotDisposedState(this.__state);
|
|
103
|
+
clearTimeout(timeoutId);
|
|
104
|
+
return;
|
|
105
|
+
case "NotInParentCacheAndDisposed":
|
|
106
|
+
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.");
|
|
107
|
+
}
|
|
105
108
|
}
|
|
106
109
|
};
|
|
107
110
|
const clearTemporaryRetainByTimeout = () => {
|
|
@@ -60,4 +60,4 @@ declare class CacheItem<T> {
|
|
|
60
60
|
declare function createTemporarilyRetainedCacheItem<T>(factory: Factory<T>, removeFromParentCache: CleanupFn, options: CacheItemOptions | void): [CacheItem<T>, CleanupFn];
|
|
61
61
|
//#endregion
|
|
62
62
|
export { CacheItem, CacheItemOptions, CacheItemState, InParentCacheAndNotDisposed, NotInParentCacheAndDisposed, NotInParentCacheAndNotDisposed, createTemporarilyRetainedCacheItem };
|
|
63
|
-
//# sourceMappingURL=CacheItem.d.
|
|
63
|
+
//# sourceMappingURL=CacheItem.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CacheItem.d.cts","names":[],"sources":["../src/CacheItem.ts"],"mappings":";;;KAQY,2BAAA;EACV,IAAA;AAAA;AAAA,KAEU,8BAAA;EACV,IAAA;EACA,KAAA,EAAO,CAAA;EACP,YAAA;EAGA,oBAAA;AAAA;AAAA,KAEU,2BAAA;EACV,IAAA;EACA,KAAA,EAAO,CAAA;EACP,YAAA;EACA,qBAAA;EAGA,oBAAA;EAGA,oBAAA;AAAA;AAAA,KAGU,cAAA,MACR,2BAAA,CAA4B,CAAA,IAC5B,8BAAA,CAA+B,CAAA,IAC/B,2BAAA;AAAA,KAEQ,gBAAA;EACV,mBAAA;AAAA;;;;;;;;;;;;;AANF;;;;;;;;;;;;cAoCa,SAAA;EAAA,QACH,OAAA;EAAA,QACA,SAAA;cAQN,OAAA,EAAS,OAAA,CAAQ,CAAA,GACjB,qBAAA,EAAuB,SAAA,EACvB,OAAA,EAAS,gBAAA;EAgBX,QAAA,CAAA,GAAY,CAAA;EAiBZ,4BAAA,CACE,wBAAA,EAA0B,SAAA,GACzB,eAAA,CAAgB,CAAA;EA+EnB,eAAA,CAAA,GAAmB,SAAA;EAsEnB,eAAA,CAAA,GAAmB,SAAA;EAAA,QAqEX,2CAAA;EAAA,QAoBA,8CAAA;AAAA;AAAA,iBAYM,kCAAA,GAAA,CACd,OAAA,EAAS,OAAA,CAAQ,CAAA,GACjB,qBAAA,EAAuB,SAAA,EACvB,OAAA,EAAS,gBAAA,WACP,SAAA,CAAU,CAAA,GAAI,SAAA"}
|
package/dist/CacheItem.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CacheItem.d.mts","names":[],"sources":["../src/CacheItem.ts"],"
|
|
1
|
+
{"version":3,"file":"CacheItem.d.mts","names":[],"sources":["../src/CacheItem.ts"],"mappings":";;;KAQY,2BAAA;EACV,IAAA;AAAA;AAAA,KAEU,8BAAA;EACV,IAAA;EACA,KAAA,EAAO,CAAA;EACP,YAAA;EAGA,oBAAA;AAAA;AAAA,KAEU,2BAAA;EACV,IAAA;EACA,KAAA,EAAO,CAAA;EACP,YAAA;EACA,qBAAA;EAGA,oBAAA;EAGA,oBAAA;AAAA;AAAA,KAGU,cAAA,MACR,2BAAA,CAA4B,CAAA,IAC5B,8BAAA,CAA+B,CAAA,IAC/B,2BAAA;AAAA,KAEQ,gBAAA;EACV,mBAAA;AAAA;;;;;;;;;;;;;AANF;;;;;;;;;;;;cAoCa,SAAA;EAAA,QACH,OAAA;EAAA,QACA,SAAA;cAQN,OAAA,EAAS,OAAA,CAAQ,CAAA,GACjB,qBAAA,EAAuB,SAAA,EACvB,OAAA,EAAS,gBAAA;EAgBX,QAAA,CAAA,GAAY,CAAA;EAiBZ,4BAAA,CACE,wBAAA,EAA0B,SAAA,GACzB,eAAA,CAAgB,CAAA;EA+EnB,eAAA,CAAA,GAAmB,SAAA;EAsEnB,eAAA,CAAA,GAAmB,SAAA;EAAA,QAqEX,2CAAA;EAAA,QAoBA,8CAAA;AAAA;AAAA,iBAYM,kCAAA,GAAA,CACd,OAAA,EAAS,OAAA,CAAQ,CAAA,GACjB,qBAAA,EAAuB,SAAA,EACvB,OAAA,EAAS,gBAAA,WACP,SAAA,CAAU,CAAA,GAAI,SAAA"}
|
package/dist/CacheItem.mjs
CHANGED
|
@@ -93,14 +93,17 @@ var CacheItem = class {
|
|
|
93
93
|
this.__state.temporaryRetainCount++;
|
|
94
94
|
const clearTemporaryRetainByCallack = () => {
|
|
95
95
|
if (status === "ClearedByCallback") throw new Error("A temporary retain should only be cleared once. This indicates a bug in react-disposable-state.");
|
|
96
|
-
else if (status === "Uncleared")
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
96
|
+
else if (status === "Uncleared") {
|
|
97
|
+
status = "ClearedByCallback";
|
|
98
|
+
switch (this.__state.kind) {
|
|
99
|
+
case "InParentCacheAndNotDisposed":
|
|
100
|
+
this.__state.temporaryRetainCount--;
|
|
101
|
+
this.__maybeExitInParentCacheAndNotDisposedState(this.__state);
|
|
102
|
+
clearTimeout(timeoutId);
|
|
103
|
+
return;
|
|
104
|
+
case "NotInParentCacheAndDisposed":
|
|
105
|
+
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.");
|
|
106
|
+
}
|
|
104
107
|
}
|
|
105
108
|
};
|
|
106
109
|
const clearTemporaryRetainByTimeout = () => {
|
package/dist/CacheItem.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CacheItem.mjs","names":["status: TemporaryRetainStatus","clearTemporaryRetainByCallack: CleanupFn"],"sources":["../src/CacheItem.ts"],"sourcesContent":["import type {\n CleanupFn,\n Factory,\n ItemCleanupPair,\n} from '@isograph/disposable-types';\n\nconst DEFAULT_TEMPORARY_RETAIN_TIME = 5000;\n\nexport type NotInParentCacheAndDisposed = {\n kind: 'NotInParentCacheAndDisposed';\n};\nexport type NotInParentCacheAndNotDisposed<T> = {\n kind: 'NotInParentCacheAndNotDisposed';\n value: T;\n disposeValue: () => void;\n\n // Invariant: >0\n permanentRetainCount: number;\n};\nexport type InParentCacheAndNotDisposed<T> = {\n kind: 'InParentCacheAndNotDisposed';\n value: T;\n disposeValue: () => void;\n removeFromParentCache: () => void;\n\n // Invariant: >0\n temporaryRetainCount: number;\n\n // Invariant: >= 0\n permanentRetainCount: number;\n};\n\nexport type CacheItemState<T> =\n | InParentCacheAndNotDisposed<T>\n | NotInParentCacheAndNotDisposed<T>\n | NotInParentCacheAndDisposed;\n\nexport type CacheItemOptions = {\n temporaryRetainTime: number;\n};\n\n// TODO don't export this class, only export type (interface) instead\n// TODO convert cacheitem impl to a getter and setter and free functions\n\n/**\n * CacheItem:\n *\n * Terminology:\n * - TRC = Temporary Retain Count\n * - PRC = Permanent Retain Count\n *\n * A CacheItem<T> can be in three states:\n * In parent cache? | Item disposed? | TRC | PRC | Name\n * -----------------+----------------+-----+-----+-------------------------------\n * In parent cache | Not disposed | >0 | >=0 | InParentCacheAndNotDisposed\n * Removed | Not disposed | 0 | >0 | NotInParentCacheAndNotDisposed\n * Removed | Disposed | 0 | 0 | NotInParentCacheAndNotDisposed\n *\n * A cache item can only move down rows. As in, if its in the parent cache,\n * it can be removed. It can never be replaced in the parent cache. (If a\n * parent cache becomes full again, it will contain a new CacheItem.) The\n * contained item can be disposed, but never un-disposed.\n *\n * So, the valid transitions are:\n * - InParentCacheAndNotDisposed => NotInParentCacheAndNotDisposed\n * - InParentCacheAndNotDisposed => NotInParentCacheAndDisposed\n * - NotInParentCacheAndNotDisposed => NotInParentCacheAndDisposed\n */\nexport class CacheItem<T> {\n private __state: CacheItemState<T>;\n private __options: CacheItemOptions | null;\n\n // Private. Do not call this constructor directly. Use\n // createTemporarilyRetainedCacheItem instead. This is because this\n // constructor creates a CacheItem in an invalid state. It must be\n // temporarily retained to enter a valid state, and JavaScript doesn't\n // let you return a tuple from a constructor.\n constructor(\n factory: Factory<T>,\n removeFromParentCache: CleanupFn,\n options: CacheItemOptions | void,\n ) {\n this.__options = options ?? null;\n const [value, disposeValue] = factory();\n this.__state = {\n kind: 'InParentCacheAndNotDisposed',\n value,\n disposeValue,\n removeFromParentCache,\n // NOTE: we are creating the CacheItem in an invalid state. This is okay, because\n // we are immediately calling .temporaryRetain.\n temporaryRetainCount: 0,\n permanentRetainCount: 0,\n };\n }\n\n getValue(): T {\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n return this.__state.value;\n }\n case 'NotInParentCacheAndNotDisposed': {\n return this.__state.value;\n }\n case 'NotInParentCacheAndDisposed': {\n throw new Error(\n 'Attempted to access disposed value from CacheItem. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n }\n\n permanentRetainIfNotDisposed(\n disposeOfTemporaryRetain: CleanupFn,\n ): ItemCleanupPair<T> | null {\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n let cleared = false;\n this.__state.permanentRetainCount++;\n disposeOfTemporaryRetain();\n return [\n this.__state.value,\n () => {\n if (cleared) {\n throw new Error(\n 'A permanent retain should only be cleared once. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n cleared = true;\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n this.__state.permanentRetainCount--;\n this.__maybeExitInParentCacheAndNotDisposedState(this.__state);\n return;\n }\n case 'NotInParentCacheAndNotDisposed': {\n this.__state.permanentRetainCount--;\n this.__maybeExitNotInParentCacheAndNotDisposedState(\n this.__state,\n );\n return;\n }\n case 'NotInParentCacheAndDisposed': {\n throw new Error(\n 'CacheItem was in a disposed state, but there existed a permanent retain. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n },\n ];\n }\n case 'NotInParentCacheAndNotDisposed': {\n let cleared = false;\n this.__state.permanentRetainCount++;\n disposeOfTemporaryRetain();\n return [\n this.__state.value,\n () => {\n if (cleared) {\n throw new Error(\n 'A permanent retain should only be cleared once. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n cleared = true;\n switch (this.__state.kind) {\n case 'NotInParentCacheAndNotDisposed': {\n this.__state.permanentRetainCount--;\n this.__maybeExitNotInParentCacheAndNotDisposedState(\n this.__state,\n );\n return;\n }\n case 'InParentCacheAndNotDisposed':\n case 'NotInParentCacheAndDisposed': {\n throw new Error(\n 'CacheItem was in an unexpected state. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n },\n ];\n }\n case 'NotInParentCacheAndDisposed': {\n // The CacheItem is disposed, so disposeOfTemporaryRetain is a no-op\n return null;\n }\n }\n }\n\n temporaryRetain(): CleanupFn {\n type TemporaryRetainStatus =\n | 'Uncleared'\n | 'ClearedByCallback'\n | 'ClearedByTimeout';\n\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n let status: TemporaryRetainStatus = 'Uncleared';\n this.__state.temporaryRetainCount++;\n const clearTemporaryRetainByCallack: CleanupFn = () => {\n if (status === 'ClearedByCallback') {\n throw new Error(\n 'A temporary retain should only be cleared once. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n } else if (status === 'Uncleared') {\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n this.__state.temporaryRetainCount--;\n this.__maybeExitInParentCacheAndNotDisposedState(this.__state);\n clearTimeout(timeoutId);\n return;\n }\n case 'NotInParentCacheAndDisposed':\n case 'NotInParentCacheAndNotDisposed': {\n throw new Error(\n 'A temporary retain was cleared, for which the CacheItem is in an invalid state. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n }\n };\n\n const clearTemporaryRetainByTimeout = () => {\n status = 'ClearedByTimeout';\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n this.__state.temporaryRetainCount--;\n this.__maybeExitInParentCacheAndNotDisposedState(this.__state);\n return;\n }\n case 'NotInParentCacheAndDisposed':\n case 'NotInParentCacheAndNotDisposed': {\n throw new Error(\n 'A temporary retain was cleared, for which the CacheItem is in an invalid state. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n };\n\n const timeoutId = setTimeout(\n clearTemporaryRetainByTimeout,\n this.__options?.temporaryRetainTime ?? DEFAULT_TEMPORARY_RETAIN_TIME,\n );\n return clearTemporaryRetainByCallack;\n }\n case 'NotInParentCacheAndDisposed':\n case 'NotInParentCacheAndNotDisposed': {\n throw new Error(\n 'temporaryRetain was called, for which the CacheItem is in an invalid state. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n }\n\n permanentRetain(): CleanupFn {\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n let cleared = false;\n this.__state.permanentRetainCount++;\n return () => {\n if (cleared) {\n throw new Error(\n 'A permanent retain should only be cleared once. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n cleared = true;\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n this.__state.permanentRetainCount--;\n this.__maybeExitInParentCacheAndNotDisposedState(this.__state);\n return;\n }\n case 'NotInParentCacheAndNotDisposed': {\n this.__state.permanentRetainCount--;\n this.__maybeExitNotInParentCacheAndNotDisposedState(this.__state);\n return;\n }\n case 'NotInParentCacheAndDisposed': {\n throw new Error(\n 'CacheItem was in a disposed state, but there existed a permanent retain. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n };\n }\n case 'NotInParentCacheAndNotDisposed': {\n let cleared = false;\n this.__state.permanentRetainCount++;\n return () => {\n if (cleared) {\n throw new Error(\n 'A permanent retain should only be cleared once. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n cleared = true;\n switch (this.__state.kind) {\n case 'NotInParentCacheAndNotDisposed': {\n this.__state.permanentRetainCount--;\n this.__maybeExitNotInParentCacheAndNotDisposedState(this.__state);\n return;\n }\n case 'InParentCacheAndNotDisposed':\n case 'NotInParentCacheAndDisposed': {\n throw new Error(\n 'CacheItem was in an unexpected state. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n };\n }\n case 'NotInParentCacheAndDisposed': {\n throw new Error(\n 'permanentRetain was called, but the CacheItem is in an invalid state. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n }\n\n private __maybeExitInParentCacheAndNotDisposedState(\n state: InParentCacheAndNotDisposed<T>,\n ) {\n if (state.temporaryRetainCount === 0 && state.permanentRetainCount === 0) {\n state.removeFromParentCache();\n state.disposeValue();\n this.__state = {\n kind: 'NotInParentCacheAndDisposed',\n };\n } else if (state.temporaryRetainCount === 0) {\n state.removeFromParentCache();\n this.__state = {\n kind: 'NotInParentCacheAndNotDisposed',\n value: state.value,\n disposeValue: state.disposeValue,\n permanentRetainCount: state.permanentRetainCount,\n };\n }\n }\n\n private __maybeExitNotInParentCacheAndNotDisposedState(\n state: NotInParentCacheAndNotDisposed<T>,\n ) {\n if (state.permanentRetainCount === 0) {\n state.disposeValue();\n this.__state = {\n kind: 'NotInParentCacheAndDisposed',\n };\n }\n }\n}\n\nexport function createTemporarilyRetainedCacheItem<T>(\n factory: Factory<T>,\n removeFromParentCache: CleanupFn,\n options: CacheItemOptions | void,\n): [CacheItem<T>, CleanupFn] {\n const cacheItem = new CacheItem(factory, removeFromParentCache, options);\n const disposeTemporaryRetain = cacheItem.temporaryRetain();\n return [cacheItem, disposeTemporaryRetain];\n}\n"],"mappings":"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"}
|
|
1
|
+
{"version":3,"file":"CacheItem.mjs","names":[],"sources":["../src/CacheItem.ts"],"sourcesContent":["import type {\n CleanupFn,\n Factory,\n ItemCleanupPair,\n} from '@isograph/disposable-types';\n\nconst DEFAULT_TEMPORARY_RETAIN_TIME = 5000;\n\nexport type NotInParentCacheAndDisposed = {\n kind: 'NotInParentCacheAndDisposed';\n};\nexport type NotInParentCacheAndNotDisposed<T> = {\n kind: 'NotInParentCacheAndNotDisposed';\n value: T;\n disposeValue: () => void;\n\n // Invariant: >0\n permanentRetainCount: number;\n};\nexport type InParentCacheAndNotDisposed<T> = {\n kind: 'InParentCacheAndNotDisposed';\n value: T;\n disposeValue: () => void;\n removeFromParentCache: () => void;\n\n // Invariant: >0\n temporaryRetainCount: number;\n\n // Invariant: >= 0\n permanentRetainCount: number;\n};\n\nexport type CacheItemState<T> =\n | InParentCacheAndNotDisposed<T>\n | NotInParentCacheAndNotDisposed<T>\n | NotInParentCacheAndDisposed;\n\nexport type CacheItemOptions = {\n temporaryRetainTime: number;\n};\n\n// TODO don't export this class, only export type (interface) instead\n// TODO convert cacheitem impl to a getter and setter and free functions\n\n/**\n * CacheItem:\n *\n * Terminology:\n * - TRC = Temporary Retain Count\n * - PRC = Permanent Retain Count\n *\n * A CacheItem<T> can be in three states:\n * In parent cache? | Item disposed? | TRC | PRC | Name\n * -----------------+----------------+-----+-----+-------------------------------\n * In parent cache | Not disposed | >0 | >=0 | InParentCacheAndNotDisposed\n * Removed | Not disposed | 0 | >0 | NotInParentCacheAndNotDisposed\n * Removed | Disposed | 0 | 0 | NotInParentCacheAndNotDisposed\n *\n * A cache item can only move down rows. As in, if its in the parent cache,\n * it can be removed. It can never be replaced in the parent cache. (If a\n * parent cache becomes full again, it will contain a new CacheItem.) The\n * contained item can be disposed, but never un-disposed.\n *\n * So, the valid transitions are:\n * - InParentCacheAndNotDisposed => NotInParentCacheAndNotDisposed\n * - InParentCacheAndNotDisposed => NotInParentCacheAndDisposed\n * - NotInParentCacheAndNotDisposed => NotInParentCacheAndDisposed\n */\nexport class CacheItem<T> {\n private __state: CacheItemState<T>;\n private __options: CacheItemOptions | null;\n\n // Private. Do not call this constructor directly. Use\n // createTemporarilyRetainedCacheItem instead. This is because this\n // constructor creates a CacheItem in an invalid state. It must be\n // temporarily retained to enter a valid state, and JavaScript doesn't\n // let you return a tuple from a constructor.\n constructor(\n factory: Factory<T>,\n removeFromParentCache: CleanupFn,\n options: CacheItemOptions | void,\n ) {\n this.__options = options ?? null;\n const [value, disposeValue] = factory();\n this.__state = {\n kind: 'InParentCacheAndNotDisposed',\n value,\n disposeValue,\n removeFromParentCache,\n // NOTE: we are creating the CacheItem in an invalid state. This is okay, because\n // we are immediately calling .temporaryRetain.\n temporaryRetainCount: 0,\n permanentRetainCount: 0,\n };\n }\n\n getValue(): T {\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n return this.__state.value;\n }\n case 'NotInParentCacheAndNotDisposed': {\n return this.__state.value;\n }\n case 'NotInParentCacheAndDisposed': {\n throw new Error(\n 'Attempted to access disposed value from CacheItem. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n }\n\n permanentRetainIfNotDisposed(\n disposeOfTemporaryRetain: CleanupFn,\n ): ItemCleanupPair<T> | null {\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n let cleared = false;\n this.__state.permanentRetainCount++;\n disposeOfTemporaryRetain();\n return [\n this.__state.value,\n () => {\n if (cleared) {\n throw new Error(\n 'A permanent retain should only be cleared once. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n cleared = true;\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n this.__state.permanentRetainCount--;\n this.__maybeExitInParentCacheAndNotDisposedState(this.__state);\n return;\n }\n case 'NotInParentCacheAndNotDisposed': {\n this.__state.permanentRetainCount--;\n this.__maybeExitNotInParentCacheAndNotDisposedState(\n this.__state,\n );\n return;\n }\n case 'NotInParentCacheAndDisposed': {\n throw new Error(\n 'CacheItem was in a disposed state, but there existed a permanent retain. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n },\n ];\n }\n case 'NotInParentCacheAndNotDisposed': {\n let cleared = false;\n this.__state.permanentRetainCount++;\n disposeOfTemporaryRetain();\n return [\n this.__state.value,\n () => {\n if (cleared) {\n throw new Error(\n 'A permanent retain should only be cleared once. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n cleared = true;\n switch (this.__state.kind) {\n case 'NotInParentCacheAndNotDisposed': {\n this.__state.permanentRetainCount--;\n this.__maybeExitNotInParentCacheAndNotDisposedState(\n this.__state,\n );\n return;\n }\n case 'InParentCacheAndNotDisposed':\n case 'NotInParentCacheAndDisposed': {\n throw new Error(\n 'CacheItem was in an unexpected state. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n },\n ];\n }\n case 'NotInParentCacheAndDisposed': {\n // The CacheItem is disposed, so disposeOfTemporaryRetain is a no-op\n return null;\n }\n }\n }\n\n temporaryRetain(): CleanupFn {\n type TemporaryRetainStatus =\n | 'Uncleared'\n | 'ClearedByCallback'\n | 'ClearedByTimeout';\n\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n let status: TemporaryRetainStatus = 'Uncleared';\n this.__state.temporaryRetainCount++;\n const clearTemporaryRetainByCallack: CleanupFn = () => {\n if (status === 'ClearedByCallback') {\n throw new Error(\n 'A temporary retain should only be cleared once. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n } else if (status === 'Uncleared') {\n status = 'ClearedByCallback';\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n this.__state.temporaryRetainCount--;\n this.__maybeExitInParentCacheAndNotDisposedState(this.__state);\n clearTimeout(timeoutId);\n return;\n }\n case 'NotInParentCacheAndDisposed':\n case 'NotInParentCacheAndNotDisposed': {\n throw new Error(\n 'A temporary retain was cleared, for which the CacheItem is in an invalid state. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n }\n };\n\n const clearTemporaryRetainByTimeout = () => {\n status = 'ClearedByTimeout';\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n this.__state.temporaryRetainCount--;\n this.__maybeExitInParentCacheAndNotDisposedState(this.__state);\n return;\n }\n case 'NotInParentCacheAndDisposed':\n case 'NotInParentCacheAndNotDisposed': {\n throw new Error(\n 'A temporary retain was cleared, for which the CacheItem is in an invalid state. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n };\n\n const timeoutId = setTimeout(\n clearTemporaryRetainByTimeout,\n this.__options?.temporaryRetainTime ?? DEFAULT_TEMPORARY_RETAIN_TIME,\n );\n return clearTemporaryRetainByCallack;\n }\n case 'NotInParentCacheAndDisposed':\n case 'NotInParentCacheAndNotDisposed': {\n throw new Error(\n 'temporaryRetain was called, for which the CacheItem is in an invalid state. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n }\n\n permanentRetain(): CleanupFn {\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n let cleared = false;\n this.__state.permanentRetainCount++;\n return () => {\n if (cleared) {\n throw new Error(\n 'A permanent retain should only be cleared once. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n cleared = true;\n switch (this.__state.kind) {\n case 'InParentCacheAndNotDisposed': {\n this.__state.permanentRetainCount--;\n this.__maybeExitInParentCacheAndNotDisposedState(this.__state);\n return;\n }\n case 'NotInParentCacheAndNotDisposed': {\n this.__state.permanentRetainCount--;\n this.__maybeExitNotInParentCacheAndNotDisposedState(this.__state);\n return;\n }\n case 'NotInParentCacheAndDisposed': {\n throw new Error(\n 'CacheItem was in a disposed state, but there existed a permanent retain. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n };\n }\n case 'NotInParentCacheAndNotDisposed': {\n let cleared = false;\n this.__state.permanentRetainCount++;\n return () => {\n if (cleared) {\n throw new Error(\n 'A permanent retain should only be cleared once. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n cleared = true;\n switch (this.__state.kind) {\n case 'NotInParentCacheAndNotDisposed': {\n this.__state.permanentRetainCount--;\n this.__maybeExitNotInParentCacheAndNotDisposedState(this.__state);\n return;\n }\n case 'InParentCacheAndNotDisposed':\n case 'NotInParentCacheAndDisposed': {\n throw new Error(\n 'CacheItem was in an unexpected state. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n };\n }\n case 'NotInParentCacheAndDisposed': {\n throw new Error(\n 'permanentRetain was called, but the CacheItem is in an invalid state. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n }\n\n private __maybeExitInParentCacheAndNotDisposedState(\n state: InParentCacheAndNotDisposed<T>,\n ) {\n if (state.temporaryRetainCount === 0 && state.permanentRetainCount === 0) {\n state.removeFromParentCache();\n state.disposeValue();\n this.__state = {\n kind: 'NotInParentCacheAndDisposed',\n };\n } else if (state.temporaryRetainCount === 0) {\n state.removeFromParentCache();\n this.__state = {\n kind: 'NotInParentCacheAndNotDisposed',\n value: state.value,\n disposeValue: state.disposeValue,\n permanentRetainCount: state.permanentRetainCount,\n };\n }\n }\n\n private __maybeExitNotInParentCacheAndNotDisposedState(\n state: NotInParentCacheAndNotDisposed<T>,\n ) {\n if (state.permanentRetainCount === 0) {\n state.disposeValue();\n this.__state = {\n kind: 'NotInParentCacheAndDisposed',\n };\n }\n }\n}\n\nexport function createTemporarilyRetainedCacheItem<T>(\n factory: Factory<T>,\n removeFromParentCache: CleanupFn,\n options: CacheItemOptions | void,\n): [CacheItem<T>, CleanupFn] {\n const cacheItem = new CacheItem(factory, removeFromParentCache, options);\n const disposeTemporaryRetain = cacheItem.temporaryRetain();\n return [cacheItem, disposeTemporaryRetain];\n}\n"],"mappings":"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"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_CacheItem = require('./CacheItem.
|
|
1
|
+
const require_CacheItem = require('./CacheItem.cjs');
|
|
2
2
|
|
|
3
3
|
//#region src/ParentCache.ts
|
|
4
4
|
/**
|
|
@@ -18,9 +18,10 @@ const require_CacheItem = require('./CacheItem.js');
|
|
|
18
18
|
* be called
|
|
19
19
|
*/
|
|
20
20
|
var ParentCache = class {
|
|
21
|
-
constructor(factory) {
|
|
21
|
+
constructor(factory, options) {
|
|
22
22
|
this.__cacheItem = null;
|
|
23
23
|
this.__factory = factory;
|
|
24
|
+
this.__options = options;
|
|
24
25
|
}
|
|
25
26
|
/**
|
|
26
27
|
* This is called from useCachedResponsivePrecommitValue, when the parent cache is populated
|
|
@@ -41,7 +42,7 @@ var ParentCache = class {
|
|
|
41
42
|
__populateAndTemporaryRetain() {
|
|
42
43
|
const pair = require_CacheItem.createTemporarilyRetainedCacheItem(this.__factory, () => {
|
|
43
44
|
if (this.__cacheItem === pair[0]) this.empty();
|
|
44
|
-
});
|
|
45
|
+
}, this.__options);
|
|
45
46
|
const [cacheItem, disposeTemporaryRetain] = pair;
|
|
46
47
|
this.__cacheItem = cacheItem;
|
|
47
48
|
return [
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { CacheItem } from "./CacheItem.
|
|
1
|
+
import { CacheItem, CacheItemOptions } from "./CacheItem.cjs";
|
|
2
2
|
import { CleanupFn, Factory, ItemCleanupPair } from "@isograph/disposable-types";
|
|
3
3
|
|
|
4
4
|
//#region src/ParentCache.d.ts
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
6
|
* ParentCache
|
|
8
7
|
* - A ParentCache can be in two states: populated and unpopulated.
|
|
@@ -22,7 +21,8 @@ import { CleanupFn, Factory, ItemCleanupPair } from "@isograph/disposable-types"
|
|
|
22
21
|
declare class ParentCache<T> {
|
|
23
22
|
private __cacheItem;
|
|
24
23
|
private readonly __factory;
|
|
25
|
-
|
|
24
|
+
private readonly __options;
|
|
25
|
+
constructor(factory: Factory<T>, options?: CacheItemOptions);
|
|
26
26
|
/**
|
|
27
27
|
* This is called from useCachedResponsivePrecommitValue, when the parent cache is populated
|
|
28
28
|
* and a previous temporary retain has been disposed. This can occur in scenarios like:
|
|
@@ -42,4 +42,4 @@ declare class ParentCache<T> {
|
|
|
42
42
|
}
|
|
43
43
|
//#endregion
|
|
44
44
|
export { ParentCache };
|
|
45
|
-
//# sourceMappingURL=ParentCache.d.
|
|
45
|
+
//# sourceMappingURL=ParentCache.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ParentCache.d.cts","names":[],"sources":["../src/ParentCache.ts"],"mappings":";;;;;;AA0BA;;;;;;;;;;;;;;cAAa,WAAA;EAAA,QACH,WAAA;EAAA,iBACS,SAAA;EAAA,iBACA,SAAA;cAIL,OAAA,EAAS,OAAA,CAAQ,CAAA,GAAI,OAAA,GAAU,gBAAA;EAJ1B;;;;;;;;;;EAmBjB,8BAAA,CAAA,GAAkC,eAAA,CAAgB,CAAA;EAMlD,+BAAA,CAAA,IAAoC,SAAA,CAAU,CAAA,GAAI,CAAA,EAAG,SAAA;EAAA,QAM7C,4BAAA;EAgCR,KAAA,CAAA;EAAA,IAII,OAAA,CAAA,GAAW,OAAA,CAAQ,CAAA;EAIvB,OAAA,CAAA;AAAA"}
|
package/dist/ParentCache.d.mts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { CacheItem } from "./CacheItem.mjs";
|
|
1
|
+
import { CacheItem, CacheItemOptions } from "./CacheItem.mjs";
|
|
2
2
|
import { CleanupFn, Factory, ItemCleanupPair } from "@isograph/disposable-types";
|
|
3
3
|
|
|
4
4
|
//#region src/ParentCache.d.ts
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
6
|
* ParentCache
|
|
8
7
|
* - A ParentCache can be in two states: populated and unpopulated.
|
|
@@ -22,7 +21,8 @@ import { CleanupFn, Factory, ItemCleanupPair } from "@isograph/disposable-types"
|
|
|
22
21
|
declare class ParentCache<T> {
|
|
23
22
|
private __cacheItem;
|
|
24
23
|
private readonly __factory;
|
|
25
|
-
|
|
24
|
+
private readonly __options;
|
|
25
|
+
constructor(factory: Factory<T>, options?: CacheItemOptions);
|
|
26
26
|
/**
|
|
27
27
|
* This is called from useCachedResponsivePrecommitValue, when the parent cache is populated
|
|
28
28
|
* and a previous temporary retain has been disposed. This can occur in scenarios like:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ParentCache.d.mts","names":[],"sources":["../src/ParentCache.ts"],"
|
|
1
|
+
{"version":3,"file":"ParentCache.d.mts","names":[],"sources":["../src/ParentCache.ts"],"mappings":";;;;;;AA0BA;;;;;;;;;;;;;;cAAa,WAAA;EAAA,QACH,WAAA;EAAA,iBACS,SAAA;EAAA,iBACA,SAAA;cAIL,OAAA,EAAS,OAAA,CAAQ,CAAA,GAAI,OAAA,GAAU,gBAAA;EAJ1B;;;;;;;;;;EAmBjB,8BAAA,CAAA,GAAkC,eAAA,CAAgB,CAAA;EAMlD,+BAAA,CAAA,IAAoC,SAAA,CAAU,CAAA,GAAI,CAAA,EAAG,SAAA;EAAA,QAM7C,4BAAA;EAgCR,KAAA,CAAA;EAAA,IAII,OAAA,CAAA,GAAW,OAAA,CAAQ,CAAA;EAIvB,OAAA,CAAA;AAAA"}
|
package/dist/ParentCache.mjs
CHANGED
|
@@ -18,9 +18,10 @@ import { createTemporarilyRetainedCacheItem } from "./CacheItem.mjs";
|
|
|
18
18
|
* be called
|
|
19
19
|
*/
|
|
20
20
|
var ParentCache = class {
|
|
21
|
-
constructor(factory) {
|
|
21
|
+
constructor(factory, options) {
|
|
22
22
|
this.__cacheItem = null;
|
|
23
23
|
this.__factory = factory;
|
|
24
|
+
this.__options = options;
|
|
24
25
|
}
|
|
25
26
|
/**
|
|
26
27
|
* This is called from useCachedResponsivePrecommitValue, when the parent cache is populated
|
|
@@ -41,7 +42,7 @@ var ParentCache = class {
|
|
|
41
42
|
__populateAndTemporaryRetain() {
|
|
42
43
|
const pair = createTemporarilyRetainedCacheItem(this.__factory, () => {
|
|
43
44
|
if (this.__cacheItem === pair[0]) this.empty();
|
|
44
|
-
});
|
|
45
|
+
}, this.__options);
|
|
45
46
|
const [cacheItem, disposeTemporaryRetain] = pair;
|
|
46
47
|
this.__cacheItem = cacheItem;
|
|
47
48
|
return [
|
package/dist/ParentCache.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ParentCache.mjs","names":[
|
|
1
|
+
{"version":3,"file":"ParentCache.mjs","names":[],"sources":["../src/ParentCache.ts"],"sourcesContent":["import type {\n CleanupFn,\n Factory,\n ItemCleanupPair,\n} from '@isograph/disposable-types';\nimport type { CacheItem, CacheItemOptions } from './CacheItem';\nimport { createTemporarilyRetainedCacheItem } from './CacheItem';\n\n// TODO convert cache impl to a getter and setter and free functions\n\n/**\n * ParentCache\n * - A ParentCache can be in two states: populated and unpopulated.\n * - A ParentCache holds a CacheItem, which can choose to remove itself from\n * the parent ParentCache.\n * - If the ParentCache is populated, the CacheItem (i.e. this.__value) must be\n * in the InParentCacheAndNotDisposed state, i.e. not disposed, so after we\n * null-check this.__value, this.__value.getValue(), this.__value.temporaryRetain()\n * and this.__value.permanentRetain() are safe to be called.\n *\n * - Though we do not do so, it is always safe to call parentCache.delete().\n *\n * Invariant:\n * - A parent cache at a given \"location\" (conceptually, an ID) should always\n * be called\n */\nexport class ParentCache<T> {\n private __cacheItem: CacheItem<T> | null = null;\n private readonly __factory: Factory<T>;\n private readonly __options: CacheItemOptions | void;\n\n // TODO pass an onEmpty function, which can e.g. remove this ParentCache\n // from some parent object.\n constructor(factory: Factory<T>, options?: CacheItemOptions) {\n this.__factory = factory;\n this.__options = options;\n }\n\n /**\n * This is called from useCachedResponsivePrecommitValue, when the parent cache is populated\n * and a previous temporary retain has been disposed. This can occur in scenarios like:\n * - temporary retain A is created by component B rendering\n * - temporary retain A expires, emptying the parent cache\n * - another component renders, sharing the same parent cache, filling\n * by calling getOrPopulateAndTemporaryRetain\n * - component B commits. We see that temporary retain A has been disposed,\n * and re-check the parent cache by calling this method.\n */\n getAndPermanentRetainIfPresent(): ItemCleanupPair<T> | null {\n return this.__cacheItem != null\n ? [this.__cacheItem.getValue(), this.__cacheItem.permanentRetain()]\n : null;\n }\n\n getOrPopulateAndTemporaryRetain(): [CacheItem<T>, T, CleanupFn] {\n return this.__cacheItem == null\n ? this.__populateAndTemporaryRetain()\n : temporaryRetain(this.__cacheItem);\n }\n\n private __populateAndTemporaryRetain(): [CacheItem<T>, T, CleanupFn] {\n const pair: ItemCleanupPair<CacheItem<T>> =\n createTemporarilyRetainedCacheItem(\n this.__factory,\n () => {\n // We are doing this check because we don't want to remove the cache item\n // if it is not the one that was created when the temporary retain was created.\n //\n // Consider the following scenario:\n // - we populate the cache with CacheItem A,\n // - then manually delete CacheItem A (e.g. to force a refetch)\n // - then, we re-populate the parent cache with CacheItem B\n // - then, the temporary retain of CacheItem A is disposed or expires.\n //\n // At this point, we don't want to delete CacheItem B from the cache.\n //\n // TODO consider what happens if items are === comparable to each other,\n // e.g. the item is a number!\n if (this.__cacheItem === pair[0]) {\n this.empty();\n }\n },\n this.__options,\n );\n\n // We deconstruct this here instead of at the definition site because otherwise,\n // typescript thinks that cacheItem is any, because it's referenced in the closure.\n const [cacheItem, disposeTemporaryRetain] = pair;\n this.__cacheItem = cacheItem;\n return [cacheItem, cacheItem.getValue(), disposeTemporaryRetain];\n }\n\n empty() {\n this.__cacheItem = null;\n }\n\n get factory(): Factory<T> {\n return this.__factory;\n }\n\n isEmpty(): boolean {\n return this.__cacheItem == null;\n }\n}\n\nfunction temporaryRetain<T>(value: CacheItem<T>): [CacheItem<T>, T, CleanupFn] {\n return [value, value.getValue(), value.temporaryRetain()];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA0BA,IAAa,cAAb,MAA4B;CAO1B,YAAY,SAAqB,SAA4B;qBANlB;AAOzC,OAAK,YAAY;AACjB,OAAK,YAAY;;;;;;;;;;;;CAanB,iCAA4D;AAC1D,SAAO,KAAK,eAAe,OACvB,CAAC,KAAK,YAAY,UAAU,EAAE,KAAK,YAAY,iBAAiB,CAAC,GACjE;;CAGN,kCAAgE;AAC9D,SAAO,KAAK,eAAe,OACvB,KAAK,8BAA8B,GACnC,gBAAgB,KAAK,YAAY;;CAGvC,AAAQ,+BAA6D;EACnE,MAAM,OACJ,mCACE,KAAK,iBACC;AAcJ,OAAI,KAAK,gBAAgB,KAAK,GAC5B,MAAK,OAAO;KAGhB,KAAK,UACN;EAIH,MAAM,CAAC,WAAW,0BAA0B;AAC5C,OAAK,cAAc;AACnB,SAAO;GAAC;GAAW,UAAU,UAAU;GAAE;GAAuB;;CAGlE,QAAQ;AACN,OAAK,cAAc;;CAGrB,IAAI,UAAsB;AACxB,SAAO,KAAK;;CAGd,UAAmB;AACjB,SAAO,KAAK,eAAe;;;AAI/B,SAAS,gBAAmB,OAAmD;AAC7E,QAAO;EAAC;EAAO,MAAM,UAAU;EAAE,MAAM,iBAAiB;EAAC"}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
const require_CacheItem = require('./CacheItem.
|
|
2
|
-
const require_ParentCache = require('./ParentCache.
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
1
|
+
const require_CacheItem = require('./CacheItem.cjs');
|
|
2
|
+
const require_ParentCache = require('./ParentCache.cjs');
|
|
3
|
+
const require_useEffectsRerunWithoutRender = require('./useEffectsRerunWithoutRender.cjs');
|
|
4
|
+
const require_useCachedResponsivePrecommitValue = require('./useCachedResponsivePrecommitValue.cjs');
|
|
5
|
+
const require_useHasCommittedRef = require('./useHasCommittedRef.cjs');
|
|
6
|
+
const require_useUpdatableDisposableState = require('./useUpdatableDisposableState.cjs');
|
|
7
|
+
const require_useDisposableState = require('./useDisposableState.cjs');
|
|
8
|
+
const require_useLazyDisposableState = require('./useLazyDisposableState.cjs');
|
|
9
|
+
const require_useUpdatableDisposableClearableState = require('./useUpdatableDisposableClearableState.cjs');
|
|
8
10
|
|
|
9
11
|
exports.CacheItem = require_CacheItem.CacheItem;
|
|
10
12
|
exports.ParentCache = require_ParentCache.ParentCache;
|
|
@@ -12,13 +14,15 @@ exports.UNASSIGNED_STATE = require_useUpdatableDisposableState.UNASSIGNED_STATE;
|
|
|
12
14
|
exports.createTemporarilyRetainedCacheItem = require_CacheItem.createTemporarilyRetainedCacheItem;
|
|
13
15
|
exports.useCachedResponsivePrecommitValue = require_useCachedResponsivePrecommitValue.useCachedResponsivePrecommitValue;
|
|
14
16
|
exports.useDisposableState = require_useDisposableState.useDisposableState;
|
|
17
|
+
exports.useEffectsRerunWithoutRenderRef = require_useEffectsRerunWithoutRender.useEffectsRerunWithoutRenderRef;
|
|
15
18
|
exports.useHasCommittedRef = require_useHasCommittedRef.useHasCommittedRef;
|
|
16
19
|
exports.useLazyDisposableState = require_useLazyDisposableState.useLazyDisposableState;
|
|
20
|
+
exports.useUpdatableDisposableClearableState = require_useUpdatableDisposableClearableState.useUpdatableDisposableClearableState;
|
|
17
21
|
exports.useUpdatableDisposableState = require_useUpdatableDisposableState.useUpdatableDisposableState;
|
|
18
|
-
var
|
|
19
|
-
Object.keys(
|
|
22
|
+
var _isograph_disposable_types = require("@isograph/disposable-types");
|
|
23
|
+
Object.keys(_isograph_disposable_types).forEach(function (k) {
|
|
20
24
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
21
25
|
enumerable: true,
|
|
22
|
-
get: function () { return
|
|
26
|
+
get: function () { return _isograph_disposable_types[k]; }
|
|
23
27
|
});
|
|
24
28
|
});
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { CacheItem, CacheItemOptions, CacheItemState, InParentCacheAndNotDisposed, NotInParentCacheAndDisposed, NotInParentCacheAndNotDisposed, createTemporarilyRetainedCacheItem } from "./CacheItem.
|
|
2
|
-
import { ParentCache } from "./ParentCache.
|
|
3
|
-
import { useCachedResponsivePrecommitValue } from "./useCachedResponsivePrecommitValue.
|
|
4
|
-
import { UNASSIGNED_STATE, UnassignedState, useUpdatableDisposableState } from "./useUpdatableDisposableState.
|
|
5
|
-
import { useDisposableState } from "./useDisposableState.
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
1
|
+
import { CacheItem, CacheItemOptions, CacheItemState, InParentCacheAndNotDisposed, NotInParentCacheAndDisposed, NotInParentCacheAndNotDisposed, createTemporarilyRetainedCacheItem } from "./CacheItem.cjs";
|
|
2
|
+
import { ParentCache } from "./ParentCache.cjs";
|
|
3
|
+
import { useCachedResponsivePrecommitValue } from "./useCachedResponsivePrecommitValue.cjs";
|
|
4
|
+
import { UNASSIGNED_STATE, UnassignedState, useUpdatableDisposableState } from "./useUpdatableDisposableState.cjs";
|
|
5
|
+
import { useDisposableState } from "./useDisposableState.cjs";
|
|
6
|
+
import { useEffectsRerunWithoutRenderRef } from "./useEffectsRerunWithoutRender.cjs";
|
|
7
|
+
import { useHasCommittedRef } from "./useHasCommittedRef.cjs";
|
|
8
|
+
import { useLazyDisposableState } from "./useLazyDisposableState.cjs";
|
|
9
|
+
import { useUpdatableDisposableClearableState } from "./useUpdatableDisposableClearableState.cjs";
|
|
8
10
|
export * from "@isograph/disposable-types";
|
|
9
|
-
export { CacheItem, CacheItemOptions, CacheItemState, InParentCacheAndNotDisposed, NotInParentCacheAndDisposed, NotInParentCacheAndNotDisposed, ParentCache, UNASSIGNED_STATE, UnassignedState, createTemporarilyRetainedCacheItem, useCachedResponsivePrecommitValue, useDisposableState, useHasCommittedRef, useLazyDisposableState, useUpdatableDisposableState };
|
|
11
|
+
export { CacheItem, CacheItemOptions, CacheItemState, InParentCacheAndNotDisposed, NotInParentCacheAndDisposed, NotInParentCacheAndNotDisposed, ParentCache, UNASSIGNED_STATE, UnassignedState, createTemporarilyRetainedCacheItem, useCachedResponsivePrecommitValue, useDisposableState, useEffectsRerunWithoutRenderRef, useHasCommittedRef, useLazyDisposableState, useUpdatableDisposableClearableState, useUpdatableDisposableState };
|
package/dist/index.d.mts
CHANGED
|
@@ -3,7 +3,9 @@ import { ParentCache } from "./ParentCache.mjs";
|
|
|
3
3
|
import { useCachedResponsivePrecommitValue } from "./useCachedResponsivePrecommitValue.mjs";
|
|
4
4
|
import { UNASSIGNED_STATE, UnassignedState, useUpdatableDisposableState } from "./useUpdatableDisposableState.mjs";
|
|
5
5
|
import { useDisposableState } from "./useDisposableState.mjs";
|
|
6
|
+
import { useEffectsRerunWithoutRenderRef } from "./useEffectsRerunWithoutRender.mjs";
|
|
6
7
|
import { useHasCommittedRef } from "./useHasCommittedRef.mjs";
|
|
7
8
|
import { useLazyDisposableState } from "./useLazyDisposableState.mjs";
|
|
9
|
+
import { useUpdatableDisposableClearableState } from "./useUpdatableDisposableClearableState.mjs";
|
|
8
10
|
export * from "@isograph/disposable-types";
|
|
9
|
-
export { CacheItem, CacheItemOptions, CacheItemState, InParentCacheAndNotDisposed, NotInParentCacheAndDisposed, NotInParentCacheAndNotDisposed, ParentCache, UNASSIGNED_STATE, UnassignedState, createTemporarilyRetainedCacheItem, useCachedResponsivePrecommitValue, useDisposableState, useHasCommittedRef, useLazyDisposableState, useUpdatableDisposableState };
|
|
11
|
+
export { CacheItem, CacheItemOptions, CacheItemState, InParentCacheAndNotDisposed, NotInParentCacheAndDisposed, NotInParentCacheAndNotDisposed, ParentCache, UNASSIGNED_STATE, UnassignedState, createTemporarilyRetainedCacheItem, useCachedResponsivePrecommitValue, useDisposableState, useEffectsRerunWithoutRenderRef, useHasCommittedRef, useLazyDisposableState, useUpdatableDisposableClearableState, useUpdatableDisposableState };
|
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { CacheItem, createTemporarilyRetainedCacheItem } from "./CacheItem.mjs";
|
|
2
2
|
import { ParentCache } from "./ParentCache.mjs";
|
|
3
|
+
import { useEffectsRerunWithoutRenderRef } from "./useEffectsRerunWithoutRender.mjs";
|
|
3
4
|
import { useCachedResponsivePrecommitValue } from "./useCachedResponsivePrecommitValue.mjs";
|
|
4
5
|
import { useHasCommittedRef } from "./useHasCommittedRef.mjs";
|
|
5
6
|
import { UNASSIGNED_STATE, useUpdatableDisposableState } from "./useUpdatableDisposableState.mjs";
|
|
6
7
|
import { useDisposableState } from "./useDisposableState.mjs";
|
|
7
8
|
import { useLazyDisposableState } from "./useLazyDisposableState.mjs";
|
|
9
|
+
import { useUpdatableDisposableClearableState } from "./useUpdatableDisposableClearableState.mjs";
|
|
8
10
|
|
|
9
11
|
export * from "@isograph/disposable-types"
|
|
10
12
|
|
|
11
|
-
export { CacheItem, ParentCache, UNASSIGNED_STATE, createTemporarilyRetainedCacheItem, useCachedResponsivePrecommitValue, useDisposableState, useHasCommittedRef, useLazyDisposableState, useUpdatableDisposableState };
|
|
13
|
+
export { CacheItem, ParentCache, UNASSIGNED_STATE, createTemporarilyRetainedCacheItem, useCachedResponsivePrecommitValue, useDisposableState, useEffectsRerunWithoutRenderRef, useHasCommittedRef, useLazyDisposableState, useUpdatableDisposableClearableState, useUpdatableDisposableState };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const require_useEffectsRerunWithoutRender = require('./useEffectsRerunWithoutRender.cjs');
|
|
2
4
|
let react = require("react");
|
|
3
|
-
react = require_rolldown_runtime.__toESM(react);
|
|
4
5
|
|
|
5
6
|
//#region src/useCachedResponsivePrecommitValue.ts
|
|
6
7
|
/**
|
|
@@ -38,16 +39,21 @@ react = require_rolldown_runtime.__toESM(react);
|
|
|
38
39
|
function useCachedResponsivePrecommitValue(parentCache, onCommit) {
|
|
39
40
|
const [, rerender] = (0, react.useState)(null);
|
|
40
41
|
const lastCommittedParentCache = (0, react.useRef)(null);
|
|
42
|
+
const effectsRerunWithoutRenderRef = require_useEffectsRerunWithoutRender.useEffectsRerunWithoutRenderRef();
|
|
41
43
|
(0, react.useEffect)(() => {
|
|
42
44
|
lastCommittedParentCache.current = parentCache;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
rerender({});
|
|
45
|
+
if (!effectsRerunWithoutRenderRef.current) {
|
|
46
|
+
const undisposedPair = cacheItem.permanentRetainIfNotDisposed(disposeOfTemporaryRetain);
|
|
47
|
+
if (undisposedPair != null) {
|
|
48
|
+
onCommit(undisposedPair);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
50
51
|
}
|
|
52
|
+
effectsRerunWithoutRenderRef.current = false;
|
|
53
|
+
const existingCacheItemCleanupPair = parentCache.getAndPermanentRetainIfPresent();
|
|
54
|
+
if (existingCacheItemCleanupPair != null) onCommit(existingCacheItemCleanupPair);
|
|
55
|
+
else onCommit(parentCache.factory());
|
|
56
|
+
rerender({});
|
|
51
57
|
}, [parentCache]);
|
|
52
58
|
if (lastCommittedParentCache.current === parentCache) return null;
|
|
53
59
|
const [cacheItem, item, disposeOfTemporaryRetain] = parentCache.getOrPopulateAndTemporaryRetain();
|
package/dist/{useCachedResponsivePrecommitValue.d.ts → useCachedResponsivePrecommitValue.d.cts}
RENAMED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { ParentCache } from "./ParentCache.
|
|
1
|
+
import { ParentCache } from "./ParentCache.cjs";
|
|
2
2
|
import { ItemCleanupPair } from "@isograph/disposable-types";
|
|
3
3
|
|
|
4
4
|
//#region src/useCachedResponsivePrecommitValue.d.ts
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
6
|
* useCachedResponsivePrecommitValue<T>
|
|
8
7
|
* - Takes a mutable parent cache, a factory function, and an onCommit callback.
|
|
@@ -40,4 +39,4 @@ declare function useCachedResponsivePrecommitValue<T>(parentCache: ParentCache<T
|
|
|
40
39
|
} | null;
|
|
41
40
|
//#endregion
|
|
42
41
|
export { useCachedResponsivePrecommitValue };
|
|
43
|
-
//# sourceMappingURL=useCachedResponsivePrecommitValue.d.
|
|
42
|
+
//# sourceMappingURL=useCachedResponsivePrecommitValue.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCachedResponsivePrecommitValue.d.cts","names":[],"sources":["../src/useCachedResponsivePrecommitValue.ts"],"mappings":";;;;;;AAuCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAgB,iCAAA,GAAA,CACd,WAAA,EAAa,WAAA,CAAY,CAAA,GACzB,QAAA,GAAW,IAAA,EAAM,eAAA,CAAgB,CAAA;EAC9B,KAAA,EAAO,CAAA;AAAA"}
|
|
@@ -2,7 +2,6 @@ import { ParentCache } from "./ParentCache.mjs";
|
|
|
2
2
|
import { ItemCleanupPair } from "@isograph/disposable-types";
|
|
3
3
|
|
|
4
4
|
//#region src/useCachedResponsivePrecommitValue.d.ts
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
6
|
* useCachedResponsivePrecommitValue<T>
|
|
8
7
|
* - Takes a mutable parent cache, a factory function, and an onCommit callback.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useCachedResponsivePrecommitValue.d.mts","names":[],"sources":["../src/useCachedResponsivePrecommitValue.ts"],"
|
|
1
|
+
{"version":3,"file":"useCachedResponsivePrecommitValue.d.mts","names":[],"sources":["../src/useCachedResponsivePrecommitValue.ts"],"mappings":";;;;;;AAuCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAgB,iCAAA,GAAA,CACd,WAAA,EAAa,WAAA,CAAY,CAAA,GACzB,QAAA,GAAW,IAAA,EAAM,eAAA,CAAgB,CAAA;EAC9B,KAAA,EAAO,CAAA;AAAA"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { useEffectsRerunWithoutRenderRef } from "./useEffectsRerunWithoutRender.mjs";
|
|
1
2
|
import { useEffect, useRef, useState } from "react";
|
|
2
3
|
|
|
3
4
|
//#region src/useCachedResponsivePrecommitValue.ts
|
|
@@ -36,16 +37,21 @@ import { useEffect, useRef, useState } from "react";
|
|
|
36
37
|
function useCachedResponsivePrecommitValue(parentCache, onCommit) {
|
|
37
38
|
const [, rerender] = useState(null);
|
|
38
39
|
const lastCommittedParentCache = useRef(null);
|
|
40
|
+
const effectsRerunWithoutRenderRef = useEffectsRerunWithoutRenderRef();
|
|
39
41
|
useEffect(() => {
|
|
40
42
|
lastCommittedParentCache.current = parentCache;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
rerender({});
|
|
43
|
+
if (!effectsRerunWithoutRenderRef.current) {
|
|
44
|
+
const undisposedPair = cacheItem.permanentRetainIfNotDisposed(disposeOfTemporaryRetain);
|
|
45
|
+
if (undisposedPair != null) {
|
|
46
|
+
onCommit(undisposedPair);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
48
49
|
}
|
|
50
|
+
effectsRerunWithoutRenderRef.current = false;
|
|
51
|
+
const existingCacheItemCleanupPair = parentCache.getAndPermanentRetainIfPresent();
|
|
52
|
+
if (existingCacheItemCleanupPair != null) onCommit(existingCacheItemCleanupPair);
|
|
53
|
+
else onCommit(parentCache.factory());
|
|
54
|
+
rerender({});
|
|
49
55
|
}, [parentCache]);
|
|
50
56
|
if (lastCommittedParentCache.current === parentCache) return null;
|
|
51
57
|
const [cacheItem, item, disposeOfTemporaryRetain] = parentCache.getOrPopulateAndTemporaryRetain();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useCachedResponsivePrecommitValue.mjs","names":[],"sources":["../src/useCachedResponsivePrecommitValue.ts"],"sourcesContent":["'use strict';\n\nimport type { ItemCleanupPair } from '@isograph/disposable-types';\nimport { useEffect, useRef, useState } from 'react';\nimport type { ParentCache } from './ParentCache';\n\n/**\n * useCachedResponsivePrecommitValue<T>\n * - Takes a mutable parent cache, a factory function, and an onCommit callback.\n * - Returns T before commit after every parent cache change, and null afterward.\n * - Calls onCommit with the ItemCleanupPair during commit after every parent cache change.\n * - The T from the render phase is only temporarily retained. It may have been\n * disposed by the time of the commit. If so, this hook checks the parent cache\n * for another T or creates one, and passes this T to onCommit.\n * - If the T returned during the last render is not the same as the one that\n * is passed to onCommit, during the commit phase, it will schedule another render.\n *\n * Invariant: the returned T has not been disposed during the tick of the render.\n * The T passed to the onCommit callback has not been disposed when the onCommit\n * callback is called.\n *\n * Passing a different parentCache:\n * - Pre-commit, passing a different parentCache has the effect of \"resetting\" this\n * hook's state to the new cache's state. For example, if you have a cache associated\n * with a set of variables (e.g. {name: \"Matthew\"}), and pass in another cache\n * (e.g. associated with {name: \"James\"}), which is empty, the hook will fill that\n * new cache with the factory function.\n * - Post-commit, passing a different parentCache will reset hook to the pre-commit\n * state. The cache will return T before commit, then fill the new cache with the\n * factory function and return null afterwards.\n *\n * Passing a different factory:\n * - Passing a different factory has no effect, except when factory is called,\n * which is when the parent cache is being filled, or during commit.\n *\n * Passing a different onCommit:\n * - Passing a different onCommit has no effect, except for during commit.\n */\nexport function useCachedResponsivePrecommitValue<T>(\n parentCache: ParentCache<T>,\n onCommit: (pair: ItemCleanupPair<T>) => void,\n): { state: T } | null {\n // TODO: there should be two APIs. One in which we always re-render if the\n // committed item was not returned during the last render, and one in which\n // we do not. The latter is useful for cases where every disposable item\n // behaves identically, but must be loaded.\n //\n // This hook is the former, i.e. re-renders if the committed item has changed.\n const [, rerender] = useState<{} | null>(null);\n const lastCommittedParentCache = useRef<ParentCache<T> | null>(null);\n\n useEffect(() => {\n lastCommittedParentCache.current = parentCache;\n // On commit, cacheItem may be disposed, because during the render phase,\n // we only temporarily retained the item, and the temporary retain could have\n // expired by the time of the commit.\n //\n // So, we can be in one of two states:\n // - the item is not disposed. In that case, permanently retain and use that item.\n // - the item is disposed. In that case, we can be in two states:\n // - the parent cache is not empty (due to another component rendering, or\n // another render of the same component.) In that case, permanently retain and\n // use the item from the parent cache. (Note: any item present in the parent\n // cache is not disposed.)\n // - the parent cache is empty. In that case, call factory, getting a new item\n // and a cleanup function.\n //\n // After the above, we have a non-disposed item and a cleanup function, which we\n // can pass to onCommit.\n const undisposedPair = cacheItem.permanentRetainIfNotDisposed(\n
|
|
1
|
+
{"version":3,"file":"useCachedResponsivePrecommitValue.mjs","names":[],"sources":["../src/useCachedResponsivePrecommitValue.ts"],"sourcesContent":["'use strict';\n\nimport type { ItemCleanupPair } from '@isograph/disposable-types';\nimport { useEffect, useRef, useState } from 'react';\nimport type { ParentCache } from './ParentCache';\nimport { useEffectsRerunWithoutRenderRef } from './useEffectsRerunWithoutRender';\n\n/**\n * useCachedResponsivePrecommitValue<T>\n * - Takes a mutable parent cache, a factory function, and an onCommit callback.\n * - Returns T before commit after every parent cache change, and null afterward.\n * - Calls onCommit with the ItemCleanupPair during commit after every parent cache change.\n * - The T from the render phase is only temporarily retained. It may have been\n * disposed by the time of the commit. If so, this hook checks the parent cache\n * for another T or creates one, and passes this T to onCommit.\n * - If the T returned during the last render is not the same as the one that\n * is passed to onCommit, during the commit phase, it will schedule another render.\n *\n * Invariant: the returned T has not been disposed during the tick of the render.\n * The T passed to the onCommit callback has not been disposed when the onCommit\n * callback is called.\n *\n * Passing a different parentCache:\n * - Pre-commit, passing a different parentCache has the effect of \"resetting\" this\n * hook's state to the new cache's state. For example, if you have a cache associated\n * with a set of variables (e.g. {name: \"Matthew\"}), and pass in another cache\n * (e.g. associated with {name: \"James\"}), which is empty, the hook will fill that\n * new cache with the factory function.\n * - Post-commit, passing a different parentCache will reset hook to the pre-commit\n * state. The cache will return T before commit, then fill the new cache with the\n * factory function and return null afterwards.\n *\n * Passing a different factory:\n * - Passing a different factory has no effect, except when factory is called,\n * which is when the parent cache is being filled, or during commit.\n *\n * Passing a different onCommit:\n * - Passing a different onCommit has no effect, except for during commit.\n */\nexport function useCachedResponsivePrecommitValue<T>(\n parentCache: ParentCache<T>,\n onCommit: (pair: ItemCleanupPair<T>) => void,\n): { state: T } | null {\n // TODO: there should be two APIs. One in which we always re-render if the\n // committed item was not returned during the last render, and one in which\n // we do not. The latter is useful for cases where every disposable item\n // behaves identically, but must be loaded.\n //\n // This hook is the former, i.e. re-renders if the committed item has changed.\n const [, rerender] = useState<{} | null>(null);\n const lastCommittedParentCache = useRef<ParentCache<T> | null>(null);\n\n const effectsRerunWithoutRenderRef = useEffectsRerunWithoutRenderRef();\n\n useEffect(() => {\n lastCommittedParentCache.current = parentCache;\n // On commit, cacheItem may be disposed, because during the render phase,\n // we only temporarily retained the item, and the temporary retain could have\n // expired by the time of the commit.\n //\n // So, we can be in one of two states:\n // - the item is not disposed. In that case, permanently retain and use that item.\n // - the item is disposed. In that case, we can be in two states:\n // - the parent cache is not empty (due to another component rendering, or\n // another render of the same component.) In that case, permanently retain and\n // use the item from the parent cache. (Note: any item present in the parent\n // cache is not disposed.)\n // - the parent cache is empty. In that case, call factory, getting a new item\n // and a cleanup function.\n //\n // After the above, we have a non-disposed item and a cleanup function, which we\n // can pass to onCommit.\n //\n // When this effect runs again without a render (StrictMode's simulated\n // remount, Fast Refresh, Activity), the render's temporary retain was\n // already converted by the first run and released by the cleanup, so the\n // item is re-acquired from the parent cache below instead.\n if (!effectsRerunWithoutRenderRef.current) {\n const undisposedPair = cacheItem.permanentRetainIfNotDisposed(\n disposeOfTemporaryRetain,\n );\n if (undisposedPair != null) {\n onCommit(undisposedPair);\n return;\n }\n }\n effectsRerunWithoutRenderRef.current = false;\n\n // The cache item we created during render has been disposed. Check if the parent\n // cache is populated.\n const existingCacheItemCleanupPair =\n parentCache.getAndPermanentRetainIfPresent();\n if (existingCacheItemCleanupPair != null) {\n onCommit(existingCacheItemCleanupPair);\n } else {\n // We did not find an item in the parent cache, create a new one.\n onCommit(parentCache.factory());\n }\n // TODO: Consider whether we always want to rerender if the committed item\n // was not returned during the last render, or whether some callers will\n // prefer opting out of this behavior (e.g. if every disposable item behaves\n // identically, but must be loaded.)\n rerender({});\n }, [parentCache]);\n\n if (lastCommittedParentCache.current === parentCache) {\n return null;\n }\n\n // Safety: item is only safe to use (i.e. guaranteed not to have been disposed)\n // during this tick.\n const [cacheItem, item, disposeOfTemporaryRetain] =\n parentCache.getOrPopulateAndTemporaryRetain();\n\n return { state: item };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,SAAgB,kCACd,aACA,UACqB;CAOrB,MAAM,GAAG,YAAY,SAAoB,KAAK;CAC9C,MAAM,2BAA2B,OAA8B,KAAK;CAEpE,MAAM,+BAA+B,iCAAiC;AAEtE,iBAAgB;AACd,2BAAyB,UAAU;AAsBnC,MAAI,CAAC,6BAA6B,SAAS;GACzC,MAAM,iBAAiB,UAAU,6BAC/B,yBACD;AACD,OAAI,kBAAkB,MAAM;AAC1B,aAAS,eAAe;AACxB;;;AAGJ,+BAA6B,UAAU;EAIvC,MAAM,+BACJ,YAAY,gCAAgC;AAC9C,MAAI,gCAAgC,KAClC,UAAS,6BAA6B;MAGtC,UAAS,YAAY,SAAS,CAAC;AAMjC,WAAS,EAAE,CAAC;IACX,CAAC,YAAY,CAAC;AAEjB,KAAI,yBAAyB,YAAY,YACvC,QAAO;CAKT,MAAM,CAAC,WAAW,MAAM,4BACtB,YAAY,iCAAiC;AAE/C,QAAO,EAAE,OAAO,MAAM"}
|