@isograph/react-disposable-state 0.0.4 → 0.1.1
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 +8 -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 +426 -436
- 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,9 +1,9 @@
|
|
1
|
-
|
1
|
+
'use strict';
|
2
2
|
|
3
|
-
import { useEffect, useState } from
|
4
|
-
import { ParentCache } from
|
5
|
-
import { useHasCommittedRef } from
|
6
|
-
import { ItemCleanupPair } from
|
3
|
+
import { useEffect, useState } from 'react';
|
4
|
+
import { ParentCache } from './ParentCache';
|
5
|
+
import { useHasCommittedRef } from './useHasCommittedRef';
|
6
|
+
import { ItemCleanupPair } from '@isograph/isograph-disposable-types/dist';
|
7
7
|
|
8
8
|
/**
|
9
9
|
* usePrecommitValue<T>
|
@@ -38,7 +38,7 @@ import { ItemCleanupPair } from "@isograph/isograph-disposable-types/dist";
|
|
38
38
|
*/
|
39
39
|
export function useCachedPrecommitValue<T>(
|
40
40
|
parentCache: ParentCache<T>,
|
41
|
-
onCommit: (pair: ItemCleanupPair<T>) => void
|
41
|
+
onCommit: (pair: ItemCleanupPair<T>) => void,
|
42
42
|
): { state: T } | null {
|
43
43
|
// TODO: there should be two APIs. One in which we always re-render if the
|
44
44
|
// committed item was not returned during the last render, and one in which
|
@@ -66,7 +66,7 @@ export function useCachedPrecommitValue<T>(
|
|
66
66
|
// After the above, we have a non-disposed item and a cleanup function, which we
|
67
67
|
// can pass to onCommit.
|
68
68
|
const undisposedPair = cacheItem.permanentRetainIfNotDisposed(
|
69
|
-
disposeOfTemporaryRetain
|
69
|
+
disposeOfTemporaryRetain,
|
70
70
|
);
|
71
71
|
if (undisposedPair !== null) {
|
72
72
|
onCommit(undisposedPair);
|
@@ -1,12 +1,12 @@
|
|
1
|
-
import { useEffect, useRef } from
|
2
|
-
import { ParentCache } from
|
3
|
-
import { ItemCleanupPair } from
|
4
|
-
import { useCachedPrecommitValue } from
|
1
|
+
import { useEffect, useRef } from 'react';
|
2
|
+
import { ParentCache } from './ParentCache';
|
3
|
+
import { ItemCleanupPair } from '@isograph/disposable-types';
|
4
|
+
import { useCachedPrecommitValue } from './useCachedPrecommitValue';
|
5
5
|
import {
|
6
6
|
UNASSIGNED_STATE,
|
7
7
|
UnassignedState,
|
8
8
|
useUpdatableDisposableState,
|
9
|
-
} from
|
9
|
+
} from './useUpdatableDisposableState';
|
10
10
|
|
11
11
|
type UseUpdatableDisposableStateReturnValue<T> = {
|
12
12
|
state: T;
|
@@ -14,7 +14,7 @@ type UseUpdatableDisposableStateReturnValue<T> = {
|
|
14
14
|
};
|
15
15
|
|
16
16
|
export function useDisposableState<T = never>(
|
17
|
-
parentCache: ParentCache<T
|
17
|
+
parentCache: ParentCache<T>,
|
18
18
|
): UseUpdatableDisposableStateReturnValue<T> {
|
19
19
|
const itemCleanupPairRef = useRef<ItemCleanupPair<T> | null>(null);
|
20
20
|
|
@@ -33,13 +33,13 @@ export function useDisposableState<T = never>(
|
|
33
33
|
itemCleanupPairRef.current = null;
|
34
34
|
} else {
|
35
35
|
throw new Error(
|
36
|
-
|
37
|
-
|
36
|
+
'itemCleanupPairRef.current is unexpectedly null. ' +
|
37
|
+
'This indicates a bug in react-disposable-state.',
|
38
38
|
);
|
39
39
|
}
|
40
40
|
}
|
41
41
|
},
|
42
|
-
[stateFromDisposableStateHook]
|
42
|
+
[stateFromDisposableStateHook],
|
43
43
|
);
|
44
44
|
|
45
45
|
useEffect(function cleanupItemCleanupPairRefIfSetStateNotCalled() {
|
@@ -81,12 +81,14 @@ export function useDisposableState<T = never>(
|
|
81
81
|
function tsTests() {
|
82
82
|
let x: any;
|
83
83
|
const a = useDisposableState(x);
|
84
|
+
// This should be a compiler error, because the generic is inferred to be of
|
85
|
+
// type never. TODO determine why this doesn't break the build!
|
84
86
|
// @ts-expect-error
|
85
|
-
a.setState([
|
87
|
+
a.setState(['asdf', () => {}]);
|
86
88
|
// @ts-expect-error
|
87
89
|
a.setState([UNASSIGNED_STATE, () => {}]);
|
88
90
|
const b = useDisposableState<string | UnassignedState>(x);
|
89
91
|
// @ts-expect-error
|
90
92
|
b.setState([UNASSIGNED_STATE, () => {}]);
|
91
|
-
b.setState([
|
93
|
+
b.setState(['asdf', () => {}]);
|
92
94
|
}
|
@@ -1,9 +1,9 @@
|
|
1
|
-
|
1
|
+
'use strict';
|
2
2
|
|
3
|
-
import { useEffect, useRef } from
|
4
|
-
import type { ItemCleanupPair } from
|
5
|
-
import { ParentCache } from
|
6
|
-
import { useCachedPrecommitValue } from
|
3
|
+
import { useEffect, useRef } from 'react';
|
4
|
+
import type { ItemCleanupPair } from '@isograph/disposable-types';
|
5
|
+
import { ParentCache } from './ParentCache';
|
6
|
+
import { useCachedPrecommitValue } from './useCachedPrecommitValue';
|
7
7
|
|
8
8
|
/**
|
9
9
|
* useLazyDisposableState<T>
|
@@ -28,7 +28,7 @@ export function useLazyDisposableState<T>(parentCache: ParentCache<T>): {
|
|
28
28
|
// TODO confirm useEffect is called in order.
|
29
29
|
if (cleanupFn == null) {
|
30
30
|
throw new Error(
|
31
|
-
|
31
|
+
'cleanupFn unexpectedly null. This indicates a bug in react-disposable-state.',
|
32
32
|
);
|
33
33
|
}
|
34
34
|
return cleanupFn;
|
@@ -43,6 +43,6 @@ export function useLazyDisposableState<T>(parentCache: ParentCache<T>): {
|
|
43
43
|
// is non-null. During the initial commit, we assign itemCleanupPairRef.current,
|
44
44
|
// so during subsequent renders, itemCleanupPairRef.current is non-null.
|
45
45
|
throw new Error(
|
46
|
-
|
46
|
+
'returnedItem was unexpectedly null. This indicates a bug in react-disposable-state.',
|
47
47
|
);
|
48
48
|
}
|