@isograph/react-disposable-state 0.0.4 → 0.1.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 +18 -14
- package/dist/CacheItem.js +65 -61
- package/dist/ParentCache.d.ts +2 -2
- package/dist/ParentCache.js +1 -1
- package/dist/index.d.ts +8 -8
- package/dist/useCachedPrecommitValue.d.ts +2 -2
- package/dist/useCachedPrecommitValue.js +1 -1
- package/dist/useDisposableState.d.ts +3 -3
- package/dist/useDisposableState.js +6 -4
- package/dist/useHasCommittedRef.d.ts +1 -1
- package/dist/useLazyDisposableState.d.ts +1 -1
- package/dist/useLazyDisposableState.js +3 -3
- package/dist/useUpdatableDisposableState.d.ts +1 -1
- package/dist/useUpdatableDisposableState.js +3 -3
- package/docs/managing-complex-state.md +4 -4
- package/package.json +7 -2
- package/src/CacheItem.test.ts +94 -94
- package/src/CacheItem.ts +80 -76
- package/src/ParentCache.test.ts +31 -27
- package/src/ParentCache.ts +3 -3
- package/src/index.ts +8 -8
- package/src/useCachedPrecommitValue.test.tsx +57 -73
- package/src/useCachedPrecommitValue.ts +7 -7
- package/src/useDisposableState.ts +13 -11
- package/src/useHasCommittedRef.ts +1 -1
- package/src/useLazyDisposableState.ts +7 -7
- package/src/useUpdatableDisposableState.test.tsx +334 -327
- package/src/useUpdatableDisposableState.ts +9 -9
@@ -1,6 +1,6 @@
|
|
1
|
-
import { ItemCleanupPair } from
|
2
|
-
import { useCallback, useEffect, useRef, useState } from
|
3
|
-
import { useHasCommittedRef } from
|
1
|
+
import { ItemCleanupPair } from '@isograph/disposable-types';
|
2
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
3
|
+
import { useHasCommittedRef } from './useHasCommittedRef';
|
4
4
|
|
5
5
|
export const UNASSIGNED_STATE = Symbol();
|
6
6
|
export type UnassignedState = typeof UNASSIGNED_STATE;
|
@@ -59,7 +59,7 @@ type ICI<T> = { item: T; cleanup: () => void; index: number };
|
|
59
59
|
* - This may only work in concurrent mode, though.
|
60
60
|
*/
|
61
61
|
export function useUpdatableDisposableState<
|
62
|
-
T = never
|
62
|
+
T = never,
|
63
63
|
>(): UseUpdatableDisposableStateReturnValue<T> {
|
64
64
|
const hasCommittedRef = useHasCommittedRef();
|
65
65
|
|
@@ -67,14 +67,14 @@ export function useUpdatableDisposableState<
|
|
67
67
|
const setStateCountRef = useRef(0);
|
68
68
|
|
69
69
|
const [stateICI, setStateICI] = useState<ICI<T> | UnassignedState>(
|
70
|
-
UNASSIGNED_STATE
|
70
|
+
UNASSIGNED_STATE,
|
71
71
|
);
|
72
72
|
|
73
73
|
const setStateAfterCommit = useCallback(
|
74
74
|
(itemCleanupPair: ItemCleanupPair<T>) => {
|
75
75
|
if (!hasCommittedRef.current) {
|
76
76
|
throw new Error(
|
77
|
-
|
77
|
+
'Calling setState before the component has committed is unsafe and disallowed.',
|
78
78
|
);
|
79
79
|
}
|
80
80
|
|
@@ -87,7 +87,7 @@ export function useUpdatableDisposableState<
|
|
87
87
|
undisposedICIs.current.add(ici);
|
88
88
|
setStateICI(ici);
|
89
89
|
},
|
90
|
-
[setStateICI]
|
90
|
+
[setStateICI],
|
91
91
|
);
|
92
92
|
|
93
93
|
useEffect(function cleanupUnreachableItems() {
|
@@ -126,9 +126,9 @@ function tsTests() {
|
|
126
126
|
// @ts-expect-error
|
127
127
|
a.setState([UNASSIGNED_STATE, () => {}]);
|
128
128
|
// @ts-expect-error
|
129
|
-
a.setState([
|
129
|
+
a.setState(['asdf', () => {}]);
|
130
130
|
const b = useUpdatableDisposableState<string | UnassignedState>();
|
131
131
|
// @ts-expect-error
|
132
132
|
b.setState([UNASSIGNED_STATE, () => {}]);
|
133
|
-
b.setState([
|
133
|
+
b.setState(['asdf', () => {}]);
|
134
134
|
}
|