@t8/react-pending 1.0.25 → 1.0.27

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/README.md CHANGED
@@ -83,7 +83,7 @@ Omit the custom string key parameter of `usePendingState()` to scope the pending
83
83
  + let [state, withState] = usePendingState(); // local
84
84
  ```
85
85
 
86
- ## Silent tracking of background and optimistic updates
86
+ ## Silent tracking of background actions and optimistic updates
87
87
 
88
88
  ```diff
89
89
  - withState(fetchItems())
@@ -96,7 +96,7 @@ Omit the custom string key parameter of `usePendingState()` to scope the pending
96
96
 
97
97
  ```diff
98
98
  - withState(fetchItems())
99
- + withState(fetchItems(), { delay: 500 })
99
+ + withState(fetchItems(), { delay: 500 }) // in milliseconds
100
100
  ```
101
101
 
102
102
  🔹 Use case: avoiding flashing a process indicator when the action is likely to complete by the end of a short delay.
@@ -121,7 +121,7 @@ Omit the custom string key parameter of `usePendingState()` to scope the pending
121
121
  + </PendingStateProvider>
122
122
  ```
123
123
 
124
- 🔹 `<PendingStateProvider>` creates an isolated instance of initial shared action state. Prime use cases: tests, SSR. It isn't required with client-side rendering, but it can be used to separate action states of larger self-contained portions of a web app.
124
+ 🔹 `<PendingStateProvider>` creates an isolated instance of initial shared action state. Prime use cases are SSR and tests. It isn't required with client-side rendering, but it can be used to separate action states of larger self-contained portions of a web app.
125
125
 
126
126
  ## Providing custom initial pending&nbsp;state
127
127
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,58 @@
1
- export * from "./src/PendingState.ts";
2
- export * from "./src/PendingStateContext.ts";
3
- export * from "./src/PendingStateProvider.tsx";
4
- export * from "./src/usePendingState.ts";
1
+ import type { SetStoreState, Store } from "@t8/react-store";
2
+ import type { ReactNode } from "react";
3
+
4
+ export type PendingState = {
5
+ initialized?: boolean | undefined;
6
+ complete?: boolean | undefined;
7
+ time?: number | undefined;
8
+ error?: unknown;
9
+ };
10
+ export declare const PendingStateContext: import("react").Context<Map<string, Store<PendingState>>>;
11
+ export type PendingStateProviderProps = {
12
+ value?: Record<string, PendingState> | Map<string, Store<PendingState>> | null | undefined;
13
+ children?: ReactNode;
14
+ };
15
+ export declare const PendingStateProvider: ({ value, children, }: PendingStateProviderProps) => import("react/jsx-runtime").JSX.Element;
16
+ export type WithStateOptions = {
17
+ /**
18
+ * Whether to track the action state silently (e.g. with a background
19
+ * action or an optimistic update).
20
+ *
21
+ * When set to `true`, the state's `complete` property doesn't switch
22
+ * to `false` in the pending state.
23
+ */
24
+ silent?: boolean;
25
+ /**
26
+ * Delays switching the action state's `complete` property to `false`
27
+ * in the pending state by the given number of milliseconds.
28
+ *
29
+ * Use case: to avoid flashing a process indicator if the action is
30
+ * likely to complete by the end of a short delay.
31
+ */
32
+ delay?: number;
33
+ /**
34
+ * Allows the async action to reject explicitly, along with exposing
35
+ * the action state's `error` property that goes by default.
36
+ */
37
+ throws?: boolean;
38
+ };
39
+ /**
40
+ * Returns an instance of pending state and the functions to update it.
41
+ *
42
+ * @param store - A unique store key or a store. Providing a store
43
+ * key or a shared store allows to share the state across multiple
44
+ * components.
45
+ *
46
+ * @returns `[state, withState, setState]`, where
47
+ * - `state` is the current value of the pending state;
48
+ * - `withState(action, options?)` reads and tracks the pending state
49
+ * of `action`;
50
+ * - `setState(update)` can update the pending state value directly.
51
+ */
52
+ export declare function usePendingState(store?: string | Store<PendingState> | null): [
53
+ PendingState,
54
+ <T>(value: T) => T,
55
+ SetStoreState<PendingState>
56
+ ];
57
+
58
+ export {};
package/package.json CHANGED
@@ -1,20 +1,17 @@
1
1
  {
2
2
  "name": "@t8/react-pending",
3
- "version": "1.0.25",
3
+ "version": "1.0.27",
4
4
  "description": "Concise async action state tracking for React apps",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "type": "module",
8
8
  "scripts": {
9
- "build": "npx npm-run-all clean compile types",
10
9
  "clean": "node -e \"require('node:fs').rmSync('dist', { force: true, recursive: true });\"",
11
10
  "compile": "npx esbuild index.ts --bundle --outdir=dist --platform=neutral --external:react",
12
11
  "demo": "npx @t8/serve 3000 * tests/async_status -b src/index.tsx",
13
- "prepublishOnly": "npm run build",
14
- "preversion": "npx npm-run-all shape build test",
15
- "shape": "npx codeshape --typecheck",
16
- "test": "npx playwright test --project=chromium",
17
- "types": "tsc -p tsconfig.build.json"
12
+ "preversion": "npx npm-run-all clean shape compile test",
13
+ "shape": "npx codeshape --typecheck --emit-types",
14
+ "test": "npx playwright test --project=chromium"
18
15
  },
19
16
  "author": "axtk",
20
17
  "license": "MIT",
@@ -34,13 +31,12 @@
34
31
  "devDependencies": {
35
32
  "@playwright/test": "^1.56.0",
36
33
  "@t8/serve": "^0.1.36",
37
- "@types/node": "^24.10.1",
34
+ "@types/node": "^24.10.2",
38
35
  "@types/react": "^19.2.7",
39
- "@types/react-dom": "^19.1.9",
40
- "react-dom": "^19.2.1",
41
- "typescript": "^5.9.3"
36
+ "@types/react-dom": "^19.2.3",
37
+ "react-dom": "^19.2.1"
42
38
  },
43
39
  "dependencies": {
44
- "@t8/react-store": "^1.2.3"
40
+ "@t8/react-store": "^1.2.4"
45
41
  }
46
42
  }
@@ -17,26 +17,43 @@ function createState(
17
17
  }
18
18
 
19
19
  export type WithStateOptions = {
20
+ /**
21
+ * Whether to track the action state silently (e.g. with a background
22
+ * action or an optimistic update).
23
+ *
24
+ * When set to `true`, the state's `complete` property doesn't switch
25
+ * to `false` in the pending state.
26
+ */
20
27
  silent?: boolean;
21
- throws?: boolean;
28
+ /**
29
+ * Delays switching the action state's `complete` property to `false`
30
+ * in the pending state by the given number of milliseconds.
31
+ *
32
+ * Use case: to avoid flashing a process indicator if the action is
33
+ * likely to complete by the end of a short delay.
34
+ */
22
35
  delay?: number;
36
+ /**
37
+ * Allows the async action to reject explicitly, along with exposing
38
+ * the action state's `error` property that goes by default.
39
+ */
40
+ throws?: boolean;
23
41
  };
24
42
 
25
43
  /**
26
- * Returns an array containing `[state, withState, setState]`:
27
- * - `state` reflects the state of a value passed to `withState()`;
28
- * - `withState(value, [options])` enables the tracking of the state
29
- * of `value`; setting the options to `{silent: true}` prevents
30
- * `withState()` from updating the state while `value` is pending
31
- * (e.g. for background or optimistic updates);
32
- * - `setState()` to directly update the state.
44
+ * Returns an instance of pending state and the functions to update it.
45
+ *
46
+ * @param store - A unique store key or a store. Providing a store
47
+ * key or a shared store allows to share the state across multiple
48
+ * components.
49
+ *
50
+ * @returns `[state, withState, setState]`, where
51
+ * - `state` is the current value of the pending state;
52
+ * - `withState(action, options?)` reads and tracks the pending state
53
+ * of `action`;
54
+ * - `setState(update)` can update the pending state value directly.
33
55
  */
34
56
  export function usePendingState(
35
- /**
36
- * A unique store key or a store. Providing a store key or a
37
- * shared store allows to share the state across multiple
38
- * components.
39
- */
40
57
  store?: string | Store<PendingState> | null,
41
58
  ): [PendingState, <T>(value: T) => T, SetStoreState<PendingState>] {
42
59
  let storeMap = useContext(PendingStateContext);
@@ -1,6 +0,0 @@
1
- export type PendingState = {
2
- initialized?: boolean | undefined;
3
- complete?: boolean | undefined;
4
- time?: number | undefined;
5
- error?: unknown;
6
- };
@@ -1,3 +0,0 @@
1
- import type { Store } from "@t8/react-store";
2
- import type { PendingState } from "./PendingState.ts";
3
- export declare const PendingStateContext: import("react").Context<Map<string, Store<PendingState>>>;
@@ -1,8 +0,0 @@
1
- import { Store } from "@t8/react-store";
2
- import { type ReactNode } from "react";
3
- import type { PendingState } from "./PendingState.ts";
4
- export type PendingStateProviderProps = {
5
- value?: Record<string, PendingState> | Map<string, Store<PendingState>> | null | undefined;
6
- children?: ReactNode;
7
- };
8
- export declare const PendingStateProvider: ({ value, children, }: PendingStateProviderProps) => import("react/jsx-runtime").JSX.Element;
@@ -1,23 +0,0 @@
1
- import { type SetStoreState, Store } from "@t8/react-store";
2
- import type { PendingState } from "./PendingState.ts";
3
- export type WithStateOptions = {
4
- silent?: boolean;
5
- throws?: boolean;
6
- delay?: number;
7
- };
8
- /**
9
- * Returns an array containing `[state, withState, setState]`:
10
- * - `state` reflects the state of a value passed to `withState()`;
11
- * - `withState(value, [options])` enables the tracking of the state
12
- * of `value`; setting the options to `{silent: true}` prevents
13
- * `withState()` from updating the state while `value` is pending
14
- * (e.g. for background or optimistic updates);
15
- * - `setState()` to directly update the state.
16
- */
17
- export declare function usePendingState(
18
- /**
19
- * A unique store key or a store. Providing a store key or a
20
- * shared store allows to share the state across multiple
21
- * components.
22
- */
23
- store?: string | Store<PendingState> | null): [PendingState, <T>(value: T) => T, SetStoreState<PendingState>];
@@ -1,4 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "include": ["./index.ts", "./src"]
4
- }