@isograph/react-disposable-state 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CacheItem.d.ts +1 -0
- package/dist/CacheItem.d.ts.map +1 -0
- package/dist/CacheItem.js +2 -2
- package/dist/ParentCache.d.ts +2 -1
- package/dist/ParentCache.d.ts.map +1 -0
- package/dist/ParentCache.js +3 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -1
- package/dist/{useCachedPrecommitValue.d.ts → useCachedResponsivePrecommitValue.d.ts} +12 -9
- package/dist/useCachedResponsivePrecommitValue.d.ts.map +1 -0
- package/dist/{useCachedPrecommitValue.js → useCachedResponsivePrecommitValue.js} +17 -16
- package/dist/useDisposableState.d.ts +2 -1
- package/dist/useDisposableState.d.ts.map +1 -0
- package/dist/useDisposableState.js +7 -6
- package/dist/useHasCommittedRef.d.ts +1 -0
- package/dist/useHasCommittedRef.d.ts.map +1 -0
- package/dist/useHasCommittedRef.js +1 -2
- package/dist/useLazyDisposableState.d.ts +4 -2
- package/dist/useLazyDisposableState.d.ts.map +1 -0
- package/dist/useLazyDisposableState.js +15 -12
- package/dist/useUpdatableDisposableState.d.ts +1 -0
- package/dist/useUpdatableDisposableState.d.ts.map +1 -0
- package/dist/useUpdatableDisposableState.js +2 -2
- package/package.json +11 -8
- package/src/ParentCache.ts +3 -1
- package/src/index.ts +1 -1
- package/src/useCachedResponsivePrecommitValue.test.tsx +571 -0
- package/src/{useCachedPrecommitValue.ts → useCachedResponsivePrecommitValue.ts} +18 -17
- package/src/useDisposableState.ts +12 -7
- package/src/useLazyDisposableState.test.tsx +70 -0
- package/src/useLazyDisposableState.ts +27 -15
- package/src/useUpdatableDisposableState.ts +1 -1
- package/tsconfig.json +6 -0
- package/src/useCachedPrecommitValue.test.tsx +0 -577
package/dist/CacheItem.d.ts
CHANGED
@@ -56,3 +56,4 @@ export declare class CacheItem<T> {
|
|
56
56
|
private __maybeExitNotInParentCacheAndNotDisposedState;
|
57
57
|
}
|
58
58
|
export declare function createTemporarilyRetainedCacheItem<T>(factory: Factory<T>, removeFromParentCache: CleanupFn, options: CacheItemOptions | void): [CacheItem<T>, CleanupFn];
|
59
|
+
//# sourceMappingURL=CacheItem.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"CacheItem.d.ts","sourceRoot":"","sources":["../src/CacheItem.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,OAAO,EACP,eAAe,EAChB,MAAM,4BAA4B,CAAC;AAIpC,MAAM,MAAM,2BAA2B,GAAG;IACxC,IAAI,EAAE,6BAA6B,CAAC;CACrC,CAAC;AACF,MAAM,MAAM,8BAA8B,CAAC,CAAC,IAAI;IAC9C,IAAI,EAAE,gCAAgC,CAAC;IACvC,KAAK,EAAE,CAAC,CAAC;IACT,YAAY,EAAE,MAAM,IAAI,CAAC;IAGzB,oBAAoB,EAAE,MAAM,CAAC;CAC9B,CAAC;AACF,MAAM,MAAM,2BAA2B,CAAC,CAAC,IAAI;IAC3C,IAAI,EAAE,6BAA6B,CAAC;IACpC,KAAK,EAAE,CAAC,CAAC;IACT,YAAY,EAAE,MAAM,IAAI,CAAC;IACzB,qBAAqB,EAAE,MAAM,IAAI,CAAC;IAGlC,oBAAoB,EAAE,MAAM,CAAC;IAG7B,oBAAoB,EAAE,MAAM,CAAC;CAC9B,CAAC;AAEF,MAAM,MAAM,cAAc,CAAC,CAAC,IACxB,2BAA2B,CAAC,CAAC,CAAC,GAC9B,8BAA8B,CAAC,CAAC,CAAC,GACjC,2BAA2B,CAAC;AAEhC,MAAM,MAAM,gBAAgB,GAAG;IAC7B,mBAAmB,EAAE,MAAM,CAAC;CAC7B,CAAC;AAKF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,SAAS,CAAC,CAAC;IACtB,OAAO,CAAC,OAAO,CAAoB;IACnC,OAAO,CAAC,SAAS,CAA0B;gBAQzC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EACnB,qBAAqB,EAAE,SAAS,EAChC,OAAO,EAAE,gBAAgB,GAAG,IAAI;IAgBlC,QAAQ,IAAI,CAAC;IAiBb,4BAA4B,CAC1B,wBAAwB,EAAE,SAAS,GAClC,eAAe,CAAC,CAAC,CAAC,GAAG,IAAI;IA8E5B,eAAe,IAAI,SAAS;IAkE5B,eAAe,IAAI,SAAS;IAoE5B,OAAO,CAAC,2CAA2C;IAoBnD,OAAO,CAAC,8CAA8C;CAUvD;AAED,wBAAgB,kCAAkC,CAAC,CAAC,EAClD,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EACnB,qBAAqB,EAAE,SAAS,EAChC,OAAO,EAAE,gBAAgB,GAAG,IAAI,GAC/B,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,CAAC,CAI3B"}
|
package/dist/CacheItem.js
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.CacheItem = void 0;
|
4
|
+
exports.createTemporarilyRetainedCacheItem = createTemporarilyRetainedCacheItem;
|
4
5
|
const DEFAULT_TEMPORARY_RETAIN_TIME = 5000;
|
5
6
|
// TODO don't export this class, only export type (interface) instead
|
6
7
|
// TODO convert cacheitem impl to a getter and setter and free functions
|
@@ -266,4 +267,3 @@ function createTemporarilyRetainedCacheItem(factory, removeFromParentCache, opti
|
|
266
267
|
const disposeTemporaryRetain = cacheItem.temporaryRetain();
|
267
268
|
return [cacheItem, disposeTemporaryRetain];
|
268
269
|
}
|
269
|
-
exports.createTemporarilyRetainedCacheItem = createTemporarilyRetainedCacheItem;
|
package/dist/ParentCache.d.ts
CHANGED
@@ -21,7 +21,7 @@ export declare class ParentCache<T> {
|
|
21
21
|
private readonly __factory;
|
22
22
|
constructor(factory: Factory<T>);
|
23
23
|
/**
|
24
|
-
* This is called from
|
24
|
+
* This is called from useCachedResponsivePrecommitValue, when the parent cache is populated
|
25
25
|
* and a previous temporary retain has been disposed. This can occur in scenarios like:
|
26
26
|
* - temporary retain A is created by component B rendering
|
27
27
|
* - temporary retain A expires, emptying the parent cache
|
@@ -37,3 +37,4 @@ export declare class ParentCache<T> {
|
|
37
37
|
get factory(): Factory<T>;
|
38
38
|
isEmpty(): boolean;
|
39
39
|
}
|
40
|
+
//# sourceMappingURL=ParentCache.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"ParentCache.d.ts","sourceRoot":"","sources":["../src/ParentCache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAsC,MAAM,aAAa,CAAC;AAC5E,OAAO,EACL,SAAS,EACT,OAAO,EACP,eAAe,EAChB,MAAM,4BAA4B,CAAC;AAKpC;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,WAAW,CAAC,CAAC;IACxB,OAAO,CAAC,WAAW,CAA6B;IAChD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAa;gBAI3B,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;IAI/B;;;;;;;;;OASG;IACH,8BAA8B,IAAI,eAAe,CAAC,CAAC,CAAC,GAAG,IAAI;IAM3D,+BAA+B,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,SAAS,CAAC;IAM/D,OAAO,CAAC,4BAA4B;IA4BpC,KAAK;IAIL,IAAI,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC,CAExB;IAED,OAAO,IAAI,OAAO;CAGnB"}
|
package/dist/ParentCache.js
CHANGED
@@ -21,12 +21,14 @@ const CacheItem_1 = require("./CacheItem");
|
|
21
21
|
* be called
|
22
22
|
*/
|
23
23
|
class ParentCache {
|
24
|
+
// TODO pass an onEmpty function, which can e.g. remove this ParentCache
|
25
|
+
// from some parent object.
|
24
26
|
constructor(factory) {
|
25
27
|
this.__cacheItem = null;
|
26
28
|
this.__factory = factory;
|
27
29
|
}
|
28
30
|
/**
|
29
|
-
* This is called from
|
31
|
+
* This is called from useCachedResponsivePrecommitValue, when the parent cache is populated
|
30
32
|
* and a previous temporary retain has been disposed. This can occur in scenarios like:
|
31
33
|
* - temporary retain A is created by component B rendering
|
32
34
|
* - temporary retain A expires, emptying the parent cache
|
package/dist/index.d.ts
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
export * from '@isograph/disposable-types';
|
2
2
|
export * from './CacheItem';
|
3
3
|
export * from './ParentCache';
|
4
|
-
export * from './
|
4
|
+
export * from './useCachedResponsivePrecommitValue';
|
5
5
|
export * from './useDisposableState';
|
6
6
|
export * from './useHasCommittedRef';
|
7
7
|
export * from './useLazyDisposableState';
|
8
8
|
export * from './useUpdatableDisposableState';
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAE3C,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,cAAc,qCAAqC,CAAC;AACpD,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,0BAA0B,CAAC;AACzC,cAAc,+BAA+B,CAAC"}
|
package/dist/index.js
CHANGED
@@ -17,7 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("@isograph/disposable-types"), exports);
|
18
18
|
__exportStar(require("./CacheItem"), exports);
|
19
19
|
__exportStar(require("./ParentCache"), exports);
|
20
|
-
__exportStar(require("./
|
20
|
+
__exportStar(require("./useCachedResponsivePrecommitValue"), exports);
|
21
21
|
__exportStar(require("./useDisposableState"), exports);
|
22
22
|
__exportStar(require("./useHasCommittedRef"), exports);
|
23
23
|
__exportStar(require("./useLazyDisposableState"), exports);
|
@@ -1,15 +1,15 @@
|
|
1
|
+
import { ItemCleanupPair } from '@isograph/disposable-types';
|
1
2
|
import { ParentCache } from './ParentCache';
|
2
|
-
import { ItemCleanupPair } from '@isograph/isograph-disposable-types/dist';
|
3
3
|
/**
|
4
|
-
*
|
4
|
+
* useCachedResponsivePrecommitValue<T>
|
5
5
|
* - Takes a mutable parent cache, a factory function, and an onCommit callback.
|
6
|
-
* - Returns T before
|
7
|
-
* - Calls onCommit with the ItemCleanupPair during
|
6
|
+
* - Returns T before commit after every parent cache change, and null afterward.
|
7
|
+
* - Calls onCommit with the ItemCleanupPair during commit after every parent cache change.
|
8
8
|
* - The T from the render phase is only temporarily retained. It may have been
|
9
9
|
* disposed by the time of the commit. If so, this hook checks the parent cache
|
10
10
|
* for another T or creates one, and passes this T to onCommit.
|
11
11
|
* - If the T returned during the last render is not the same as the one that
|
12
|
-
* is passed to onCommit, during the commit phase, will schedule another render.
|
12
|
+
* is passed to onCommit, during the commit phase, it will schedule another render.
|
13
13
|
*
|
14
14
|
* Invariant: the returned T has not been disposed during the tick of the render.
|
15
15
|
* The T passed to the onCommit callback has not been disposed when the onCommit
|
@@ -21,16 +21,19 @@ import { ItemCleanupPair } from '@isograph/isograph-disposable-types/dist';
|
|
21
21
|
* with a set of variables (e.g. {name: "Matthew"}), and pass in another cache
|
22
22
|
* (e.g. associated with {name: "James"}), which is empty, the hook will fill that
|
23
23
|
* new cache with the factory function.
|
24
|
+
* - Post-commit, passing a different parentCache will reset hook to the pre-commit
|
25
|
+
* state. The cache will return T before commit, then fill the new cache with the
|
26
|
+
* factory function and return null afterwards.
|
24
27
|
*
|
25
28
|
* Passing a different factory:
|
26
29
|
* - Passing a different factory has no effect, except when factory is called,
|
27
|
-
* which is when the parent cache is being filled, or during
|
30
|
+
* which is when the parent cache is being filled, or during commit.
|
28
31
|
*
|
29
32
|
* Passing a different onCommit:
|
30
|
-
* - Passing a different onCommit has no effect, except for during
|
33
|
+
* - Passing a different onCommit has no effect, except for during commit.
|
31
34
|
*
|
32
|
-
* Post-commit, all parameters are ignored and the hook returns null.
|
33
35
|
*/
|
34
|
-
export declare function
|
36
|
+
export declare function useCachedResponsivePrecommitValue<T>(parentCache: ParentCache<T>, onCommit: (pair: ItemCleanupPair<T>) => void): {
|
35
37
|
state: T;
|
36
38
|
} | null;
|
39
|
+
//# sourceMappingURL=useCachedResponsivePrecommitValue.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useCachedResponsivePrecommitValue.d.ts","sourceRoot":"","sources":["../src/useCachedResponsivePrecommitValue.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAE7D,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,iCAAiC,CAAC,CAAC,EACjD,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,EAC3B,QAAQ,EAAE,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC,CAAC,KAAK,IAAI,GAC3C;IAAE,KAAK,EAAE,CAAC,CAAA;CAAE,GAAG,IAAI,CA8DrB"}
|
@@ -1,18 +1,17 @@
|
|
1
1
|
'use strict';
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.useCachedResponsivePrecommitValue = useCachedResponsivePrecommitValue;
|
4
4
|
const react_1 = require("react");
|
5
|
-
const useHasCommittedRef_1 = require("./useHasCommittedRef");
|
6
5
|
/**
|
7
|
-
*
|
6
|
+
* useCachedResponsivePrecommitValue<T>
|
8
7
|
* - Takes a mutable parent cache, a factory function, and an onCommit callback.
|
9
|
-
* - Returns T before
|
10
|
-
* - Calls onCommit with the ItemCleanupPair during
|
8
|
+
* - Returns T before commit after every parent cache change, and null afterward.
|
9
|
+
* - Calls onCommit with the ItemCleanupPair during commit after every parent cache change.
|
11
10
|
* - The T from the render phase is only temporarily retained. It may have been
|
12
11
|
* disposed by the time of the commit. If so, this hook checks the parent cache
|
13
12
|
* for another T or creates one, and passes this T to onCommit.
|
14
13
|
* - If the T returned during the last render is not the same as the one that
|
15
|
-
* is passed to onCommit, during the commit phase, will schedule another render.
|
14
|
+
* is passed to onCommit, during the commit phase, it will schedule another render.
|
16
15
|
*
|
17
16
|
* Invariant: the returned T has not been disposed during the tick of the render.
|
18
17
|
* The T passed to the onCommit callback has not been disposed when the onCommit
|
@@ -24,17 +23,19 @@ const useHasCommittedRef_1 = require("./useHasCommittedRef");
|
|
24
23
|
* with a set of variables (e.g. {name: "Matthew"}), and pass in another cache
|
25
24
|
* (e.g. associated with {name: "James"}), which is empty, the hook will fill that
|
26
25
|
* new cache with the factory function.
|
26
|
+
* - Post-commit, passing a different parentCache will reset hook to the pre-commit
|
27
|
+
* state. The cache will return T before commit, then fill the new cache with the
|
28
|
+
* factory function and return null afterwards.
|
27
29
|
*
|
28
30
|
* Passing a different factory:
|
29
31
|
* - Passing a different factory has no effect, except when factory is called,
|
30
|
-
* which is when the parent cache is being filled, or during
|
32
|
+
* which is when the parent cache is being filled, or during commit.
|
31
33
|
*
|
32
34
|
* Passing a different onCommit:
|
33
|
-
* - Passing a different onCommit has no effect, except for during
|
35
|
+
* - Passing a different onCommit has no effect, except for during commit.
|
34
36
|
*
|
35
|
-
* Post-commit, all parameters are ignored and the hook returns null.
|
36
37
|
*/
|
37
|
-
function
|
38
|
+
function useCachedResponsivePrecommitValue(parentCache, onCommit) {
|
38
39
|
// TODO: there should be two APIs. One in which we always re-render if the
|
39
40
|
// committed item was not returned during the last render, and one in which
|
40
41
|
// we do not. The latter is useful for cases where every disposable item
|
@@ -42,8 +43,10 @@ function useCachedPrecommitValue(parentCache, onCommit) {
|
|
42
43
|
//
|
43
44
|
// This hook is the former, i.e. re-renders if the committed item has changed.
|
44
45
|
const [, rerender] = (0, react_1.useState)(null);
|
46
|
+
const lastCommittedParentCache = (0, react_1.useRef)(null);
|
45
47
|
(0, react_1.useEffect)(() => {
|
46
|
-
|
48
|
+
lastCommittedParentCache.current = parentCache;
|
49
|
+
// On commit, cacheItem may be disposed, because during the render phase,
|
47
50
|
// we only temporarily retained the item, and the temporary retain could have
|
48
51
|
// expired by the time of the commit.
|
49
52
|
//
|
@@ -80,14 +83,12 @@ function useCachedPrecommitValue(parentCache, onCommit) {
|
|
80
83
|
// identically, but must be loaded.)
|
81
84
|
rerender({});
|
82
85
|
}
|
83
|
-
}, []);
|
84
|
-
|
85
|
-
if (hasCommittedRef.current) {
|
86
|
+
}, [parentCache]);
|
87
|
+
if (lastCommittedParentCache.current === parentCache) {
|
86
88
|
return null;
|
87
89
|
}
|
88
|
-
// Safety: item is only safe to use (i.e. guaranteed not to have disposed)
|
90
|
+
// Safety: item is only safe to use (i.e. guaranteed not to have been disposed)
|
89
91
|
// during this tick.
|
90
92
|
const [cacheItem, item, disposeOfTemporaryRetain] = parentCache.getOrPopulateAndTemporaryRetain();
|
91
93
|
return { state: item };
|
92
94
|
}
|
93
|
-
exports.useCachedPrecommitValue = useCachedPrecommitValue;
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { ParentCache } from './ParentCache';
|
2
1
|
import { ItemCleanupPair } from '@isograph/disposable-types';
|
2
|
+
import { ParentCache } from './ParentCache';
|
3
3
|
import { UnassignedState } from './useUpdatableDisposableState';
|
4
4
|
type UseUpdatableDisposableStateReturnValue<T> = {
|
5
5
|
state: T;
|
@@ -7,3 +7,4 @@ type UseUpdatableDisposableStateReturnValue<T> = {
|
|
7
7
|
};
|
8
8
|
export declare function useDisposableState<T = never>(parentCache: ParentCache<T>): UseUpdatableDisposableStateReturnValue<T>;
|
9
9
|
export {};
|
10
|
+
//# sourceMappingURL=useDisposableState.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useDisposableState.d.ts","sourceRoot":"","sources":["../src/useDisposableState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAE7D,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAG5C,OAAO,EAEL,eAAe,EAEhB,MAAM,+BAA+B,CAAC;AAEvC,KAAK,sCAAsC,CAAC,CAAC,IAAI;IAC/C,KAAK,EAAE,CAAC,CAAC;IACT,QAAQ,EAAE,CAAC,IAAI,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC,EAAE,eAAe,CAAC,CAAC,KAAK,IAAI,CAAC;CACxE,CAAC;AAEF,wBAAgB,kBAAkB,CAAC,CAAC,GAAG,KAAK,EAC1C,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,GAC1B,sCAAsC,CAAC,CAAC,CAAC,CAgE3C"}
|
@@ -1,13 +1,15 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.useDisposableState =
|
3
|
+
exports.useDisposableState = useDisposableState;
|
4
4
|
const react_1 = require("react");
|
5
|
-
const
|
5
|
+
const useCachedResponsivePrecommitValue_1 = require("./useCachedResponsivePrecommitValue");
|
6
6
|
const useUpdatableDisposableState_1 = require("./useUpdatableDisposableState");
|
7
7
|
function useDisposableState(parentCache) {
|
8
8
|
var _a, _b, _c;
|
9
9
|
const itemCleanupPairRef = (0, react_1.useRef)(null);
|
10
|
-
const preCommitItem = (0,
|
10
|
+
const preCommitItem = (0, useCachedResponsivePrecommitValue_1.useCachedResponsivePrecommitValue)(parentCache, (pair) => {
|
11
|
+
var _a;
|
12
|
+
(_a = itemCleanupPairRef.current) === null || _a === void 0 ? void 0 : _a[1]();
|
11
13
|
itemCleanupPairRef.current = pair;
|
12
14
|
});
|
13
15
|
const { state: stateFromDisposableStateHook, setState } = (0, useUpdatableDisposableState_1.useUpdatableDisposableState)();
|
@@ -44,15 +46,14 @@ function useDisposableState(parentCache) {
|
|
44
46
|
// Note that in the post-commit post-setState state, itemCleanupPairRef
|
45
47
|
// can still be assigned, during the render before the
|
46
48
|
// cleanupItemCleanupPairRefAfterSetState effect is called.
|
47
|
-
const state = (
|
49
|
+
const state = (_b = (_a = (stateFromDisposableStateHook != useUpdatableDisposableState_1.UNASSIGNED_STATE
|
48
50
|
? stateFromDisposableStateHook
|
49
|
-
: null)) !== null && _a !== void 0 ? _a :
|
51
|
+
: null)) !== null && _a !== void 0 ? _a : preCommitItem === null || preCommitItem === void 0 ? void 0 : preCommitItem.state) !== null && _b !== void 0 ? _b : (_c = itemCleanupPairRef.current) === null || _c === void 0 ? void 0 : _c[0];
|
50
52
|
return {
|
51
53
|
state: state,
|
52
54
|
setState,
|
53
55
|
};
|
54
56
|
}
|
55
|
-
exports.useDisposableState = useDisposableState;
|
56
57
|
// @ts-ignore
|
57
58
|
function tsTests() {
|
58
59
|
let x;
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useHasCommittedRef.d.ts","sourceRoot":"","sources":["../src/useHasCommittedRef.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAqB,MAAM,OAAO,CAAC;AAE5D;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,gBAAgB,CAAC,OAAO,CAAC,CAM9D"}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.useHasCommittedRef =
|
3
|
+
exports.useHasCommittedRef = useHasCommittedRef;
|
4
4
|
const react_1 = require("react");
|
5
5
|
/**
|
6
6
|
* Returns true if the component has committed, false otherwise.
|
@@ -12,4 +12,3 @@ function useHasCommittedRef() {
|
|
12
12
|
}, []);
|
13
13
|
return hasCommittedRef;
|
14
14
|
}
|
15
|
-
exports.useHasCommittedRef = useHasCommittedRef;
|
@@ -1,13 +1,15 @@
|
|
1
1
|
import { ParentCache } from './ParentCache';
|
2
|
+
import { type UnassignedState } from './useUpdatableDisposableState';
|
2
3
|
/**
|
3
4
|
* useLazyDisposableState<T>
|
4
5
|
* - Takes a mutable parent cache and a factory function
|
5
6
|
* - Returns { state: T }
|
6
7
|
*
|
7
|
-
* This lazily loads the disposable item using
|
8
|
+
* This lazily loads the disposable item using useCachedResponsivePrecommitValue, then
|
8
9
|
* (on commit) sets it in state. The item continues to be returned after
|
9
10
|
* commit and is disposed when the hook unmounts.
|
10
11
|
*/
|
11
|
-
export declare function useLazyDisposableState<T>(parentCache: ParentCache<T
|
12
|
+
export declare function useLazyDisposableState<T>(parentCache: ParentCache<Exclude<T, UnassignedState>>): {
|
12
13
|
state: T;
|
13
14
|
};
|
15
|
+
//# sourceMappingURL=useLazyDisposableState.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useLazyDisposableState.d.ts","sourceRoot":"","sources":["../src/useLazyDisposableState.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAGrE;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,EACtC,WAAW,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC,EAAE,eAAe,CAAC,CAAC,GACpD;IACD,KAAK,EAAE,CAAC,CAAC;CACV,CAoCA"}
|
@@ -1,31 +1,35 @@
|
|
1
1
|
'use strict';
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.useLazyDisposableState =
|
3
|
+
exports.useLazyDisposableState = useLazyDisposableState;
|
4
4
|
const react_1 = require("react");
|
5
|
-
const
|
5
|
+
const useCachedResponsivePrecommitValue_1 = require("./useCachedResponsivePrecommitValue");
|
6
6
|
/**
|
7
7
|
* useLazyDisposableState<T>
|
8
8
|
* - Takes a mutable parent cache and a factory function
|
9
9
|
* - Returns { state: T }
|
10
10
|
*
|
11
|
-
* This lazily loads the disposable item using
|
11
|
+
* This lazily loads the disposable item using useCachedResponsivePrecommitValue, then
|
12
12
|
* (on commit) sets it in state. The item continues to be returned after
|
13
13
|
* commit and is disposed when the hook unmounts.
|
14
14
|
*/
|
15
15
|
function useLazyDisposableState(parentCache) {
|
16
16
|
var _a, _b;
|
17
17
|
const itemCleanupPairRef = (0, react_1.useRef)(null);
|
18
|
-
const preCommitItem = (0,
|
18
|
+
const preCommitItem = (0, useCachedResponsivePrecommitValue_1.useCachedResponsivePrecommitValue)(parentCache, (pair) => {
|
19
|
+
var _a;
|
20
|
+
(_a = itemCleanupPairRef.current) === null || _a === void 0 ? void 0 : _a[1]();
|
19
21
|
itemCleanupPairRef.current = pair;
|
20
22
|
});
|
21
23
|
(0, react_1.useEffect)(() => {
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
return () => {
|
25
|
+
var _a;
|
26
|
+
const cleanupFn = (_a = itemCleanupPairRef.current) === null || _a === void 0 ? void 0 : _a[1];
|
27
|
+
// TODO confirm useEffect is called in order.
|
28
|
+
if (cleanupFn == null) {
|
29
|
+
throw new Error('cleanupFn unexpectedly null. This indicates a bug in react-disposable-state.');
|
30
|
+
}
|
31
|
+
return cleanupFn();
|
32
|
+
};
|
29
33
|
}, []);
|
30
34
|
const returnedItem = (_a = preCommitItem === null || preCommitItem === void 0 ? void 0 : preCommitItem.state) !== null && _a !== void 0 ? _a : (_b = itemCleanupPairRef.current) === null || _b === void 0 ? void 0 : _b[0];
|
31
35
|
if (returnedItem != null) {
|
@@ -36,4 +40,3 @@ function useLazyDisposableState(parentCache) {
|
|
36
40
|
// so during subsequent renders, itemCleanupPairRef.current is non-null.
|
37
41
|
throw new Error('returnedItem was unexpectedly null. This indicates a bug in react-disposable-state.');
|
38
42
|
}
|
39
|
-
exports.useLazyDisposableState = useLazyDisposableState;
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useUpdatableDisposableState.d.ts","sourceRoot":"","sources":["../src/useUpdatableDisposableState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAI7D,eAAO,MAAM,gBAAgB,EAAE,OAAO,MAAiB,CAAC;AACxD,MAAM,MAAM,eAAe,GAAG,OAAO,gBAAgB,CAAC;AAEtD,KAAK,sCAAsC,CAAC,CAAC,IAAI;IAC/C,KAAK,EAAE,CAAC,GAAG,eAAe,CAAC;IAC3B,QAAQ,EAAE,CAAC,IAAI,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC,EAAE,eAAe,CAAC,CAAC,KAAK,IAAI,CAAC;CACxE,CAAC;AAoBF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,2BAA2B,CACzC,CAAC,GAAG,KAAK,KACN,sCAAsC,CAAC,CAAC,CAAC,CA0D7C"}
|
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.UNASSIGNED_STATE = void 0;
|
4
|
+
exports.useUpdatableDisposableState = useUpdatableDisposableState;
|
4
5
|
const react_1 = require("react");
|
5
6
|
const useHasCommittedRef_1 = require("./useHasCommittedRef");
|
6
7
|
exports.UNASSIGNED_STATE = Symbol();
|
@@ -77,7 +78,6 @@ function useUpdatableDisposableState() {
|
|
77
78
|
state: stateICI !== exports.UNASSIGNED_STATE ? stateICI.item : exports.UNASSIGNED_STATE,
|
78
79
|
};
|
79
80
|
}
|
80
|
-
exports.useUpdatableDisposableState = useUpdatableDisposableState;
|
81
81
|
// @ts-ignore
|
82
82
|
function tsTests() {
|
83
83
|
const a = useUpdatableDisposableState();
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@isograph/react-disposable-state",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.3.0",
|
4
4
|
"description": "Primitives for managing disposable state in React",
|
5
5
|
"homepage": "https://isograph.dev",
|
6
6
|
"main": "dist/index.js",
|
@@ -14,21 +14,24 @@
|
|
14
14
|
"test-watch": "vitest watch",
|
15
15
|
"coverage": "vitest run --coverage",
|
16
16
|
"note": "WE SHOULD ALSO TEST HERE",
|
17
|
-
"prepack": "
|
17
|
+
"prepack": "pnpm run compile",
|
18
|
+
"tsc": "tsc"
|
18
19
|
},
|
19
20
|
"dependencies": {
|
20
|
-
"@isograph/disposable-types": "
|
21
|
-
|
21
|
+
"@isograph/disposable-types": "*"
|
22
|
+
},
|
23
|
+
"peerDependencies": {
|
24
|
+
"react": "18.3.1"
|
22
25
|
},
|
23
26
|
"devDependencies": {
|
24
|
-
"@types/react": "
|
27
|
+
"@types/react": "18.3.1",
|
25
28
|
"react-test-renderer": "^18.2.0",
|
26
|
-
"
|
27
|
-
"typescript": "^5.0.3"
|
29
|
+
"typescript": "5.6.3"
|
28
30
|
},
|
29
31
|
"repository": {
|
30
32
|
"type": "git",
|
31
33
|
"url": "git+https://github.com/isographlabs/isograph.git",
|
32
34
|
"directory": "libs/isograph-react-disposable-state"
|
33
|
-
}
|
35
|
+
},
|
36
|
+
"sideEffects": false
|
34
37
|
}
|
package/src/ParentCache.ts
CHANGED
@@ -28,12 +28,14 @@ export class ParentCache<T> {
|
|
28
28
|
private __cacheItem: CacheItem<T> | null = null;
|
29
29
|
private readonly __factory: Factory<T>;
|
30
30
|
|
31
|
+
// TODO pass an onEmpty function, which can e.g. remove this ParentCache
|
32
|
+
// from some parent object.
|
31
33
|
constructor(factory: Factory<T>) {
|
32
34
|
this.__factory = factory;
|
33
35
|
}
|
34
36
|
|
35
37
|
/**
|
36
|
-
* This is called from
|
38
|
+
* This is called from useCachedResponsivePrecommitValue, when the parent cache is populated
|
37
39
|
* and a previous temporary retain has been disposed. This can occur in scenarios like:
|
38
40
|
* - temporary retain A is created by component B rendering
|
39
41
|
* - temporary retain A expires, emptying the parent cache
|
package/src/index.ts
CHANGED
@@ -2,7 +2,7 @@ export * from '@isograph/disposable-types';
|
|
2
2
|
|
3
3
|
export * from './CacheItem';
|
4
4
|
export * from './ParentCache';
|
5
|
-
export * from './
|
5
|
+
export * from './useCachedResponsivePrecommitValue';
|
6
6
|
export * from './useDisposableState';
|
7
7
|
export * from './useHasCommittedRef';
|
8
8
|
export * from './useLazyDisposableState';
|