@isograph/react-disposable-state 0.0.0-main-1cd3db6d → 0.0.0-main-2c275831
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 +4 -4
- package/dist/CacheItem.js +51 -51
- package/dist/ParentCache.d.ts +2 -2
- 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 +5 -7
- 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/package.json +2 -2
- package/src/CacheItem.test.ts +184 -222
- package/src/CacheItem.ts +64 -81
- package/src/ParentCache.test.ts +9 -10
- package/src/ParentCache.ts +7 -9
- package/src/index.ts +8 -8
- package/src/useCachedPrecommitValue.test.tsx +47 -77
- package/src/useCachedPrecommitValue.ts +9 -13
- package/src/useDisposableState.ts +13 -16
- package/src/useHasCommittedRef.ts +1 -1
- package/src/useLazyDisposableState.ts +7 -7
- package/src/useUpdatableDisposableState.test.tsx +24 -33
- package/src/useUpdatableDisposableState.ts +9 -11
@@ -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,22 +59,20 @@ 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
|
|
66
66
|
const undisposedICIs = useRef(new Set<ICI<T>>());
|
67
67
|
const setStateCountRef = useRef(0);
|
68
68
|
|
69
|
-
const [stateICI, setStateICI] = useState<ICI<T> | UnassignedState>(
|
70
|
-
UNASSIGNED_STATE
|
71
|
-
);
|
69
|
+
const [stateICI, setStateICI] = useState<ICI<T> | UnassignedState>(UNASSIGNED_STATE);
|
72
70
|
|
73
71
|
const setStateAfterCommit = useCallback(
|
74
72
|
(itemCleanupPair: ItemCleanupPair<T>) => {
|
75
73
|
if (!hasCommittedRef.current) {
|
76
74
|
throw new Error(
|
77
|
-
|
75
|
+
'Calling setState before the component has committed is unsafe and disallowed.',
|
78
76
|
);
|
79
77
|
}
|
80
78
|
|
@@ -87,7 +85,7 @@ export function useUpdatableDisposableState<
|
|
87
85
|
undisposedICIs.current.add(ici);
|
88
86
|
setStateICI(ici);
|
89
87
|
},
|
90
|
-
[setStateICI]
|
88
|
+
[setStateICI],
|
91
89
|
);
|
92
90
|
|
93
91
|
useEffect(function cleanupUnreachableItems() {
|
@@ -126,9 +124,9 @@ function tsTests() {
|
|
126
124
|
// @ts-expect-error
|
127
125
|
a.setState([UNASSIGNED_STATE, () => {}]);
|
128
126
|
// @ts-expect-error
|
129
|
-
a.setState([
|
127
|
+
a.setState(['asdf', () => {}]);
|
130
128
|
const b = useUpdatableDisposableState<string | UnassignedState>();
|
131
129
|
// @ts-expect-error
|
132
130
|
b.setState([UNASSIGNED_STATE, () => {}]);
|
133
|
-
b.setState([
|
131
|
+
b.setState(['asdf', () => {}]);
|
134
132
|
}
|