@isograph/react-disposable-state 0.0.0-main-1ba744d7 → 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.
- package/.turbo/turbo-compile-libs.log +3 -3
- package/.turbo/turbo-tsc.log +2 -2
- package/README.md +19 -0
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +2 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +2 -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/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/useUpdatableDisposableClearableState.ts +67 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
../.. | WARN Unsupported engine: wanted: {"node":"24.12.0"} (current: {"node":"v24.
|
|
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-
|
|
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
|
[34mℹ[39m tsdown [2mv0.20.1[22m powered by rolldown [2mv1.0.0-rc.1[22m
|
|
7
7
|
[34mℹ[39m config file: [4m/home/runner/work/isograph/isograph/tsdown.config.ts[24m
|
|
8
|
-
(node:
|
|
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)
|
package/.turbo/turbo-tsc.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
../.. | WARN Unsupported engine: wanted: {"node":"24.12.0"} (current: {"node":"v24.
|
|
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-
|
|
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
|
@@ -6,6 +6,7 @@ const require_useHasCommittedRef = require('./useHasCommittedRef.cjs');
|
|
|
6
6
|
const require_useUpdatableDisposableState = require('./useUpdatableDisposableState.cjs');
|
|
7
7
|
const require_useDisposableState = require('./useDisposableState.cjs');
|
|
8
8
|
const require_useLazyDisposableState = require('./useLazyDisposableState.cjs');
|
|
9
|
+
const require_useUpdatableDisposableClearableState = require('./useUpdatableDisposableClearableState.cjs');
|
|
9
10
|
|
|
10
11
|
exports.CacheItem = require_CacheItem.CacheItem;
|
|
11
12
|
exports.ParentCache = require_ParentCache.ParentCache;
|
|
@@ -16,6 +17,7 @@ exports.useDisposableState = require_useDisposableState.useDisposableState;
|
|
|
16
17
|
exports.useEffectsRerunWithoutRenderRef = require_useEffectsRerunWithoutRender.useEffectsRerunWithoutRenderRef;
|
|
17
18
|
exports.useHasCommittedRef = require_useHasCommittedRef.useHasCommittedRef;
|
|
18
19
|
exports.useLazyDisposableState = require_useLazyDisposableState.useLazyDisposableState;
|
|
20
|
+
exports.useUpdatableDisposableClearableState = require_useUpdatableDisposableClearableState.useUpdatableDisposableClearableState;
|
|
19
21
|
exports.useUpdatableDisposableState = require_useUpdatableDisposableState.useUpdatableDisposableState;
|
|
20
22
|
var _isograph_disposable_types = require("@isograph/disposable-types");
|
|
21
23
|
Object.keys(_isograph_disposable_types).forEach(function (k) {
|
package/dist/index.d.cts
CHANGED
|
@@ -6,5 +6,6 @@ import { useDisposableState } from "./useDisposableState.cjs";
|
|
|
6
6
|
import { useEffectsRerunWithoutRenderRef } from "./useEffectsRerunWithoutRender.cjs";
|
|
7
7
|
import { useHasCommittedRef } from "./useHasCommittedRef.cjs";
|
|
8
8
|
import { useLazyDisposableState } from "./useLazyDisposableState.cjs";
|
|
9
|
+
import { useUpdatableDisposableClearableState } from "./useUpdatableDisposableClearableState.cjs";
|
|
9
10
|
export * from "@isograph/disposable-types";
|
|
10
|
-
export { CacheItem, CacheItemOptions, CacheItemState, InParentCacheAndNotDisposed, NotInParentCacheAndDisposed, NotInParentCacheAndNotDisposed, ParentCache, UNASSIGNED_STATE, UnassignedState, createTemporarilyRetainedCacheItem, useCachedResponsivePrecommitValue, useDisposableState, useEffectsRerunWithoutRenderRef, 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
|
@@ -6,5 +6,6 @@ import { useDisposableState } from "./useDisposableState.mjs";
|
|
|
6
6
|
import { useEffectsRerunWithoutRenderRef } from "./useEffectsRerunWithoutRender.mjs";
|
|
7
7
|
import { useHasCommittedRef } from "./useHasCommittedRef.mjs";
|
|
8
8
|
import { useLazyDisposableState } from "./useLazyDisposableState.mjs";
|
|
9
|
+
import { useUpdatableDisposableClearableState } from "./useUpdatableDisposableClearableState.mjs";
|
|
9
10
|
export * from "@isograph/disposable-types";
|
|
10
|
-
export { CacheItem, CacheItemOptions, CacheItemState, InParentCacheAndNotDisposed, NotInParentCacheAndDisposed, NotInParentCacheAndNotDisposed, ParentCache, UNASSIGNED_STATE, UnassignedState, createTemporarilyRetainedCacheItem, useCachedResponsivePrecommitValue, useDisposableState, useEffectsRerunWithoutRenderRef, 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
|
@@ -6,7 +6,8 @@ import { useHasCommittedRef } from "./useHasCommittedRef.mjs";
|
|
|
6
6
|
import { UNASSIGNED_STATE, useUpdatableDisposableState } from "./useUpdatableDisposableState.mjs";
|
|
7
7
|
import { useDisposableState } from "./useDisposableState.mjs";
|
|
8
8
|
import { useLazyDisposableState } from "./useLazyDisposableState.mjs";
|
|
9
|
+
import { useUpdatableDisposableClearableState } from "./useUpdatableDisposableClearableState.mjs";
|
|
9
10
|
|
|
10
11
|
export * from "@isograph/disposable-types"
|
|
11
12
|
|
|
12
|
-
export { CacheItem, ParentCache, UNASSIGNED_STATE, createTemporarilyRetainedCacheItem, useCachedResponsivePrecommitValue, useDisposableState, useEffectsRerunWithoutRenderRef, useHasCommittedRef, useLazyDisposableState, useUpdatableDisposableState };
|
|
13
|
+
export { CacheItem, ParentCache, UNASSIGNED_STATE, createTemporarilyRetainedCacheItem, useCachedResponsivePrecommitValue, useDisposableState, useEffectsRerunWithoutRenderRef, useHasCommittedRef, useLazyDisposableState, useUpdatableDisposableClearableState, useUpdatableDisposableState };
|
|
@@ -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"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { UNASSIGNED_STATE, useUpdatableDisposableState } from "./useUpdatableDisposableState.mjs";
|
|
2
|
+
import { useCallback } from "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 } = useUpdatableDisposableState();
|
|
26
|
+
return {
|
|
27
|
+
clearState: useCallback(() => {
|
|
28
|
+
setState([CLEARED_STATE, noop]);
|
|
29
|
+
}, [setState]),
|
|
30
|
+
setState,
|
|
31
|
+
state: state === CLEARED_STATE ? UNASSIGNED_STATE : state
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
//#endregion
|
|
36
|
+
export { useUpdatableDisposableClearableState };
|
|
37
|
+
//# sourceMappingURL=useUpdatableDisposableClearableState.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useUpdatableDisposableClearableState.mjs","names":[],"sources":["../src/useUpdatableDisposableClearableState.ts"],"sourcesContent":["import type { ItemCleanupPair } from '@isograph/disposable-types';\nimport { useCallback } from 'react';\nimport {\n UNASSIGNED_STATE,\n type UnassignedState,\n useUpdatableDisposableState,\n} from './useUpdatableDisposableState';\n\ntype UseUpdatableDisposableClearableStateReturnValue<T> = {\n state: T | UnassignedState;\n setState: (pair: ItemCleanupPair<Exclude<T, UnassignedState>>) => void;\n clearState: () => void;\n};\n\n/**\n * The item that `clearState` puts in the underlying state. It is never returned\n * from this hook; the hook reports UNASSIGNED_STATE while it is current.\n */\nconst CLEARED_STATE: unique symbol = Symbol();\ntype ClearedState = typeof CLEARED_STATE;\n\nfunction noop() {}\n\n/**\n * useUpdatableDisposableClearableState\n * - useUpdatableDisposableState, plus clearState.\n * - clearState returns the state to UNASSIGNED_STATE. The item that was in state\n * is disposed on the next commit, exactly as if it had been superseded by\n * setState.\n * - Like setState, clearState throws if called before the initial commit.\n *\n * This is a wrapper around useUpdatableDisposableState: clearing sets a sentinel\n * item with no cleanup, so the underlying hook's commit-time disposal of\n * superseded items disposes the cleared item, and the sentinel is mapped back to\n * UNASSIGNED_STATE on the way out.\n */\nexport function useUpdatableDisposableClearableState<\n T = never,\n>(): UseUpdatableDisposableClearableStateReturnValue<T> {\n const { state, setState } = useUpdatableDisposableState<T | ClearedState>();\n\n const clearState = useCallback(() => {\n setState([CLEARED_STATE, noop]);\n }, [setState]);\n\n return {\n clearState,\n setState,\n state: state === CLEARED_STATE ? UNASSIGNED_STATE : state,\n };\n}\n\n// @ts-ignore\nfunction tsTests() {\n const a = useUpdatableDisposableClearableState();\n // @ts-expect-error\n a.setState([UNASSIGNED_STATE, () => {}]);\n // @ts-expect-error\n a.setState(['asdf', () => {}]);\n const b = useUpdatableDisposableClearableState<string | UnassignedState>();\n // @ts-expect-error\n b.setState([UNASSIGNED_STATE, () => {}]);\n b.setState(['asdf', () => {}]);\n // The cleared sentinel is not part of T, so it cannot be set from outside.\n // @ts-expect-error\n b.setState([CLEARED_STATE, () => {}]);\n}\n"],"mappings":";;;;;;;;AAkBA,MAAM,gBAA+B,QAAQ;AAG7C,SAAS,OAAO;;;;;;;;;;;;;;AAehB,SAAgB,uCAEwC;CACtD,MAAM,EAAE,OAAO,aAAa,6BAA+C;AAM3E,QAAO;EACL,YALiB,kBAAkB;AACnC,YAAS,CAAC,eAAe,KAAK,CAAC;KAC9B,CAAC,SAAS,CAAC;EAIZ;EACA,OAAO,UAAU,gBAAgB,mBAAmB;EACrD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@isograph/react-disposable-state",
|
|
3
|
-
"version": "0.0.0-main-
|
|
3
|
+
"version": "0.0.0-main-082842ca",
|
|
4
4
|
"description": "Primitives for managing disposable state in React",
|
|
5
5
|
"homepage": "https://isograph.dev",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"author": "Isograph Labs",
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@isograph/disposable-types": "0.0.0-main-
|
|
11
|
+
"@isograph/disposable-types": "0.0.0-main-082842ca"
|
|
12
12
|
},
|
|
13
13
|
"peerDependencies": {
|
|
14
14
|
"react": "^18.0.0 || ^19.0.0"
|
package/src/index.ts
CHANGED
|
@@ -7,4 +7,5 @@ export * from './useDisposableState';
|
|
|
7
7
|
export * from './useEffectsRerunWithoutRender';
|
|
8
8
|
export * from './useHasCommittedRef';
|
|
9
9
|
export * from './useLazyDisposableState';
|
|
10
|
+
export * from './useUpdatableDisposableClearableState';
|
|
10
11
|
export * from './useUpdatableDisposableState';
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { ItemCleanupPair } from '@isograph/disposable-types';
|
|
2
|
+
import { useCallback } from 'react';
|
|
3
|
+
import {
|
|
4
|
+
UNASSIGNED_STATE,
|
|
5
|
+
type UnassignedState,
|
|
6
|
+
useUpdatableDisposableState,
|
|
7
|
+
} from './useUpdatableDisposableState';
|
|
8
|
+
|
|
9
|
+
type UseUpdatableDisposableClearableStateReturnValue<T> = {
|
|
10
|
+
state: T | UnassignedState;
|
|
11
|
+
setState: (pair: ItemCleanupPair<Exclude<T, UnassignedState>>) => void;
|
|
12
|
+
clearState: () => void;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The item that `clearState` puts in the underlying state. It is never returned
|
|
17
|
+
* from this hook; the hook reports UNASSIGNED_STATE while it is current.
|
|
18
|
+
*/
|
|
19
|
+
const CLEARED_STATE: unique symbol = Symbol();
|
|
20
|
+
type ClearedState = typeof CLEARED_STATE;
|
|
21
|
+
|
|
22
|
+
function noop() {}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* useUpdatableDisposableClearableState
|
|
26
|
+
* - useUpdatableDisposableState, plus clearState.
|
|
27
|
+
* - clearState returns the state to UNASSIGNED_STATE. The item that was in state
|
|
28
|
+
* is disposed on the next commit, exactly as if it had been superseded by
|
|
29
|
+
* setState.
|
|
30
|
+
* - Like setState, clearState throws if called before the initial commit.
|
|
31
|
+
*
|
|
32
|
+
* This is a wrapper around useUpdatableDisposableState: clearing sets a sentinel
|
|
33
|
+
* item with no cleanup, so the underlying hook's commit-time disposal of
|
|
34
|
+
* superseded items disposes the cleared item, and the sentinel is mapped back to
|
|
35
|
+
* UNASSIGNED_STATE on the way out.
|
|
36
|
+
*/
|
|
37
|
+
export function useUpdatableDisposableClearableState<
|
|
38
|
+
T = never,
|
|
39
|
+
>(): UseUpdatableDisposableClearableStateReturnValue<T> {
|
|
40
|
+
const { state, setState } = useUpdatableDisposableState<T | ClearedState>();
|
|
41
|
+
|
|
42
|
+
const clearState = useCallback(() => {
|
|
43
|
+
setState([CLEARED_STATE, noop]);
|
|
44
|
+
}, [setState]);
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
clearState,
|
|
48
|
+
setState,
|
|
49
|
+
state: state === CLEARED_STATE ? UNASSIGNED_STATE : state,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// @ts-ignore
|
|
54
|
+
function tsTests() {
|
|
55
|
+
const a = useUpdatableDisposableClearableState();
|
|
56
|
+
// @ts-expect-error
|
|
57
|
+
a.setState([UNASSIGNED_STATE, () => {}]);
|
|
58
|
+
// @ts-expect-error
|
|
59
|
+
a.setState(['asdf', () => {}]);
|
|
60
|
+
const b = useUpdatableDisposableClearableState<string | UnassignedState>();
|
|
61
|
+
// @ts-expect-error
|
|
62
|
+
b.setState([UNASSIGNED_STATE, () => {}]);
|
|
63
|
+
b.setState(['asdf', () => {}]);
|
|
64
|
+
// The cleared sentinel is not part of T, so it cannot be set from outside.
|
|
65
|
+
// @ts-expect-error
|
|
66
|
+
b.setState([CLEARED_STATE, () => {}]);
|
|
67
|
+
}
|