@orpc/react 1.1.0 → 1.2.0
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/hooks/index.mjs +54 -42
- package/package.json +6 -6
package/dist/hooks/index.mjs
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { safe, createORPCErrorFromJson } from '@orpc/client';
|
2
2
|
import { intercept, toArray } from '@orpc/shared';
|
3
|
-
import { useState, useCallback, useMemo } from 'react';
|
3
|
+
import { useState, useRef, useTransition, useCallback, useMemo } from 'react';
|
4
4
|
|
5
5
|
const INITIAL_STATE = {
|
6
6
|
data: void 0,
|
@@ -9,56 +9,68 @@ const INITIAL_STATE = {
|
|
9
9
|
isPending: false,
|
10
10
|
isSuccess: false,
|
11
11
|
isError: false,
|
12
|
-
status: "idle"
|
13
|
-
|
14
|
-
|
12
|
+
status: "idle"
|
13
|
+
};
|
14
|
+
const PENDING_STATE = {
|
15
|
+
data: void 0,
|
16
|
+
error: null,
|
17
|
+
isIdle: false,
|
18
|
+
isPending: true,
|
19
|
+
isSuccess: false,
|
20
|
+
isError: false,
|
21
|
+
status: "pending"
|
15
22
|
};
|
16
23
|
function useServerAction(action, options = {}) {
|
17
24
|
const [state, setState] = useState(INITIAL_STATE);
|
25
|
+
const executedAtRef = useRef(void 0);
|
26
|
+
const [input, setInput] = useState(void 0);
|
27
|
+
const [isPending, startTransition] = useTransition();
|
18
28
|
const reset = useCallback(() => {
|
19
|
-
|
29
|
+
executedAtRef.current = void 0;
|
30
|
+
setInput(void 0);
|
31
|
+
setState({ ...INITIAL_STATE });
|
20
32
|
}, []);
|
21
|
-
const execute = useCallback(async (
|
33
|
+
const execute = useCallback(async (input2, executeOptions = {}) => {
|
22
34
|
const executedAt = /* @__PURE__ */ new Date();
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
35
|
+
executedAtRef.current = executedAt;
|
36
|
+
setInput(input2);
|
37
|
+
return new Promise((resolve) => {
|
38
|
+
startTransition(async () => {
|
39
|
+
const result2 = await safe(intercept(
|
40
|
+
[...toArray(options.interceptors), ...toArray(executeOptions.interceptors)],
|
41
|
+
{ input: input2 },
|
42
|
+
({ input: input3 }) => action(input3).then(([error, data]) => {
|
43
|
+
if (error) {
|
44
|
+
throw createORPCErrorFromJson(error);
|
45
|
+
}
|
46
|
+
return data;
|
47
|
+
})
|
48
|
+
));
|
49
|
+
if (executedAtRef.current === executedAt) {
|
50
|
+
setState({
|
51
|
+
data: result2.data,
|
52
|
+
error: result2.error,
|
53
|
+
isIdle: false,
|
54
|
+
isPending: false,
|
55
|
+
isSuccess: !result2.error,
|
56
|
+
isError: !!result2.error,
|
57
|
+
status: !result2.error ? "success" : "error"
|
58
|
+
});
|
40
59
|
}
|
41
|
-
|
42
|
-
})
|
43
|
-
));
|
44
|
-
setState({
|
45
|
-
data: result2.data,
|
46
|
-
error: result2.error,
|
47
|
-
isIdle: false,
|
48
|
-
isPending: false,
|
49
|
-
isSuccess: !result2.error,
|
50
|
-
isError: !!result2.error,
|
51
|
-
status: !result2.error ? "success" : "error",
|
52
|
-
executedAt,
|
53
|
-
input
|
60
|
+
resolve(result2);
|
61
|
+
});
|
54
62
|
});
|
55
|
-
return result2;
|
56
63
|
}, [action, ...toArray(options.interceptors)]);
|
57
|
-
const result = useMemo(() =>
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
64
|
+
const result = useMemo(() => {
|
65
|
+
const currentState = isPending && executedAtRef.current !== void 0 ? PENDING_STATE : state;
|
66
|
+
return {
|
67
|
+
...currentState,
|
68
|
+
executedAt: executedAtRef.current,
|
69
|
+
input,
|
70
|
+
reset,
|
71
|
+
execute
|
72
|
+
};
|
73
|
+
}, [isPending, state, input, reset, execute]);
|
62
74
|
return result;
|
63
75
|
}
|
64
76
|
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@orpc/react",
|
3
3
|
"type": "module",
|
4
|
-
"version": "1.
|
4
|
+
"version": "1.2.0",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
7
7
|
"repository": {
|
@@ -34,11 +34,11 @@
|
|
34
34
|
"react": ">=18.0.0"
|
35
35
|
},
|
36
36
|
"dependencies": {
|
37
|
-
"@orpc/client": "1.
|
38
|
-
"@orpc/
|
39
|
-
"@orpc/
|
40
|
-
"@orpc/
|
41
|
-
"@orpc/
|
37
|
+
"@orpc/client": "1.2.0",
|
38
|
+
"@orpc/contract": "1.2.0",
|
39
|
+
"@orpc/openapi-client": "1.2.0",
|
40
|
+
"@orpc/shared": "1.2.0",
|
41
|
+
"@orpc/server": "1.2.0"
|
42
42
|
},
|
43
43
|
"devDependencies": {
|
44
44
|
"react": "^19.1.0",
|