@isograph/react-disposable-state 0.0.0-main-dbb17f5b → 0.0.0-main-4adb5045

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.
@@ -1,5 +1,5 @@
1
1
  ../.. |  WARN  Unsupported engine: wanted: {"node":"22.9.0"} (current: {"node":"v22.21.1","pnpm":"10.15.0"})
2
2
 
3
- > @isograph/react-disposable-state@0.0.0-main-dbb17f5b compile-libs /home/runner/work/isograph/isograph/libs/isograph-react-disposable-state
3
+ > @isograph/react-disposable-state@0.0.0-main-4adb5045 compile-libs /home/runner/work/isograph/isograph/libs/isograph-react-disposable-state
4
4
  > rimraf dist && tsc -p tsconfig.pkg.json
5
5
 
@@ -43,7 +43,7 @@ class ParentCache {
43
43
  : null;
44
44
  }
45
45
  getOrPopulateAndTemporaryRetain() {
46
- return this.__cacheItem === null
46
+ return this.__cacheItem == null
47
47
  ? this.__populateAndTemporaryRetain()
48
48
  : temporaryRetain(this.__cacheItem);
49
49
  }
@@ -79,7 +79,7 @@ class ParentCache {
79
79
  return this.__factory;
80
80
  }
81
81
  isEmpty() {
82
- return this.__cacheItem === null;
82
+ return this.__cacheItem == null;
83
83
  }
84
84
  }
85
85
  exports.ParentCache = ParentCache;
