@isograph/react-disposable-state 0.0.0-main-a8e129cf → 0.0.0-main-082842ca

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.
Files changed (48) hide show
  1. package/.turbo/turbo-compile-libs.log +3 -3
  2. package/.turbo/turbo-tsc.log +2 -2
  3. package/README.md +19 -0
  4. package/dist/index.cjs +4 -0
  5. package/dist/index.d.cts +3 -1
  6. package/dist/index.d.mts +3 -1
  7. package/dist/index.mjs +3 -1
  8. package/dist/useCachedResponsivePrecommitValue.cjs +13 -7
  9. package/dist/useCachedResponsivePrecommitValue.d.cts.map +1 -1
  10. package/dist/useCachedResponsivePrecommitValue.d.mts.map +1 -1
  11. package/dist/useCachedResponsivePrecommitValue.mjs +13 -7
  12. package/dist/useCachedResponsivePrecommitValue.mjs.map +1 -1
  13. package/dist/useDisposableState.cjs +7 -6
  14. package/dist/useDisposableState.mjs +7 -6
  15. package/dist/useDisposableState.mjs.map +1 -1
  16. package/dist/useEffectsRerunWithoutRender.cjs +27 -0
  17. package/dist/useEffectsRerunWithoutRender.d.cts +19 -0
  18. package/dist/useEffectsRerunWithoutRender.d.cts.map +1 -0
  19. package/dist/useEffectsRerunWithoutRender.d.mts +19 -0
  20. package/dist/useEffectsRerunWithoutRender.d.mts.map +1 -0
  21. package/dist/useEffectsRerunWithoutRender.mjs +28 -0
  22. package/dist/useEffectsRerunWithoutRender.mjs.map +1 -0
  23. package/dist/useLazyDisposableState.cjs +3 -3
  24. package/dist/useLazyDisposableState.mjs +3 -3
  25. package/dist/useLazyDisposableState.mjs.map +1 -1
  26. package/dist/useUpdatableDisposableClearableState.cjs +36 -0
  27. package/dist/useUpdatableDisposableClearableState.d.cts +26 -0
  28. package/dist/useUpdatableDisposableClearableState.d.cts.map +1 -0
  29. package/dist/useUpdatableDisposableClearableState.d.mts +26 -0
  30. package/dist/useUpdatableDisposableClearableState.d.mts.map +1 -0
  31. package/dist/useUpdatableDisposableClearableState.mjs +37 -0
  32. package/dist/useUpdatableDisposableClearableState.mjs.map +1 -0
  33. package/dist/useUpdatableDisposableState.cjs +5 -1
  34. package/dist/useUpdatableDisposableState.mjs +5 -1
  35. package/dist/useUpdatableDisposableState.mjs.map +1 -1
  36. package/package.json +5 -2
  37. package/src/index.ts +2 -0
  38. package/src/useCachedResponsivePrecommitValue.test.tsx +8 -3
  39. package/src/useCachedResponsivePrecommitValue.ts +32 -20
  40. package/src/useDisposableState.ts +13 -15
  41. package/src/useEffectsRerunWithoutRender.test.tsx +54 -0
  42. package/src/useEffectsRerunWithoutRender.ts +23 -0
  43. package/src/useLazyDisposableState.test.tsx +73 -34
  44. package/src/useLazyDisposableState.ts +3 -4
  45. package/src/useUpdatableDisposableClearableState.ts +67 -0
  46. package/src/useUpdatableDisposableState.test.tsx +3 -2
  47. package/src/useUpdatableDisposableState.ts +2 -0
  48. package/vitest.config.ts +8 -0
@@ -1,11 +1,11 @@
1
- ../.. |  WARN  Unsupported engine: wanted: {"node":"24.12.0"} (current: {"node":"v24.20.0","pnpm":"10.15.0"})
1
+ ../.. |  WARN  Unsupported engine: wanted: {"node":"24.12.0"} (current: {"node":"v24.21.0","pnpm":"10.15.0"})
2
2
 
3
- > @isograph/react-disposable-state@0.0.0-main-a8e129cf compile-libs /home/runner/work/isograph/isograph/libs/isograph-react-disposable-state
3
+ > @isograph/react-disposable-state@0.0.0-main-082842ca compile-libs /home/runner/work/isograph/isograph/libs/isograph-react-disposable-state
4
4
  > tsdown
5
5
 
6
6
  ℹ tsdown v0.20.1 powered by rolldown v1.0.0-rc.1
7
7
  ℹ config file: /home/runner/work/isograph/isograph/tsdown.config.ts
8
- (node:2729) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/runner/work/isograph/isograph/tsdown.config.ts?no-cache=77ede110-960f-4f9b-b5d4-94a217d394dd is not specified and it doesn't parse as CommonJS.
8
+ (node:2571) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/runner/work/isograph/isograph/tsdown.config.ts?no-cache=d49a3978-e0e9-45c9-8009-cf6a5d84baaf 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)
@@ -1,5 +1,5 @@
1
- ../.. |  WARN  Unsupported engine: wanted: {"node":"24.12.0"} (current: {"node":"v24.20.0","pnpm":"10.15.0"})
1
+ ../.. |  WARN  Unsupported engine: wanted: {"node":"24.12.0"} (current: {"node":"v24.21.0","pnpm":"10.15.0"})
2
2
 
3
- > @isograph/react-disposable-state@0.0.0-main-a8e129cf tsc /home/runner/work/isograph/isograph/libs/isograph-react-disposable-state
3
+ > @isograph/react-disposable-state@0.0.0-main-082842ca tsc /home/runner/work/isograph/isograph/libs/isograph-react-disposable-state
4
4
  > tsc
5
5
 
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!
package/dist/index.cjs CHANGED
@@ -1,10 +1,12 @@
1
1
  const require_CacheItem = require('./CacheItem.cjs');
