@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
package/dist/CacheItem.d.ts
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
import { CleanupFn, Factory, ItemCleanupPair } from
|
1
|
+
import { CleanupFn, Factory, ItemCleanupPair } from '@isograph/disposable-types';
|
2
2
|
export type NotInParentCacheAndDisposed = {
|
3
|
-
kind:
|
3
|
+
kind: 'NotInParentCacheAndDisposed';
|
4
4
|
};
|
5
5
|
export type NotInParentCacheAndNotDisposed<T> = {
|
6
|
-
kind:
|
6
|
+
kind: 'NotInParentCacheAndNotDisposed';
|
7
7
|
value: T;
|
8
8
|
disposeValue: () => void;
|
9
9
|
permanentRetainCount: number;
|
10
10
|
};
|
11
11
|
export type InParentCacheAndNotDisposed<T> = {
|
12
|
-
kind:
|
12
|
+
kind: 'InParentCacheAndNotDisposed';
|
13
13
|
value: T;
|
14
14
|
disposeValue: () => void;
|
15
15
|
removeFromParentCache: () => void;
|
package/dist/CacheItem.js
CHANGED
@@ -34,7 +34,7 @@ class CacheItem {
|
|
34
34
|
this.__options = options !== null && options !== void 0 ? options : null;
|
35
35
|
const [value, disposeValue] = factory();
|
36
36
|
this.__state = {
|
37
|
-
kind:
|
37
|
+
kind: 'InParentCacheAndNotDisposed',
|
38
38
|
value,
|
39
39
|
disposeValue,
|
40
40
|
removeFromParentCache,
|
@@ -46,21 +46,21 @@ class CacheItem {
|
|
46
46
|
}
|
47
47
|
getValue() {
|
48
48
|
switch (this.__state.kind) {
|
49
|
-
case
|
49
|
+
case 'InParentCacheAndNotDisposed': {
|
50
50
|
return this.__state.value;
|
51
51
|
}
|
52
|
-
case
|
52
|
+
case 'NotInParentCacheAndNotDisposed': {
|
53
53
|
return this.__state.value;
|
54
54
|
}
|
55
55
|
default: {
|
56
|
-
throw new Error(
|
57
|
-
|
56
|
+
throw new Error('Attempted to access disposed value from CacheItem. ' +
|
57
|
+
'This indicates a bug in react-disposable-state.');
|
58
58
|
}
|
59
59
|
}
|
60
60
|
}
|
61
61
|
permanentRetainIfNotDisposed(disposeOfTemporaryRetain) {
|
62
62
|
switch (this.__state.kind) {
|
63
|
-
case
|
63
|
+
case 'InParentCacheAndNotDisposed': {
|
64
64
|
let cleared = false;
|
65
65
|
this.__state.permanentRetainCount++;
|
66
66
|
disposeOfTemporaryRetain();
|
@@ -68,30 +68,30 @@ class CacheItem {
|
|
68
68
|
this.__state.value,
|
69
69
|
() => {
|
70
70
|
if (cleared) {
|
71
|
-
throw new Error(
|
72
|
-
|
71
|
+
throw new Error('A permanent retain should only be cleared once. ' +
|
72
|
+
'This indicates a bug in react-disposable-state.');
|
73
73
|
}
|
74
74
|
cleared = true;
|
75
75
|
switch (this.__state.kind) {
|
76
|
-
case
|
76
|
+
case 'InParentCacheAndNotDisposed': {
|
77
77
|
this.__state.permanentRetainCount--;
|
78
78
|
this.__maybeExitInParentCacheAndNotDisposedState(this.__state);
|
79
79
|
return;
|
80
80
|
}
|
81
|
-
case
|
81
|
+
case 'NotInParentCacheAndNotDisposed': {
|
82
82
|
this.__state.permanentRetainCount--;
|
83
83
|
this.__maybeExitNotInParentCacheAndNotDisposedState(this.__state);
|
84
84
|
return;
|
85
85
|
}
|
86
86
|
default: {
|
87
|
-
throw new Error(
|
88
|
-
|
87
|
+
throw new Error('CacheItem was in a disposed state, but there existed a permanent retain. ' +
|
88
|
+
'This indicates a bug in react-disposable-state.');
|
89
89
|
}
|
90
90
|
}
|
91
91
|
},
|
92
92
|
];
|
93
93
|
}
|
94
|
-
case
|
94
|
+
case 'NotInParentCacheAndNotDisposed': {
|
95
95
|
let cleared = false;
|
96
96
|
this.__state.permanentRetainCount++;
|
97
97
|
disposeOfTemporaryRetain();
|
@@ -99,19 +99,19 @@ class CacheItem {
|
|
99
99
|
this.__state.value,
|
100
100
|
() => {
|
101
101
|
if (cleared) {
|
102
|
-
throw new Error(
|
103
|
-
|
102
|
+
throw new Error('A permanent retain should only be cleared once. ' +
|
103
|
+
'This indicates a bug in react-disposable-state.');
|
104
104
|
}
|
105
105
|
cleared = true;
|
106
106
|
switch (this.__state.kind) {
|
107
|
-
case
|
107
|
+
case 'NotInParentCacheAndNotDisposed': {
|
108
108
|
this.__state.permanentRetainCount--;
|
109
109
|
this.__maybeExitNotInParentCacheAndNotDisposedState(this.__state);
|
110
110
|
return;
|
111
111
|
}
|
112
112
|
default: {
|
113
|
-
throw new Error(
|
114
|
-
|
113
|
+
throw new Error('CacheItem was in an unexpected state. ' +
|
114
|
+
'This indicates a bug in react-disposable-state.');
|
115
115
|
}
|
116
116
|
}
|
117
117
|
},
|
@@ -126,40 +126,40 @@ class CacheItem {
|
|
126
126
|
temporaryRetain() {
|
127
127
|
var _a, _b;
|
128
128
|
switch (this.__state.kind) {
|
129
|
-
case
|
130
|
-
let status =
|
129
|
+
case 'InParentCacheAndNotDisposed': {
|
130
|
+
let status = 'Uncleared';
|
131
131
|
this.__state.temporaryRetainCount++;
|
132
132
|
const clearTemporaryRetainByCallack = () => {
|
133
|
-
if (status ===
|
134
|
-
throw new Error(
|
135
|
-
|
133
|
+
if (status === 'ClearedByCallback') {
|
134
|
+
throw new Error('A temporary retain should only be cleared once. ' +
|
135
|
+
'This indicates a bug in react-disposable-state.');
|
136
136
|
}
|
137
|
-
else if (status ===
|
137
|
+
else if (status === 'Uncleared') {
|
138
138
|
switch (this.__state.kind) {
|
139
|
-
case
|
139
|
+
case 'InParentCacheAndNotDisposed': {
|
140
140
|
this.__state.temporaryRetainCount--;
|
141
141
|
this.__maybeExitInParentCacheAndNotDisposedState(this.__state);
|
142
142
|
clearTimeout(timeoutId);
|
143
143
|
return;
|
144
144
|
}
|
145
145
|
default: {
|
146
|
-
throw new Error(
|
147
|
-
|
146
|
+
throw new Error('A temporary retain was cleared, for which the CacheItem is in an invalid state. ' +
|
147
|
+
'This indicates a bug in react-disposable-state.');
|
148
148
|
}
|
149
149
|
}
|
150
150
|
}
|
151
151
|
};
|
152
152
|
const clearTemporaryRetainByTimeout = () => {
|
153
|
-
status =
|
153
|
+
status = 'ClearedByTimeout';
|
154
154
|
switch (this.__state.kind) {
|
155
|
-
case
|
155
|
+
case 'InParentCacheAndNotDisposed': {
|
156
156
|
this.__state.temporaryRetainCount--;
|
157
157
|
this.__maybeExitInParentCacheAndNotDisposedState(this.__state);
|
158
158
|
return;
|
159
159
|
}
|
160
160
|
default: {
|
161
|
-
throw new Error(
|
162
|
-
|
161
|
+
throw new Error('A temporary retain was cleared, for which the CacheItem is in an invalid state. ' +
|
162
|
+
'This indicates a bug in react-disposable-state.');
|
163
163
|
}
|
164
164
|
}
|
165
165
|
};
|
@@ -167,65 +167,65 @@ class CacheItem {
|
|
167
167
|
return clearTemporaryRetainByCallack;
|
168
168
|
}
|
169
169
|
default: {
|
170
|
-
throw new Error(
|
171
|
-
|
170
|
+
throw new Error('temporaryRetain was called, for which the CacheItem is in an invalid state. ' +
|
171
|
+
'This indicates a bug in react-disposable-state.');
|
172
172
|
}
|
173
173
|
}
|
174
174
|
}
|
175
175
|
permanentRetain() {
|
176
176
|
switch (this.__state.kind) {
|
177
|
-
case
|
177
|
+
case 'InParentCacheAndNotDisposed': {
|
178
178
|
let cleared = false;
|
179
179
|
this.__state.permanentRetainCount++;
|
180
180
|
return () => {
|
181
181
|
if (cleared) {
|
182
|
-
throw new Error(
|
183
|
-
|
182
|
+
throw new Error('A permanent retain should only be cleared once. ' +
|
183
|
+
'This indicates a bug in react-disposable-state.');
|
184
184
|
}
|
185
185
|
cleared = true;
|
186
186
|
switch (this.__state.kind) {
|
187
|
-
case
|
187
|
+
case 'InParentCacheAndNotDisposed': {
|
188
188
|
this.__state.permanentRetainCount--;
|
189
189
|
this.__maybeExitInParentCacheAndNotDisposedState(this.__state);
|
190
190
|
return;
|
191
191
|
}
|
192
|
-
case
|
192
|
+
case 'NotInParentCacheAndNotDisposed': {
|
193
193
|
this.__state.permanentRetainCount--;
|
194
194
|
this.__maybeExitNotInParentCacheAndNotDisposedState(this.__state);
|
195
195
|
return;
|
196
196
|
}
|
197
197
|
default: {
|
198
|
-
throw new Error(
|
199
|
-
|
198
|
+
throw new Error('CacheItem was in a disposed state, but there existed a permanent retain. ' +
|
199
|
+
'This indicates a bug in react-disposable-state.');
|
200
200
|
}
|
201
201
|
}
|
202
202
|
};
|
203
203
|
}
|
204
|
-
case
|
204
|
+
case 'NotInParentCacheAndNotDisposed': {
|
205
205
|
let cleared = false;
|
206
206
|
this.__state.permanentRetainCount++;
|
207
207
|
return () => {
|
208
208
|
if (cleared) {
|
209
|
-
throw new Error(
|
210
|
-
|
209
|
+
throw new Error('A permanent retain should only be cleared once. ' +
|
210
|
+
'This indicates a bug in react-disposable-state.');
|
211
211
|
}
|
212
212
|
cleared = true;
|
213
213
|
switch (this.__state.kind) {
|
214
|
-
case
|
214
|
+
case 'NotInParentCacheAndNotDisposed': {
|
215
215
|
this.__state.permanentRetainCount--;
|
216
216
|
this.__maybeExitNotInParentCacheAndNotDisposedState(this.__state);
|
217
217
|
return;
|
218
218
|
}
|
219
219
|
default: {
|
220
|
-
throw new Error(
|
221
|
-
|
220
|
+
throw new Error('CacheItem was in an unexpected state. ' +
|
221
|
+
'This indicates a bug in react-disposable-state.');
|
222
222
|
}
|
223
223
|
}
|
224
224
|
};
|
225
225
|
}
|
226
226
|
default: {
|
227
|
-
throw new Error(
|
228
|
-
|
227
|
+
throw new Error('permanentRetain was called, but the CacheItem is in an invalid state. ' +
|
228
|
+
'This indicates a bug in react-disposable-state.');
|
229
229
|
}
|
230
230
|
}
|
231
231
|
}
|
@@ -234,13 +234,13 @@ class CacheItem {
|
|
234
234
|
state.removeFromParentCache();
|
235
235
|
state.disposeValue();
|
236
236
|
this.__state = {
|
237
|
-
kind:
|
237
|
+
kind: 'NotInParentCacheAndDisposed',
|
238
238
|
};
|
239
239
|
}
|
240
240
|
else if (state.temporaryRetainCount === 0) {
|
241
241
|
state.removeFromParentCache();
|
242
242
|
this.__state = {
|
243
|
-
kind:
|
243
|
+
kind: 'NotInParentCacheAndNotDisposed',
|
244
244
|
value: state.value,
|
245
245
|
disposeValue: state.disposeValue,
|
246
246
|
permanentRetainCount: state.permanentRetainCount,
|
@@ -251,7 +251,7 @@ class CacheItem {
|
|
251
251
|
if (state.permanentRetainCount === 0) {
|
252
252
|
state.disposeValue();
|
253
253
|
this.__state = {
|
254
|
-
kind:
|
254
|
+
kind: 'NotInParentCacheAndDisposed',
|
255
255
|
};
|
256
256
|
}
|
257
257
|
}
|
package/dist/ParentCache.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import { CacheItem } from
|
2
|
-
import { CleanupFn, Factory, ItemCleanupPair } from
|
1
|
+
import { CacheItem } from './CacheItem';
|
2
|
+
import { CleanupFn, Factory, ItemCleanupPair } from '@isograph/disposable-types';
|
3
3
|
/**
|
4
4
|
* ParentCache
|
5
5
|
* - A ParentCache can be in two states: populated and unpopulated.
|
package/dist/index.d.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
export * from
|
2
|
-
export * from
|
3
|
-
export * from
|
4
|
-
export * from
|
5
|
-
export * from
|
6
|
-
export * from
|
7
|
-
export * from
|
8
|
-
export * from
|
1
|
+
export * from '@isograph/disposable-types';
|
2
|
+
export * from './CacheItem';
|
3
|
+
export * from './ParentCache';
|
4
|
+
export * from './useCachedPrecommitValue';
|
5
|
+
export * from './useDisposableState';
|
6
|
+
export * from './useHasCommittedRef';
|
7
|
+
export * from './useLazyDisposableState';
|
8
|
+
export * from './useUpdatableDisposableState';
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { ParentCache } from
|
2
|
-
import { ItemCleanupPair } from
|
1
|
+
import { ParentCache } from './ParentCache';
|
2
|
+
import { ItemCleanupPair } from '@isograph/isograph-disposable-types/dist';
|
3
3
|
/**
|
4
4
|
* usePrecommitValue<T>
|
5
5
|
* - Takes a mutable parent cache, a factory function, and an onCommit callback.
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import { ParentCache } from
|
2
|
-
import { ItemCleanupPair } from
|
3
|
-
import { UnassignedState } from
|
1
|
+
import { ParentCache } from './ParentCache';
|
2
|
+
import { ItemCleanupPair } from '@isograph/disposable-types';
|
3
|
+
import { UnassignedState } from './useUpdatableDisposableState';
|
4
4
|
type UseUpdatableDisposableStateReturnValue<T> = {
|
5
5
|
state: T;
|
6
6
|
setState: (pair: ItemCleanupPair<Exclude<T, UnassignedState>>) => void;
|
@@ -18,8 +18,8 @@ function useDisposableState(parentCache) {
|
|
18
18
|
itemCleanupPairRef.current = null;
|
19
19
|
}
|
20
20
|
else {
|
21
|
-
throw new Error(
|
22
|
-
|
21
|
+
throw new Error('itemCleanupPairRef.current is unexpectedly null. ' +
|
22
|
+
'This indicates a bug in react-disposable-state.');
|
23
23
|
}
|
24
24
|
}
|
25
25
|
}, [stateFromDisposableStateHook]);
|
@@ -44,9 +44,7 @@ function useDisposableState(parentCache) {
|
|
44
44
|
// Note that in the post-commit post-setState state, itemCleanupPairRef
|
45
45
|
// can still be assigned, during the render before the
|
46
46
|
// cleanupItemCleanupPairRefAfterSetState effect is called.
|
47
|
-
const state = (_c = (_a = (stateFromDisposableStateHook != useUpdatableDisposableState_1.UNASSIGNED_STATE
|
48
|
-
? stateFromDisposableStateHook
|
49
|
-
: null)) !== null && _a !== void 0 ? _a : (_b = itemCleanupPairRef.current) === null || _b === void 0 ? void 0 : _b[0]) !== null && _c !== void 0 ? _c : preCommitItem === null || preCommitItem === void 0 ? void 0 : preCommitItem.state;
|
47
|
+
const state = (_c = (_a = (stateFromDisposableStateHook != useUpdatableDisposableState_1.UNASSIGNED_STATE ? stateFromDisposableStateHook : null)) !== null && _a !== void 0 ? _a : (_b = itemCleanupPairRef.current) === null || _b === void 0 ? void 0 : _b[0]) !== null && _c !== void 0 ? _c : preCommitItem === null || preCommitItem === void 0 ? void 0 : preCommitItem.state;
|
50
48
|
return {
|
51
49
|
state: state,
|
52
50
|
setState,
|
@@ -58,11 +56,11 @@ function tsTests() {
|
|
58
56
|
let x;
|
59
57
|
const a = useDisposableState(x);
|
60
58
|
// @ts-expect-error
|
61
|
-
a.setState([
|
59
|
+
a.setState(['asdf', () => { }]);
|
62
60
|
// @ts-expect-error
|
63
61
|
a.setState([useUpdatableDisposableState_1.UNASSIGNED_STATE, () => { }]);
|
64
62
|
const b = useDisposableState(x);
|
65
63
|
// @ts-expect-error
|
66
64
|
b.setState([useUpdatableDisposableState_1.UNASSIGNED_STATE, () => { }]);
|
67
|
-
b.setState([
|
65
|
+
b.setState(['asdf', () => { }]);
|
68
66
|
}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
'use strict';
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.useLazyDisposableState = void 0;
|
4
4
|
const react_1 = require("react");
|
@@ -23,7 +23,7 @@ function useLazyDisposableState(parentCache) {
|
|
23
23
|
const cleanupFn = (_a = itemCleanupPairRef.current) === null || _a === void 0 ? void 0 : _a[1];
|
24
24
|
// TODO confirm useEffect is called in order.
|
25
25
|
if (cleanupFn == null) {
|
26
|
-
throw new Error(
|
26
|
+
throw new Error('cleanupFn unexpectedly null. This indicates a bug in react-disposable-state.');
|
27
27
|
}
|
28
28
|
return cleanupFn;
|
29
29
|
}, []);
|
@@ -34,6 +34,6 @@ function useLazyDisposableState(parentCache) {
|
|
34
34
|
// Safety: This can't happen. For renders before the initial commit, preCommitItem
|
35
35
|
// is non-null. During the initial commit, we assign itemCleanupPairRef.current,
|
36
36
|
// so during subsequent renders, itemCleanupPairRef.current is non-null.
|
37
|
-
throw new Error(
|
37
|
+
throw new Error('returnedItem was unexpectedly null. This indicates a bug in react-disposable-state.');
|
38
38
|
}
|
39
39
|
exports.useLazyDisposableState = useLazyDisposableState;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { ItemCleanupPair } from
|
1
|
+
import { ItemCleanupPair } from '@isograph/disposable-types';
|
2
2
|
export declare const UNASSIGNED_STATE: unique symbol;
|
3
3
|
export type UnassignedState = typeof UNASSIGNED_STATE;
|
4
4
|
type UseUpdatableDisposableStateReturnValue<T> = {
|
@@ -41,7 +41,7 @@ function useUpdatableDisposableState() {
|
|
41
41
|
const [stateICI, setStateICI] = (0, react_1.useState)(exports.UNASSIGNED_STATE);
|
42
42
|
const setStateAfterCommit = (0, react_1.useCallback)((itemCleanupPair) => {
|
43
43
|
if (!hasCommittedRef.current) {
|
44
|
-
throw new Error(
|
44
|
+
throw new Error('Calling setState before the component has committed is unsafe and disallowed.');
|
45
45
|
}
|
46
46
|
const ici = {
|
47
47
|
item: itemCleanupPair[0],
|
@@ -84,9 +84,9 @@ function tsTests() {
|
|
84
84
|
// @ts-expect-error
|
85
85
|
a.setState([exports.UNASSIGNED_STATE, () => { }]);
|
86
86
|
// @ts-expect-error
|
87
|
-
a.setState([
|
87
|
+
a.setState(['asdf', () => { }]);
|
88
88
|
const b = useUpdatableDisposableState();
|
89
89
|
// @ts-expect-error
|
90
90
|
b.setState([exports.UNASSIGNED_STATE, () => { }]);
|
91
|
-
b.setState([
|
91
|
+
b.setState(['asdf', () => { }]);
|
92
92
|
}
|
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-2c275831",
|
4
4
|
"description": "Primitives for managing disposable state in React",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
@@ -16,7 +16,7 @@
|
|
16
16
|
"prepack": "yarn run compile"
|
17
17
|
},
|
18
18
|
"dependencies": {
|
19
|
-
"@isograph/disposable-types": "0.0.0-main-
|
19
|
+
"@isograph/disposable-types": "0.0.0-main-2c275831",
|
20
20
|
"react": "^18.2.0"
|
21
21
|
},
|
22
22
|
"devDependencies": {
|