@@ -62,14 +62,14 @@ function useCachedResponsivePrecommitValue(parentCache, onCommit) {
62
62
  // After the above, we have a non-disposed item and a cleanup function, which we
63
63
  // can pass to onCommit.
64
64
  const undisposedPair = cacheItem.permanentRetainIfNotDisposed(disposeOfTemporaryRetain);
65
- if (undisposedPair !== null) {
65
+ if (undisposedPair != null) {
66
66
  onCommit(undisposedPair);
67
67
  }
68
68
  else {
69
69
  // The cache item we created during render has been disposed. Check if the parent
70
70
  // cache is populated.
71
71
  const existingCacheItemCleanupPair = parentCache.getAndPermanentRetainIfPresent();
72
- if (existingCacheItemCleanupPair !== null) {
72
+ if (existingCacheItemCleanupPair != null) {
73
73
  onCommit(existingCacheItemCleanupPair);
74
74
  }
75
75
  else {
@@ -1 +1 @@
1
- {"version":3,"file":"useDisposableState.d.ts","sourceRoot":"","sources":["../src/useDisposableState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAE7D,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAEL,eAAe,EAEhB,MAAM,+BAA+B,CAAC;AAEvC,KAAK,sCAAsC,CAAC,CAAC,IAAI;IAC/C,KAAK,EAAE,CAAC,CAAC;IACT,QAAQ,EAAE,CAAC,IAAI,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC,EAAE,eAAe,CAAC,CAAC,KAAK,IAAI,CAAC;CACxE,CAAC;AAEF,wBAAgB,kBAAkB,CAAC,CAAC,GAAG,KAAK,EAC1C,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,GAC1B,sCAAsC,CAAC,CAAC,CAAC,CAgE3C"}
1
+ {"version":3,"file":"useDisposableState.d.ts","sourceRoot":"","sources":["../src/useDisposableState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAE7D,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAEL,eAAe,EAEhB,MAAM,+BAA+B,CAAC;AAEvC,KAAK,sCAAsC,CAAC,CAAC,IAAI;IAC/C,KAAK,EAAE,CAAC,CAAC;IACT,QAAQ,EAAE,CAAC,IAAI,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC,EAAE,eAAe,CAAC,CAAC,KAAK,IAAI,CAAC;CACxE,CAAC;AAEF,wBAAgB,kBAAkB,CAAC,CAAC,GAAG,KAAK,EAC1C,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,GAC1B,sCAAsC,CAAC,CAAC,CAAC,CAqE3C"}
@@ -15,7 +15,7 @@ function useDisposableState(parentCache) {
15
15
  const { state: stateFromDisposableStateHook, setState } = (0, useUpdatableDisposableState_1.useUpdatableDisposableState)();
16
16
  (0, react_1.useEffect)(function cleanupItemCleanupPairRefAfterSetState() {
17
17
  if (stateFromDisposableStateHook !== useUpdatableDisposableState_1.UNASSIGNED_STATE) {
18
- if (itemCleanupPairRef.current !== null) {
18
+ if (itemCleanupPairRef.current != null) {
19
19
  itemCleanupPairRef.current[1]();
20
20
  itemCleanupPairRef.current = null;
21
21
  }
@@ -27,12 +27,21 @@ function useDisposableState(parentCache) {
27
27
  }, [stateFromDisposableStateHook]);
28
28
  (0, react_1.useEffect)(function cleanupItemCleanupPairRefIfSetStateNotCalled() {
29
29
  return () => {
30
- if (itemCleanupPairRef.current !== null) {
30
+ if (itemCleanupPairRef.current != null) {
31
31
  itemCleanupPairRef.current[1]();
32
32
  itemCleanupPairRef.current = null;
33
33
  }
34
34
  };
35
35
  }, []);
36
+ const state = (_b = (_a = (stateFromDisposableStateHook !== useUpdatableDisposableState_1.UNASSIGNED_STATE
37
+ ? stateFromDisposableStateHook
38
+ : null)) !== null && _a !== void 0 ? _a : preCommitItem === null || preCommitItem === void 0 ? void 0 : preCommitItem.state) !== null && _b !== void 0 ? _b : (_c = itemCleanupPairRef.current) === null || _c === void 0 ? void 0 : _c[0];
39
+ if (state != null) {
40
+ return {
41
+ state: state,
42
+ setState,
43
+ };
44
+ }
36
45
  // Safety: we can be in one of three states. Pre-commit, in which case
37
46
  // preCommitItem is assigned, post-commit but before setState has been
38
47
  // called, in which case itemCleanupPairRef.current is assigned, or
@@ -46,13 +55,7 @@ function useDisposableState(parentCache) {
46
55
  // Note that in the post-commit post-setState state, itemCleanupPairRef
47
56
  // can still be assigned, during the render before the
48
57
  // cleanupItemCleanupPairRefAfterSetState effect is called.
49
- const state = (_b = (_a = (stateFromDisposableStateHook != useUpdatableDisposableState_1.UNASSIGNED_STATE
50
- ? stateFromDisposableStateHook
51
- : null)) !== null && _a !== void 0 ? _a : preCommitItem === null || preCommitItem === void 0 ? void 0 : preCommitItem.state) !== null && _b !== void 0 ? _b : (_c = itemCleanupPairRef.current) === null || _c === void 0 ? void 0 : _c[0];
52
- return {
53
- state: state,
54
- setState,
55
- };
58
+ throw new Error('state was unexpectedly null. This indicates a bug in react-disposable-state.');
56
59
  }
57
60
  // @ts-ignore
58
61
  function tsTests() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isograph/react-disposable-state",
3
- "version": "0.0.0-main-dbb17f5b",
3
+ "version": "0.0.0-main-4adb5045",
4
4
  "description": "Primitives for managing disposable state in React",
5
5
  "homepage": "https://isograph.dev",
6
6
  "main": "dist/index.js",
@@ -8,7 +8,7 @@
8
8
  "author": "Isograph Labs",
9
9
  "license": "MIT",
10
10
  "dependencies": {
11
- "@isograph/disposable-types": "0.0.0-main-dbb17f5b"
11
+ "@isograph/disposable-types": "0.0.0-main-4adb5045"
12
12
  },
13
13
  "peerDependencies": {
14
14
  "react": "^18.0.0 || ^19.0.0"
@@ -298,7 +298,7 @@ describe('CacheItem', () => {
298
298
  assert(state.kind === 'NotInParentCacheAndDisposed');
299
299
 
300
300
  assert(
301
- cacheItem.permanentRetainIfNotDisposed(disposeTemporaryRetain) === null,
301
+ cacheItem.permanentRetainIfNotDisposed(disposeTemporaryRetain) == null,
302
302
  );
303
303
 
304
304
  expect(() => {
@@ -26,7 +26,7 @@ describe('ParentCache', () => {
26
26
  assert(getValue(parentCache) != null, 'Parent cache should not be empty');
27
27
 
28
28
  parentCache.empty();
29
- assert(getValue(parentCache) === null);
29
+ assert(getValue(parentCache) == null);
30
30
 
31
31
  parentCache.getOrPopulateAndTemporaryRetain();
32
32
  expect(factory.mock.calls.length).toBe(2);
@@ -50,7 +50,7 @@ describe('ParentCache', () => {
50
50
  parentCache.getOrPopulateAndTemporaryRetain();
51
51
  clearTemporaryRetain();
52
52
 
53
- assert(getValue(parentCache) === null);
53
+ assert(getValue(parentCache) == null);
54
54
  });
55
55
 
56
56
  test('Clearing one of two temporary retains does not remove the item from the parent cache', () => {
@@ -69,6 +69,6 @@ describe('ParentCache', () => {
69
69
  assert(getValue(parentCache) != null);
70
70
 
71
71
  clearTemporaryRetain2();
72
- assert(getValue(parentCache) === null);
72
+ assert(getValue(parentCache) == null);
73
73
  });
74
74
  });
@@ -51,7 +51,7 @@ export class ParentCache<T> {
51
51
  }
52
52
 
53
53
  getOrPopulateAndTemporaryRetain(): [CacheItem<T>, T, CleanupFn] {
54
- return this.__cacheItem === null
54
+ return this.__cacheItem == null
55
55
  ? this.__populateAndTemporaryRetain()
56
56
  : temporaryRetain(this.__cacheItem);
57
57
  }
@@ -93,7 +93,7 @@ export class ParentCache<T> {
93
93
  }
94
94
 
95
95
  isEmpty(): boolean {
96
- return this.__cacheItem === null;
96
+ return this.__cacheItem == null;
97
97
  }
98
98
  }
99
99
 
@@ -158,7 +158,7 @@ describe('useCachedResponsivePrecommitValue', () => {
158
158
  setState = _setState;
159
159
  const value = useCachedResponsivePrecommitValue(cache, hookOnCommit);
160
160
 
161
- if (initialRender && value !== null) {
161
+ if (initialRender && value != null) {
162
162
  initialRender = false;
163
163
  expect(value).toEqual({ state: 1 });
164
164
  } else {
@@ -70,14 +70,14 @@ export function useCachedResponsivePrecommitValue<T>(
70
70
  const undisposedPair = cacheItem.permanentRetainIfNotDisposed(
71
71
  disposeOfTemporaryRetain,
72
72
  );
73
- if (undisposedPair !== null) {
73
+ if (undisposedPair != null) {
74
74
  onCommit(undisposedPair);
75
75
  } else {
76
76
  // The cache item we created during render has been disposed. Check if the parent
77
77
  // cache is populated.
78
78
  const existingCacheItemCleanupPair =
79
79
  parentCache.getAndPermanentRetainIfPresent();
80
- if (existingCacheItemCleanupPair !== null) {
80
+ if (existingCacheItemCleanupPair != null) {
81
81
  onCommit(existingCacheItemCleanupPair);
82
82
  } else {
83
83
  // We did not find an item in the parent cache, create a new one.
@@ -32,7 +32,7 @@ export function useDisposableState<T = never>(
32
32
  useEffect(
33
33
  function cleanupItemCleanupPairRefAfterSetState() {
34
34
  if (stateFromDisposableStateHook !== UNASSIGNED_STATE) {
35
- if (itemCleanupPairRef.current !== null) {
35
+ if (itemCleanupPairRef.current != null) {
36
36
  itemCleanupPairRef.current[1]();
37
37
  itemCleanupPairRef.current = null;
38
38
  } else {
@@ -48,12 +48,25 @@ export function useDisposableState<T = never>(
48
48
 
49
49
  useEffect(function cleanupItemCleanupPairRefIfSetStateNotCalled() {
50
50
  return () => {
51
- if (itemCleanupPairRef.current !== null) {
51
+ if (itemCleanupPairRef.current != null) {
52
52
  itemCleanupPairRef.current[1]();
53
53
  itemCleanupPairRef.current = null;
54
54
  }
55
55
  };
56
56
  }, []);
57
+ const state: T | undefined =
58
+ (stateFromDisposableStateHook !== UNASSIGNED_STATE
59
+ ? stateFromDisposableStateHook
60
+ : null) ??
61
+ preCommitItem?.state ??
62
+ itemCleanupPairRef.current?.[0];
63
+
64
+ if (state != null) {
65
+ return {
66
+ state: state,
67
+ setState,
68
+ };
69
+ }
57
70
 
58
71
  // Safety: we can be in one of three states. Pre-commit, in which case
59
72
  // preCommitItem is assigned, post-commit but before setState has been
@@ -68,17 +81,9 @@ export function useDisposableState<T = never>(
68
81
  // Note that in the post-commit post-setState state, itemCleanupPairRef
69
82
  // can still be assigned, during the render before the
70
83
  // cleanupItemCleanupPairRefAfterSetState effect is called.
71
- const state: T | undefined =
72
- (stateFromDisposableStateHook != UNASSIGNED_STATE
73
- ? stateFromDisposableStateHook
74
- : null) ??
75
- preCommitItem?.state ??
76
- itemCleanupPairRef.current?.[0];
77
-
78
- return {
79
- state: state!,
80
- setState,
81
- };
84
+ throw new Error(
85
+ 'state was unexpectedly null. This indicates a bug in react-disposable-state.',
86
+ );
82
87
  }
83
88
 
84
89
  // @ts-ignore
@@ -47,7 +47,7 @@ describe('useLazyDisposableState', async () => {
47
47
  }, []);
48
48
 
49
49
  useEffect(() => {
50
- if (state == 1) return;
50
+ if (state === 1) return;
51
51
  committed.resolve();
52
52
  }, [state]);
53
53