2
2
  const require_ParentCache = require('./ParentCache.cjs');
3
+ const require_useEffectsRerunWithoutRender = require('./useEffectsRerunWithoutRender.cjs');
3
4
  const require_useCachedResponsivePrecommitValue = require('./useCachedResponsivePrecommitValue.cjs');
4
5
  const require_useHasCommittedRef = require('./useHasCommittedRef.cjs');
5
6
  const require_useUpdatableDisposableState = require('./useUpdatableDisposableState.cjs');
6
7
  const require_useDisposableState = require('./useDisposableState.cjs');
7
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,8 +14,10 @@ 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
22
  var _isograph_disposable_types = require("@isograph/disposable-types");
19
23
  Object.keys(_isograph_disposable_types).forEach(function (k) {
package/dist/index.d.cts CHANGED
@@ -3,7 +3,9 @@ import { ParentCache } from "./ParentCache.cjs";
3
3
  import { useCachedResponsivePrecommitValue } from "./useCachedResponsivePrecommitValue.cjs";
4
4
  import { UNASSIGNED_STATE, UnassignedState, useUpdatableDisposableState } from "./useUpdatableDisposableState.cjs";
5
5
  import { useDisposableState } from "./useDisposableState.cjs";
6
+ import { useEffectsRerunWithoutRenderRef } from "./useEffectsRerunWithoutRender.cjs";
6
7
  import { useHasCommittedRef } from "./useHasCommittedRef.cjs";
7
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,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ const require_useEffectsRerunWithoutRender = require('./useEffectsRerunWithoutRender.cjs');
3
4
  let react = require("react");
4
5
 
5
6
  //#region src/useCachedResponsivePrecommitValue.ts
@@ -38,16 +39,21 @@ let react = require("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
- const undisposedPair = cacheItem.permanentRetainIfNotDisposed(disposeOfTemporaryRetain);
44
- if (undisposedPair != null) onCommit(undisposedPair);
45
- else {
46
- const existingCacheItemCleanupPair = parentCache.getAndPermanentRetainIfPresent();
47
- if (existingCacheItemCleanupPair != null) onCommit(existingCacheItemCleanupPair);
48
- else onCommit(parentCache.factory());
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();
@@ -1 +1 @@
1
- {"version":3,"file":"useCachedResponsivePrecommitValue.d.cts","names":[],"sources":["../src/useCachedResponsivePrecommitValue.ts"],"mappings":";;;;;;AAsCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAgB,iCAAA,GAAA,CACd,WAAA,EAAa,WAAA,CAAY,CAAA,GACzB,QAAA,GAAW,IAAA,EAAM,eAAA,CAAgB,CAAA;EAC9B,KAAA,EAAO,CAAA;AAAA"}
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"}
@@ -1 +1 @@
1
- {"version":3,"file":"useCachedResponsivePrecommitValue.d.mts","names":[],"sources":["../src/useCachedResponsivePrecommitValue.ts"],"mappings":";;;;;;AAsCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAgB,iCAAA,GAAA,CACd,WAAA,EAAa,WAAA,CAAY,CAAA,GACzB,QAAA,GAAW,IAAA,EAAM,eAAA,CAAgB,CAAA;EAC9B,KAAA,EAAO,CAAA;AAAA"}
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
- const undisposedPair = cacheItem.permanentRetainIfNotDisposed(disposeOfTemporaryRetain);
42
- if (undisposedPair != null) onCommit(undisposedPair);
43
- else {
44
- const existingCacheItemCleanupPair = parentCache.getAndPermanentRetainIfPresent();
45
- if (existingCacheItemCleanupPair != null) onCommit(existingCacheItemCleanupPair);
46
- else onCommit(parentCache.factory());
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 disposeOfTemporaryRetain,\n );\n if (undisposedPair != null) {\n onCommit(undisposedPair);\n } else {\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 }\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,SAAgB,kCACd,aACA,UACqB;CAOrB,MAAM,GAAG,YAAY,SAAoB,KAAK;CAC9C,MAAM,2BAA2B,OAA8B,KAAK;AAEpE,iBAAgB;AACd,2BAAyB,UAAU;EAiBnC,MAAM,iBAAiB,UAAU,6BAC/B,yBACD;AACD,MAAI,kBAAkB,KACpB,UAAS,eAAe;OACnB;GAGL,MAAM,+BACJ,YAAY,gCAAgC;AAC9C,OAAI,gCAAgC,KAClC,UAAS,6BAA6B;OAGtC,UAAS,YAAY,SAAS,CAAC;AAMjC,YAAS,EAAE,CAAC;;IAEb,CAAC,YAAY,CAAC;AAEjB,KAAI,yBAAyB,YAAY,YACvC,QAAO;CAKT,MAAM,CAAC,WAAW,MAAM,4BACtB,YAAY,iCAAiC;AAE/C,QAAO,EAAE,OAAO,MAAM"}
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"}
@@ -6,15 +6,16 @@ let react = require("react");
6
6
  function useDisposableState(parentCache) {
7
7
  const itemCleanupPairRef = (0, react.useRef)(null);
8
8
  const preCommitItem = require_useCachedResponsivePrecommitValue.useCachedResponsivePrecommitValue(parentCache, (pair) => {
9
- itemCleanupPairRef.current?.[1]();
10
9
  itemCleanupPairRef.current = pair;
11
10
  });
12
11
  const { state: stateFromDisposableStateHook, setState } = require_useUpdatableDisposableState.useUpdatableDisposableState();
13
12
  (0, react.useEffect)(function cleanupItemCleanupPairRefAfterSetState() {
14
- if (stateFromDisposableStateHook !== require_useUpdatableDisposableState.UNASSIGNED_STATE) if (itemCleanupPairRef.current != null) {
15
- itemCleanupPairRef.current[1]();
16
- itemCleanupPairRef.current = null;
17
- } else throw new Error("itemCleanupPairRef.current is unexpectedly null. This indicates a bug in react-disposable-state.");
13
+ if (stateFromDisposableStateHook !== require_useUpdatableDisposableState.UNASSIGNED_STATE) {
14
+ if (itemCleanupPairRef.current != null) {
15
+ itemCleanupPairRef.current[1]();
16
+ itemCleanupPairRef.current = null;
17
+ }
18
+ }
18
19
  }, [stateFromDisposableStateHook]);
19
20
  (0, react.useEffect)(function cleanupItemCleanupPairRefIfSetStateNotCalled() {
20
21
  return () => {
@@ -23,7 +24,7 @@ function useDisposableState(parentCache) {
23
24
  itemCleanupPairRef.current = null;
24
25
  }
25
26
  };
26
- }, []);
27
+ }, [parentCache]);
27
28
  const state = (stateFromDisposableStateHook !== require_useUpdatableDisposableState.UNASSIGNED_STATE ? stateFromDisposableStateHook : null) ?? preCommitItem?.state ?? itemCleanupPairRef.current?.[0];
28
29
  if (state != null) return {
29
30
  state,
@@ -6,15 +6,16 @@ import { useEffect, useRef } from "react";
6
6
  function useDisposableState(parentCache) {
7
7
  const itemCleanupPairRef = useRef(null);
8
8
  const preCommitItem = useCachedResponsivePrecommitValue(parentCache, (pair) => {
9
- itemCleanupPairRef.current?.[1]();
10
9
  itemCleanupPairRef.current = pair;
11
10
  });
12
11
  const { state: stateFromDisposableStateHook, setState } = useUpdatableDisposableState();
13
12
  useEffect(function cleanupItemCleanupPairRefAfterSetState() {
14
- if (stateFromDisposableStateHook !== UNASSIGNED_STATE) if (itemCleanupPairRef.current != null) {
15
- itemCleanupPairRef.current[1]();
16
- itemCleanupPairRef.current = null;
17
- } else throw new Error("itemCleanupPairRef.current is unexpectedly null. This indicates a bug in react-disposable-state.");
13
+ if (stateFromDisposableStateHook !== UNASSIGNED_STATE) {
14
+ if (itemCleanupPairRef.current != null) {
15
+ itemCleanupPairRef.current[1]();
16
+ itemCleanupPairRef.current = null;
17
+ }
18
+ }
18
19
  }, [stateFromDisposableStateHook]);
19
20
  useEffect(function cleanupItemCleanupPairRefIfSetStateNotCalled() {
20
21
  return () => {
@@ -23,7 +24,7 @@ function useDisposableState(parentCache) {
23
24
  itemCleanupPairRef.current = null;
24
25
  }
25
26
  };
26
- }, []);
27
+ }, [parentCache]);
27
28
  const state = (stateFromDisposableStateHook !== UNASSIGNED_STATE ? stateFromDisposableStateHook : null) ?? preCommitItem?.state ?? itemCleanupPairRef.current?.[0];
28
29
  if (state != null) return {
29
30
  state,
@@ -1 +1 @@
1
- {"version":3,"file":"useDisposableState.mjs","names":[],"sources":["../src/useDisposableState.ts"],"sourcesContent":["import type { ItemCleanupPair } from '@isograph/disposable-types';\nimport { useEffect, useRef } from 'react';\nimport type { ParentCache } from './ParentCache';\nimport { useCachedResponsivePrecommitValue } from './useCachedResponsivePrecommitValue';\nimport {\n UNASSIGNED_STATE,\n type UnassignedState,\n useUpdatableDisposableState,\n} from './useUpdatableDisposableState';\n\ntype UseUpdatableDisposableStateReturnValue<T> = {\n state: T;\n setState: (pair: ItemCleanupPair<Exclude<T, UnassignedState>>) => void;\n};\n\nexport function useDisposableState<T = never>(\n parentCache: ParentCache<T>,\n): UseUpdatableDisposableStateReturnValue<T> {\n const itemCleanupPairRef = useRef<ItemCleanupPair<T> | null>(null);\n\n const preCommitItem = useCachedResponsivePrecommitValue(\n parentCache,\n (pair) => {\n itemCleanupPairRef.current?.[1]();\n itemCleanupPairRef.current = pair;\n },\n );\n\n const { state: stateFromDisposableStateHook, setState } =\n useUpdatableDisposableState<T>();\n\n useEffect(\n function cleanupItemCleanupPairRefAfterSetState() {\n if (stateFromDisposableStateHook !== UNASSIGNED_STATE) {\n if (itemCleanupPairRef.current != null) {\n itemCleanupPairRef.current[1]();\n itemCleanupPairRef.current = null;\n } else {\n throw new Error(\n 'itemCleanupPairRef.current is unexpectedly null. ' +\n 'This indicates a bug in react-disposable-state.',\n );\n }\n }\n },\n [stateFromDisposableStateHook],\n );\n\n useEffect(function cleanupItemCleanupPairRefIfSetStateNotCalled() {\n return () => {\n if (itemCleanupPairRef.current != null) {\n itemCleanupPairRef.current[1]();\n itemCleanupPairRef.current = null;\n }\n };\n }, []);\n const state: T | undefined =\n (stateFromDisposableStateHook !== UNASSIGNED_STATE\n ? stateFromDisposableStateHook\n : null) ??\n preCommitItem?.state ??\n itemCleanupPairRef.current?.[0];\n\n if (state != null) {\n return {\n state: state,\n setState,\n };\n }\n\n // Safety: we can be in one of three states. Pre-commit, in which case\n // preCommitItem is assigned, post-commit but before setState has been\n // called, in which case itemCleanupPairRef.current is assigned, or\n // after setState has been called, in which case\n // stateFromDisposableStateHook is assigned.\n //\n // Therefore, the type of state is T, not T | undefined. But the fact\n // that we are in one of the three states is not reflected in the types.\n // So we have to cast to T.\n //\n // Note that in the post-commit post-setState state, itemCleanupPairRef\n // can still be assigned, during the render before the\n // cleanupItemCleanupPairRefAfterSetState effect is called.\n throw new Error(\n 'state was unexpectedly null. This indicates a bug in react-disposable-state.',\n );\n}\n\n// @ts-ignore\nfunction tsTests() {\n let x: any;\n const a = useDisposableState(x);\n // This should be a compiler error, because the generic is inferred to be of\n // type never. TODO determine why this doesn't break the build!\n // @ts-expect-error\n a.setState(['asdf', () => {}]);\n // @ts-expect-error\n a.setState([UNASSIGNED_STATE, () => {}]);\n const b = useDisposableState<string | UnassignedState>(x);\n // @ts-expect-error\n b.setState([UNASSIGNED_STATE, () => {}]);\n b.setState(['asdf', () => {}]);\n}\n"],"mappings":";;;;;AAeA,SAAgB,mBACd,aAC2C;CAC3C,MAAM,qBAAqB,OAAkC,KAAK;CAElE,MAAM,gBAAgB,kCACpB,cACC,SAAS;AACR,qBAAmB,UAAU,IAAI;AACjC,qBAAmB,UAAU;GAEhC;CAED,MAAM,EAAE,OAAO,8BAA8B,aAC3C,6BAAgC;AAElC,WACE,SAAS,yCAAyC;AAChD,MAAI,iCAAiC,iBACnC,KAAI,mBAAmB,WAAW,MAAM;AACtC,sBAAmB,QAAQ,IAAI;AAC/B,sBAAmB,UAAU;QAE7B,OAAM,IAAI,MACR,mGAED;IAIP,CAAC,6BAA6B,CAC/B;AAED,WAAU,SAAS,+CAA+C;AAChE,eAAa;AACX,OAAI,mBAAmB,WAAW,MAAM;AACtC,uBAAmB,QAAQ,IAAI;AAC/B,uBAAmB,UAAU;;;IAGhC,EAAE,CAAC;CACN,MAAM,SACH,iCAAiC,mBAC9B,+BACA,SACJ,eAAe,SACf,mBAAmB,UAAU;AAE/B,KAAI,SAAS,KACX,QAAO;EACE;EACP;EACD;AAgBH,OAAM,IAAI,MACR,+EACD"}
1
+ {"version":3,"file":"useDisposableState.mjs","names":[],"sources":["../src/useDisposableState.ts"],"sourcesContent":["import type { ItemCleanupPair } from '@isograph/disposable-types';\nimport { useEffect, useRef } from 'react';\nimport type { ParentCache } from './ParentCache';\nimport { useCachedResponsivePrecommitValue } from './useCachedResponsivePrecommitValue';\nimport {\n UNASSIGNED_STATE,\n type UnassignedState,\n useUpdatableDisposableState,\n} from './useUpdatableDisposableState';\n\ntype UseUpdatableDisposableStateReturnValue<T> = {\n state: T;\n setState: (pair: ItemCleanupPair<Exclude<T, UnassignedState>>) => void;\n};\n\nexport function useDisposableState<T = never>(\n parentCache: ParentCache<T>,\n): UseUpdatableDisposableStateReturnValue<T> {\n const itemCleanupPairRef = useRef<ItemCleanupPair<T> | null>(null);\n\n const preCommitItem = useCachedResponsivePrecommitValue(\n parentCache,\n (pair) => {\n itemCleanupPairRef.current = pair;\n },\n );\n\n const { state: stateFromDisposableStateHook, setState } =\n useUpdatableDisposableState<T>();\n\n useEffect(\n function cleanupItemCleanupPairRefAfterSetState() {\n if (stateFromDisposableStateHook !== UNASSIGNED_STATE) {\n if (itemCleanupPairRef.current != null) {\n itemCleanupPairRef.current[1]();\n itemCleanupPairRef.current = null;\n }\n }\n },\n [stateFromDisposableStateHook],\n );\n\n useEffect(\n function cleanupItemCleanupPairRefIfSetStateNotCalled() {\n return () => {\n if (itemCleanupPairRef.current != null) {\n itemCleanupPairRef.current[1]();\n // Forgotten so that nothing can release this pair a second time\n // before an effect re-run (StrictMode, Fast Refresh) stores a new one.\n itemCleanupPairRef.current = null;\n }\n };\n },\n [parentCache],\n );\n const state: T | undefined =\n (stateFromDisposableStateHook !== UNASSIGNED_STATE\n ? stateFromDisposableStateHook\n : null) ??\n preCommitItem?.state ??\n itemCleanupPairRef.current?.[0];\n\n if (state != null) {\n return {\n state: state,\n setState,\n };\n }\n // Safety: we can be in one of three states. Pre-commit, in which case\n // preCommitItem is assigned, post-commit but before setState has been\n // called, in which case itemCleanupPairRef.current is assigned, or\n // after setState has been called, in which case\n // stateFromDisposableStateHook is assigned.\n //\n // Therefore, the type of state is T, not T | undefined. But the fact\n // that we are in one of the three states is not reflected in the types.\n // So we have to cast to T.\n //\n // Note that in the post-commit post-setState state, itemCleanupPairRef\n // can still be assigned, during the render before the\n // cleanupItemCleanupPairRefAfterSetState effect is called.\n throw new Error(\n 'state was unexpectedly null. This indicates a bug in react-disposable-state.',\n );\n}\n\n// @ts-ignore\nfunction tsTests() {\n let x: any;\n const a = useDisposableState(x);\n // This should be a compiler error, because the generic is inferred to be of\n // type never. TODO determine why this doesn't break the build!\n // @ts-expect-error\n a.setState(['asdf', () => {}]);\n // @ts-expect-error\n a.setState([UNASSIGNED_STATE, () => {}]);\n const b = useDisposableState<string | UnassignedState>(x);\n // @ts-expect-error\n b.setState([UNASSIGNED_STATE, () => {}]);\n b.setState(['asdf', () => {}]);\n}\n"],"mappings":";;;;;AAeA,SAAgB,mBACd,aAC2C;CAC3C,MAAM,qBAAqB,OAAkC,KAAK;CAElE,MAAM,gBAAgB,kCACpB,cACC,SAAS;AACR,qBAAmB,UAAU;GAEhC;CAED,MAAM,EAAE,OAAO,8BAA8B,aAC3C,6BAAgC;AAElC,WACE,SAAS,yCAAyC;AAChD,MAAI,iCAAiC,kBACnC;OAAI,mBAAmB,WAAW,MAAM;AACtC,uBAAmB,QAAQ,IAAI;AAC/B,uBAAmB,UAAU;;;IAInC,CAAC,6BAA6B,CAC/B;AAED,WACE,SAAS,+CAA+C;AACtD,eAAa;AACX,OAAI,mBAAmB,WAAW,MAAM;AACtC,uBAAmB,QAAQ,IAAI;AAG/B,uBAAmB,UAAU;;;IAInC,CAAC,YAAY,CACd;CACD,MAAM,SACH,iCAAiC,mBAC9B,+BACA,SACJ,eAAe,SACf,mBAAmB,UAAU;AAE/B,KAAI,SAAS,KACX,QAAO;EACE;EACP;EACD;AAeH,OAAM,IAAI,MACR,+EACD"}
@@ -0,0 +1,27 @@
1
+ let react = require("react");
2
+
3
+ //#region src/useEffectsRerunWithoutRender.ts
4
+ /**
5
+ * Detects that React is running this component's effects again without a
6
+ * render in between. That happens on StrictMode's simulated unmount and
7
+ * remount on the first commit, on Fast Refresh, and when an `Activity`
8
+ * boundary hides and then shows the component. In each case React runs every
9
+ * effect's cleanup and then its mount, so a cleanup that sets a flag is
10
+ * observable from the mounts that follow.
11
+ *
12
+ * Returns a ref whose `current` is `true` from the moment the cleanup ran until
13
+ * the caller resets it. The effect that must tell a re-run apart from a first
14
+ * mount reads it and sets it back to `false`; nothing else clears it.
15
+ */
16
+ function useEffectsRerunWithoutRenderRef() {
17
+ const effectsRerunWithoutRenderRef = (0, react.useRef)(false);
18
+ (0, react.useEffect)(() => {
19
+ return () => {
20
+ effectsRerunWithoutRenderRef.current = true;
21
+ };
22
+ }, []);
23
+ return effectsRerunWithoutRenderRef;
24
+ }
25
+
26
+ //#endregion
27
+ exports.useEffectsRerunWithoutRenderRef = useEffectsRerunWithoutRenderRef;
@@ -0,0 +1,19 @@
1
+ import { MutableRefObject } from "react";
2
+
3
+ //#region src/useEffectsRerunWithoutRender.d.ts
4
+ /**
5
+ * Detects that React is running this component's effects again without a
6
+ * render in between. That happens on StrictMode's simulated unmount and
7
+ * remount on the first commit, on Fast Refresh, and when an `Activity`
8
+ * boundary hides and then shows the component. In each case React runs every
9
+ * effect's cleanup and then its mount, so a cleanup that sets a flag is
10
+ * observable from the mounts that follow.
11
+ *
12
+ * Returns a ref whose `current` is `true` from the moment the cleanup ran until
13
+ * the caller resets it. The effect that must tell a re-run apart from a first
14
+ * mount reads it and sets it back to `false`; nothing else clears it.
15
+ */
16
+ declare function useEffectsRerunWithoutRenderRef(): MutableRefObject<boolean>;
17
+ //#endregion
18
+ export { useEffectsRerunWithoutRenderRef };
19
+ //# sourceMappingURL=useEffectsRerunWithoutRender.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useEffectsRerunWithoutRender.d.cts","names":[],"sources":["../src/useEffectsRerunWithoutRender.ts"],"mappings":";;;;;AAcA;;;;;;;;;;iBAAgB,+BAAA,CAAA,GAAmC,gBAAA"}
@@ -0,0 +1,19 @@
1
+ import { MutableRefObject } from "react";
2
+
3
+ //#region src/useEffectsRerunWithoutRender.d.ts
4
+ /**
5
+ * Detects that React is running this component's effects again without a
6
+ * render in between. That happens on StrictMode's simulated unmount and
7
+ * remount on the first commit, on Fast Refresh, and when an `Activity`
8
+ * boundary hides and then shows the component. In each case React runs every
9
+ * effect's cleanup and then its mount, so a cleanup that sets a flag is
10
+ * observable from the mounts that follow.
11
+ *
12
+ * Returns a ref whose `current` is `true` from the moment the cleanup ran until
13
+ * the caller resets it. The effect that must tell a re-run apart from a first
14
+ * mount reads it and sets it back to `false`; nothing else clears it.
15
+ */
16
+ declare function useEffectsRerunWithoutRenderRef(): MutableRefObject<boolean>;
17
+ //#endregion
18
+ export { useEffectsRerunWithoutRenderRef };
19
+ //# sourceMappingURL=useEffectsRerunWithoutRender.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useEffectsRerunWithoutRender.d.mts","names":[],"sources":["../src/useEffectsRerunWithoutRender.ts"],"mappings":";;;;;AAcA;;;;;;;;;;iBAAgB,+BAAA,CAAA,GAAmC,gBAAA"}
@@ -0,0 +1,28 @@
1
+ import { useEffect, useRef } from "react";
2
+
3
+ //#region src/useEffectsRerunWithoutRender.ts
4
+ /**
5
+ * Detects that React is running this component's effects again without a
6
+ * render in between. That happens on StrictMode's simulated unmount and
7
+ * remount on the first commit, on Fast Refresh, and when an `Activity`
8
+ * boundary hides and then shows the component. In each case React runs every
9
+ * effect's cleanup and then its mount, so a cleanup that sets a flag is
10
+ * observable from the mounts that follow.
11
+ *
12
+ * Returns a ref whose `current` is `true` from the moment the cleanup ran until
13
+ * the caller resets it. The effect that must tell a re-run apart from a first
14
+ * mount reads it and sets it back to `false`; nothing else clears it.
15
+ */
16
+ function useEffectsRerunWithoutRenderRef() {
17
+ const effectsRerunWithoutRenderRef = useRef(false);
18
+ useEffect(() => {
19
+ return () => {
20
+ effectsRerunWithoutRenderRef.current = true;
21
+ };
22
+ }, []);
23
+ return effectsRerunWithoutRenderRef;
24
+ }
25
+
26
+ //#endregion
27
+ export { useEffectsRerunWithoutRenderRef };
28
+ //# sourceMappingURL=useEffectsRerunWithoutRender.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useEffectsRerunWithoutRender.mjs","names":[],"sources":["../src/useEffectsRerunWithoutRender.ts"],"sourcesContent":["import { type MutableRefObject, useEffect, useRef } from 'react';\n\n/**\n * Detects that React is running this component's effects again without a\n * render in between. That happens on StrictMode's simulated unmount and\n * remount on the first commit, on Fast Refresh, and when an `Activity`\n * boundary hides and then shows the component. In each case React runs every\n * effect's cleanup and then its mount, so a cleanup that sets a flag is\n * observable from the mounts that follow.\n *\n * Returns a ref whose `current` is `true` from the moment the cleanup ran until\n * the caller resets it. The effect that must tell a re-run apart from a first\n * mount reads it and sets it back to `false`; nothing else clears it.\n */\nexport function useEffectsRerunWithoutRenderRef(): MutableRefObject<boolean> {\n const effectsRerunWithoutRenderRef = useRef(false);\n useEffect(() => {\n return () => {\n effectsRerunWithoutRenderRef.current = true;\n };\n }, []);\n return effectsRerunWithoutRenderRef;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAcA,SAAgB,kCAA6D;CAC3E,MAAM,+BAA+B,OAAO,MAAM;AAClD,iBAAgB;AACd,eAAa;AACX,gCAA6B,UAAU;;IAExC,EAAE,CAAC;AACN,QAAO"}
@@ -16,16 +16,16 @@ let react = require("react");
16
16
  function useLazyDisposableState(parentCache) {
17
17
  const itemCleanupPairRef = (0, react.useRef)(null);
18
18
  const preCommitItem = require_useCachedResponsivePrecommitValue.useCachedResponsivePrecommitValue(parentCache, (pair) => {
19
- itemCleanupPairRef.current?.[1]();
20
19
  itemCleanupPairRef.current = pair;
21
20
  });
22
21
  (0, react.useEffect)(() => {
23
22
  return () => {
24
23
  const cleanupFn = itemCleanupPairRef.current?.[1];
25
24
  if (cleanupFn == null) throw new Error("cleanupFn unexpectedly null. This indicates a bug in react-disposable-state.");
26
- return cleanupFn();
25
+ cleanupFn();
26
+ itemCleanupPairRef.current = null;
27
27
  };
28
- }, []);
28
+ }, [parentCache]);
29
29
  const returnedItem = preCommitItem?.state ?? itemCleanupPairRef.current?.[0];
30
30
  if (returnedItem != null) return { state: returnedItem };
31
31
  throw new Error("returnedItem was unexpectedly null. This indicates a bug in react-disposable-state.");
@@ -14,16 +14,16 @@ import { useEffect, useRef } from "react";
14
14
  function useLazyDisposableState(parentCache) {
15
15
  const itemCleanupPairRef = useRef(null);
16
16
  const preCommitItem = useCachedResponsivePrecommitValue(parentCache, (pair) => {
17
- itemCleanupPairRef.current?.[1]();
18
17
  itemCleanupPairRef.current = pair;
19
18
  });
20
19
  useEffect(() => {
21
20
  return () => {
22
21
  const cleanupFn = itemCleanupPairRef.current?.[1];
23
22
  if (cleanupFn == null) throw new Error("cleanupFn unexpectedly null. This indicates a bug in react-disposable-state.");
24
- return cleanupFn();
23
+ cleanupFn();
24
+ itemCleanupPairRef.current = null;
25
25
  };
26
- }, []);
26
+ }, [parentCache]);
27
27
  const returnedItem = preCommitItem?.state ?? itemCleanupPairRef.current?.[0];
28
28
  if (returnedItem != null) return { state: returnedItem };
29
29
  throw new Error("returnedItem was unexpectedly null. This indicates a bug in react-disposable-state.");
@@ -1 +1 @@
1
- {"version":3,"file":"useLazyDisposableState.mjs","names":[],"sources":["../src/useLazyDisposableState.ts"],"sourcesContent":["'use strict';\n\nimport type { ItemCleanupPair } from '@isograph/disposable-types';\nimport { useEffect, useRef } from 'react';\nimport type { ParentCache } from './ParentCache';\nimport { useCachedResponsivePrecommitValue } from './useCachedResponsivePrecommitValue';\nimport { type UnassignedState } from './useUpdatableDisposableState';\n\n/**\n * useLazyDisposableState<T>\n * - Takes a mutable parent cache and a factory function\n * - Returns { state: T }\n *\n * This lazily loads the disposable item using useCachedResponsivePrecommitValue, then\n * (on commit) sets it in state. The item continues to be returned after\n * commit and is disposed when the hook unmounts.\n */\nexport function useLazyDisposableState<T>(\n parentCache: ParentCache<Exclude<T, UnassignedState>>,\n): {\n state: T;\n} {\n const itemCleanupPairRef = useRef<ItemCleanupPair<T> | null>(null);\n\n const preCommitItem = useCachedResponsivePrecommitValue(\n parentCache,\n (pair) => {\n itemCleanupPairRef.current?.[1]();\n itemCleanupPairRef.current = pair;\n },\n );\n\n useEffect(() => {\n return () => {\n const cleanupFn = itemCleanupPairRef.current?.[1];\n // TODO confirm useEffect is called in order.\n if (cleanupFn == null) {\n throw new Error(\n 'cleanupFn unexpectedly null. This indicates a bug in react-disposable-state.',\n );\n }\n return cleanupFn();\n };\n }, []);\n\n const returnedItem = preCommitItem?.state ?? itemCleanupPairRef.current?.[0];\n\n if (returnedItem != null) {\n return { state: returnedItem };\n }\n\n // Safety: This can't happen. For renders before the initial commit, preCommitItem\n // is non-null. During the initial commit, we assign itemCleanupPairRef.current,\n // so during subsequent renders, itemCleanupPairRef.current is non-null.\n throw new Error(\n 'returnedItem was unexpectedly null. This indicates a bug in react-disposable-state.',\n );\n}\n"],"mappings":";;;;;;;;;;;;;AAiBA,SAAgB,uBACd,aAGA;CACA,MAAM,qBAAqB,OAAkC,KAAK;CAElE,MAAM,gBAAgB,kCACpB,cACC,SAAS;AACR,qBAAmB,UAAU,IAAI;AACjC,qBAAmB,UAAU;GAEhC;AAED,iBAAgB;AACd,eAAa;GACX,MAAM,YAAY,mBAAmB,UAAU;AAE/C,OAAI,aAAa,KACf,OAAM,IAAI,MACR,+EACD;AAEH,UAAO,WAAW;;IAEnB,EAAE,CAAC;CAEN,MAAM,eAAe,eAAe,SAAS,mBAAmB,UAAU;AAE1E,KAAI,gBAAgB,KAClB,QAAO,EAAE,OAAO,cAAc;AAMhC,OAAM,IAAI,MACR,sFACD"}
1
+ {"version":3,"file":"useLazyDisposableState.mjs","names":[],"sources":["../src/useLazyDisposableState.ts"],"sourcesContent":["'use strict';\n\nimport type { ItemCleanupPair } from '@isograph/disposable-types';\nimport { useEffect, useRef } from 'react';\nimport type { ParentCache } from './ParentCache';\nimport { useCachedResponsivePrecommitValue } from './useCachedResponsivePrecommitValue';\nimport { type UnassignedState } from './useUpdatableDisposableState';\n\n/**\n * useLazyDisposableState<T>\n * - Takes a mutable parent cache and a factory function\n * - Returns { state: T }\n *\n * This lazily loads the disposable item using useCachedResponsivePrecommitValue, then\n * (on commit) sets it in state. The item continues to be returned after\n * commit and is disposed when the hook unmounts.\n */\nexport function useLazyDisposableState<T>(\n parentCache: ParentCache<Exclude<T, UnassignedState>>,\n): {\n state: T;\n} {\n const itemCleanupPairRef = useRef<ItemCleanupPair<T> | null>(null);\n const preCommitItem = useCachedResponsivePrecommitValue(\n parentCache,\n (pair) => {\n itemCleanupPairRef.current = pair;\n },\n );\n\n useEffect(() => {\n return () => {\n const cleanupFn = itemCleanupPairRef.current?.[1];\n // TODO confirm useEffect is called in order.\n if (cleanupFn == null) {\n throw new Error(\n 'cleanupFn unexpectedly null. This indicates a bug in react-disposable-state.',\n );\n }\n cleanupFn();\n itemCleanupPairRef.current = null;\n };\n }, [parentCache]);\n\n const returnedItem = preCommitItem?.state ?? itemCleanupPairRef.current?.[0];\n\n if (returnedItem != null) {\n return { state: returnedItem };\n }\n\n // Safety: This can't happen. For renders before the initial commit, preCommitItem\n // is non-null. During the initial commit, we assign itemCleanupPairRef.current,\n // so during subsequent renders, itemCleanupPairRef.current is non-null.\n throw new Error(\n 'returnedItem was unexpectedly null. This indicates a bug in react-disposable-state.',\n );\n}\n"],"mappings":";;;;;;;;;;;;;AAiBA,SAAgB,uBACd,aAGA;CACA,MAAM,qBAAqB,OAAkC,KAAK;CAClE,MAAM,gBAAgB,kCACpB,cACC,SAAS;AACR,qBAAmB,UAAU;GAEhC;AAED,iBAAgB;AACd,eAAa;GACX,MAAM,YAAY,mBAAmB,UAAU;AAE/C,OAAI,aAAa,KACf,OAAM,IAAI,MACR,+EACD;AAEH,cAAW;AACX,sBAAmB,UAAU;;IAE9B,CAAC,YAAY,CAAC;CAEjB,MAAM,eAAe,eAAe,SAAS,mBAAmB,UAAU;AAE1E,KAAI,gBAAgB,KAClB,QAAO,EAAE,OAAO,cAAc;AAMhC,OAAM,IAAI,MACR,sFACD"}
@@ -0,0 +1,36 @@
1
+ const require_useUpdatableDisposableState = require('./useUpdatableDisposableState.cjs');
2
+ let react = require("react");
3
+
4
+ //#region src/useUpdatableDisposableClearableState.ts
5
+ /**
6
+ * The item that `clearState` puts in the underlying state. It is never returned
7
+ * from this hook; the hook reports UNASSIGNED_STATE while it is current.
8
+ */
9
+ const CLEARED_STATE = Symbol();
10
+ function noop() {}
11
+ /**
12
+ * useUpdatableDisposableClearableState
13
+ * - useUpdatableDisposableState, plus clearState.
14
+ * - clearState returns the state to UNASSIGNED_STATE. The item that was in state
15
+ * is disposed on the next commit, exactly as if it had been superseded by
16
+ * setState.
17
+ * - Like setState, clearState throws if called before the initial commit.
18
+ *
19
+ * This is a wrapper around useUpdatableDisposableState: clearing sets a sentinel
20
+ * item with no cleanup, so the underlying hook's commit-time disposal of
21
+ * superseded items disposes the cleared item, and the sentinel is mapped back to
22
+ * UNASSIGNED_STATE on the way out.
23
+ */
24
+ function useUpdatableDisposableClearableState() {
25
+ const { state, setState } = require_useUpdatableDisposableState.useUpdatableDisposableState();
26
+ return {
27
+ clearState: (0, react.useCallback)(() => {
28
+ setState([CLEARED_STATE, noop]);
29
+ }, [setState]),
30
+ setState,
31
+ state: state === CLEARED_STATE ? require_useUpdatableDisposableState.UNASSIGNED_STATE : state
32
+ };
33
+ }
34
+
35
+ //#endregion
36
+ exports.useUpdatableDisposableClearableState = useUpdatableDisposableClearableState;
@@ -0,0 +1,26 @@
1
+ import { UnassignedState } from "./useUpdatableDisposableState.cjs";
2
+ import { ItemCleanupPair } from "@isograph/disposable-types";
3
+
4
+ //#region src/useUpdatableDisposableClearableState.d.ts
5
+ type UseUpdatableDisposableClearableStateReturnValue<T> = {
6
+ state: T | UnassignedState;
7
+ setState: (pair: ItemCleanupPair<Exclude<T, UnassignedState>>) => void;
8
+ clearState: () => void;
9
+ };
10
+ /**
11
+ * useUpdatableDisposableClearableState
12
+ * - useUpdatableDisposableState, plus clearState.
13
+ * - clearState returns the state to UNASSIGNED_STATE. The item that was in state
14
+ * is disposed on the next commit, exactly as if it had been superseded by
15
+ * setState.
16
+ * - Like setState, clearState throws if called before the initial commit.
17
+ *
18
+ * This is a wrapper around useUpdatableDisposableState: clearing sets a sentinel
19
+ * item with no cleanup, so the underlying hook's commit-time disposal of
20
+ * superseded items disposes the cleared item, and the sentinel is mapped back to
21
+ * UNASSIGNED_STATE on the way out.
22
+ */
23
+ declare function useUpdatableDisposableClearableState<T = never>(): UseUpdatableDisposableClearableStateReturnValue<T>;
24
+ //#endregion
25
+ export { useUpdatableDisposableClearableState };
26
+ //# sourceMappingURL=useUpdatableDisposableClearableState.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useUpdatableDisposableClearableState.d.cts","names":[],"sources":["../src/useUpdatableDisposableClearableState.ts"],"mappings":";;;;KAQK,+CAAA;EACH,KAAA,EAAO,CAAA,GAAI,eAAA;EACX,QAAA,GAAW,IAAA,EAAM,eAAA,CAAgB,OAAA,CAAQ,CAAA,EAAG,eAAA;EAC5C,UAAA;AAAA;;;;;;;;;;;;;;iBAyBc,oCAAA,WAAA,CAAA,GAEX,+CAAA,CAAgD,CAAA"}
@@ -0,0 +1,26 @@
1
+ import { UnassignedState } from "./useUpdatableDisposableState.mjs";
2
+ import { ItemCleanupPair } from "@isograph/disposable-types";
3
+
4
+ //#region src/useUpdatableDisposableClearableState.d.ts
5
+ type UseUpdatableDisposableClearableStateReturnValue<T> = {
6
+ state: T | UnassignedState;
7
+ setState: (pair: ItemCleanupPair<Exclude<T, UnassignedState>>) => void;
8
+ clearState: () => void;
9
+ };
10
+ /**
11
+ * useUpdatableDisposableClearableState
12
+ * - useUpdatableDisposableState, plus clearState.
13
+ * - clearState returns the state to UNASSIGNED_STATE. The item that was in state
14
+ * is disposed on the next commit, exactly as if it had been superseded by
15
+ * setState.
16
+ * - Like setState, clearState throws if called before the initial commit.
17
+ *
18
+ * This is a wrapper around useUpdatableDisposableState: clearing sets a sentinel
19
+ * item with no cleanup, so the underlying hook's commit-time disposal of
20
+ * superseded items disposes the cleared item, and the sentinel is mapped back to
21
+ * UNASSIGNED_STATE on the way out.
22
+ */
23
+ declare function useUpdatableDisposableClearableState<T = never>(): UseUpdatableDisposableClearableStateReturnValue<T>;
24
+ //#endregion
25
+ export { useUpdatableDisposableClearableState };
26
+ //# sourceMappingURL=useUpdatableDisposableClearableState.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useUpdatableDisposableClearableState.d.mts","names":[],"sources":["../src/useUpdatableDisposableClearableState.ts"],"mappings":";;;;KAQK,+CAAA;EACH,KAAA,EAAO,CAAA,GAAI,eAAA;EACX,QAAA,GAAW,IAAA,EAAM,eAAA,CAAgB,OAAA,CAAQ,CAAA,EAAG,eAAA;EAC5C,UAAA;AAAA;;;;;;;;;;;;;;iBAyBc,oCAAA,WAAA,CAAA,GAEX,+CAAA,CAAgD,CAAA"}