@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.
@@ -1,9 +1,9 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
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";
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 "react";
2
- import { ParentCache } from "./ParentCache";
3
- import { ItemCleanupPair } from "@isograph/disposable-types";
4
- import { useCachedPrecommitValue } from "./useCachedPrecommitValue";
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 "./useUpdatableDisposableState";
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
- "itemCleanupPairRef.current is unexpectedly null. " +
37
- "This indicates a bug in react-disposable-state."
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(["asdf", () => {}]);
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(["asdf", () => {}]);
93
+ b.setState(['asdf', () => {}]);
92
94
  }
@@ -1,4 +1,4 @@
1
- import { MutableRefObject, useEffect, useRef } from "react";
1
+ import { MutableRefObject, useEffect, useRef } from 'react';
2
2
 
3
3
  /**
4
4
  * Returns true if the component has committed, false otherwise.
@@ -1,9 +1,9 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- import { useEffect, useRef } from "react";
4
- import type { ItemCleanupPair } from "@isograph/disposable-types";
5
- import { ParentCache } from "./ParentCache";
6
- import { useCachedPrecommitValue } from "./useCachedPrecommitValue";
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
- "cleanupFn unexpectedly null. This indicates a bug in react-disposable-state."
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
- "returnedItem was unexpectedly null. This indicates a bug in react-disposable-state."
46
+ 'returnedItem was unexpectedly null. This indicates a bug in react-disposable-state.',
47
47
  );
48
48
  }