@ic-reactor/react 0.2.0 → 0.2.2
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.d.ts +12 -3
- package/dist/hooks.js +23 -6
- package/dist/index.d.ts +11 -1
- package/dist/index.js +6 -8
- package/dist/types.d.ts +1 -1
- package/package.json +10 -10
package/dist/hooks.d.ts
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
|
-
import { CallMethod, ExtractReActorMethodArgs } from "@ic-reactor/store";
|
|
1
|
+
import { CallMethod, ExtractReActorMethodArgs, ReActorActorStore, ReActorMethodField } from "@ic-reactor/store";
|
|
2
2
|
import { ReActorUseQueryArgs, ReActorUseUpdateArgs } from "./types";
|
|
3
|
-
export declare const getCallHooks: <A extends unknown>(callMethod: CallMethod<A>) => {
|
|
3
|
+
export declare const getCallHooks: <A extends unknown>(callMethod: CallMethod<A>, actorStore: ReActorActorStore<A>) => {
|
|
4
|
+
useField: (functionName: keyof A & string) => {
|
|
5
|
+
current: ReActorMethodField<A> | undefined;
|
|
6
|
+
};
|
|
4
7
|
useQueryCall: <M extends keyof A>({ autoRefresh, refreshInterval, disableInitialCall, ...rest }: ReActorUseQueryArgs<A, M>) => {
|
|
5
8
|
data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined;
|
|
6
9
|
error: Error | undefined;
|
|
7
10
|
loading: boolean;
|
|
8
|
-
|
|
11
|
+
field: {
|
|
12
|
+
current: ReActorMethodField<A> | undefined;
|
|
13
|
+
};
|
|
14
|
+
call: (replaceArgs?: ExtractReActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined>;
|
|
9
15
|
};
|
|
10
16
|
useUpdateCall: <M_1 extends keyof A>(args: ReActorUseUpdateArgs<A, M_1>) => {
|
|
11
17
|
data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M_1]> | undefined;
|
|
12
18
|
error: Error | undefined;
|
|
13
19
|
loading: boolean;
|
|
14
20
|
call: (replaceArgs?: ExtractReActorMethodArgs<A[M_1]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M_1]> | undefined>;
|
|
21
|
+
field: {
|
|
22
|
+
current: ReActorMethodField<A> | undefined;
|
|
23
|
+
};
|
|
15
24
|
};
|
|
16
25
|
};
|
package/dist/hooks.js
CHANGED
|
@@ -22,7 +22,22 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
23
|
exports.getCallHooks = void 0;
|
|
24
24
|
const react_1 = require("react");
|
|
25
|
-
const getCallHooks = (callMethod) => {
|
|
25
|
+
const getCallHooks = (callMethod, actorStore) => {
|
|
26
|
+
const useField = (functionName) => {
|
|
27
|
+
const [field, setField] = (0, react_1.useState)(undefined);
|
|
28
|
+
(0, react_1.useEffect)(() => {
|
|
29
|
+
const unsubscribe = actorStore.subscribe((state) => {
|
|
30
|
+
if (state.methodFields) {
|
|
31
|
+
const field = state.methodFields.find((f) => f.functionName === functionName);
|
|
32
|
+
setField(field);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
return () => {
|
|
36
|
+
unsubscribe();
|
|
37
|
+
};
|
|
38
|
+
}, [functionName]);
|
|
39
|
+
return { current: field };
|
|
40
|
+
};
|
|
26
41
|
const useReActorCall = ({ onError, onSuccess, onLoading, args = [], functionName, }) => {
|
|
27
42
|
const [state, setState] = (0, react_1.useState)({
|
|
28
43
|
data: undefined,
|
|
@@ -47,33 +62,35 @@ const getCallHooks = (callMethod) => {
|
|
|
47
62
|
setState((prevState) => (Object.assign(Object.assign({}, prevState), { error: error, loading: false })));
|
|
48
63
|
}
|
|
49
64
|
}), [args, functionName, onError, onSuccess, onLoading]);
|
|
50
|
-
|
|
65
|
+
const field = useField(functionName);
|
|
66
|
+
return Object.assign({ call, field }, state);
|
|
51
67
|
};
|
|
52
68
|
const useQueryCall = (_a) => {
|
|
53
69
|
var { autoRefresh, refreshInterval = 5000, disableInitialCall } = _a, rest = __rest(_a, ["autoRefresh", "refreshInterval", "disableInitialCall"]);
|
|
54
|
-
const _b = useReActorCall(rest), { call
|
|
70
|
+
const _b = useReActorCall(rest), { call } = _b, state = __rest(_b, ["call"]);
|
|
55
71
|
let intervalId = (0, react_1.useRef)(undefined);
|
|
56
72
|
(0, react_1.useEffect)(() => {
|
|
57
73
|
// Auto-refresh logic
|
|
58
74
|
if (autoRefresh) {
|
|
59
75
|
intervalId.current = setInterval(() => {
|
|
60
|
-
|
|
76
|
+
call();
|
|
61
77
|
}, refreshInterval);
|
|
62
78
|
}
|
|
63
79
|
// Initial call logic
|
|
64
80
|
if (!disableInitialCall) {
|
|
65
|
-
|
|
81
|
+
call();
|
|
66
82
|
}
|
|
67
83
|
return () => {
|
|
68
84
|
clearInterval(intervalId.current);
|
|
69
85
|
};
|
|
70
86
|
}, [disableInitialCall, autoRefresh, refreshInterval]);
|
|
71
|
-
return Object.assign({
|
|
87
|
+
return Object.assign({ call }, state);
|
|
72
88
|
};
|
|
73
89
|
const useUpdateCall = (args) => {
|
|
74
90
|
return useReActorCall(args);
|
|
75
91
|
};
|
|
76
92
|
return {
|
|
93
|
+
useField,
|
|
77
94
|
useQueryCall,
|
|
78
95
|
useUpdateCall,
|
|
79
96
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,9 @@ import { AuthClientLoginOptions } from "@dfinity/auth-client";
|
|
|
2
2
|
import type { ActorSubclass, ReActorAuthStore, ReActorOptions } from "@ic-reactor/store";
|
|
3
3
|
export type ReActorContextType<A = ActorSubclass<any>> = ReActorAuthStore<A>;
|
|
4
4
|
export declare const createReActor: <A extends unknown>(options: ReActorOptions) => {
|
|
5
|
+
useField: (functionName: keyof A & string) => {
|
|
6
|
+
current: import("@ic-reactor/store").ReActorMethodField<A> | undefined;
|
|
7
|
+
};
|
|
5
8
|
useActorStore: () => {
|
|
6
9
|
initialize: (agentOptions?: import("@dfinity/agent").HttpAgentOptions | undefined, identity?: import("@ic-reactor/store").Identity | undefined) => void;
|
|
7
10
|
actor: A | null;
|
|
@@ -9,19 +12,26 @@ export declare const createReActor: <A extends unknown>(options: ReActorOptions)
|
|
|
9
12
|
initializing: boolean;
|
|
10
13
|
error: Error | undefined;
|
|
11
14
|
methodState: import("@ic-reactor/store").ReActorMethodStates<A>;
|
|
15
|
+
methodFields: import("@ic-reactor/store").ReActorMethodField<A>[];
|
|
12
16
|
};
|
|
13
17
|
useAuthStore: () => import("@ic-reactor/store").ReActorAuthState<A>;
|
|
14
18
|
useQueryCall: <M extends keyof A>({ autoRefresh, refreshInterval, disableInitialCall, ...rest }: import("./types").ReActorUseQueryArgs<A, M>) => {
|
|
15
19
|
data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined;
|
|
16
20
|
error: Error | undefined;
|
|
17
21
|
loading: boolean;
|
|
18
|
-
|
|
22
|
+
field: {
|
|
23
|
+
current: import("@ic-reactor/store").ReActorMethodField<A> | undefined;
|
|
24
|
+
};
|
|
25
|
+
call: (replaceArgs?: import("@ic-reactor/store").ExtractReActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined>;
|
|
19
26
|
};
|
|
20
27
|
useUpdateCall: <M_1 extends keyof A>(args: import("./types").ReActorUseUpdateArgs<A, M_1>) => {
|
|
21
28
|
data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M_1]> | undefined;
|
|
22
29
|
error: Error | undefined;
|
|
23
30
|
loading: boolean;
|
|
24
31
|
call: (replaceArgs?: import("@ic-reactor/store").ExtractReActorMethodArgs<A[M_1]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M_1]> | undefined>;
|
|
32
|
+
field: {
|
|
33
|
+
current: import("@ic-reactor/store").ReActorMethodField<A> | undefined;
|
|
34
|
+
};
|
|
25
35
|
};
|
|
26
36
|
useAuthClient: () => {
|
|
27
37
|
authClient: import("@dfinity/auth-client").AuthClient | null;
|
package/dist/index.js
CHANGED
|
@@ -14,16 +14,13 @@ const store_1 = require("@ic-reactor/store");
|
|
|
14
14
|
const react_1 = require("react");
|
|
15
15
|
const zustand_1 = require("zustand");
|
|
16
16
|
const hooks_1 = require("./hooks");
|
|
17
|
-
const isLocal = (process === null || process === void 0 ? void 0 : process.env.NODE_ENV) === "development" ||
|
|
18
|
-
(process === null || process === void 0 ? void 0 : process.env.DFX_NETWORK) === "local";
|
|
19
17
|
const createReActor = (options) => {
|
|
20
|
-
const
|
|
18
|
+
const isLocal = typeof process !== "undefined" &&
|
|
19
|
+
(process.env.NODE_ENV === "development" ||
|
|
20
|
+
process.env.DFX_NETWORK === "local");
|
|
21
|
+
const { callMethod, initialize, authenticate, authStore, actorStore } = (0, store_1.createReActorStore)(Object.assign({ isLocal }, options));
|
|
21
22
|
const useActorStore = () => {
|
|
22
23
|
const actorState = (0, zustand_1.useStore)(actorStore, (state) => state);
|
|
23
|
-
(0, react_1.useEffect)(() => {
|
|
24
|
-
initialize();
|
|
25
|
-
return unsubscribe;
|
|
26
|
-
}, []);
|
|
27
24
|
return Object.assign(Object.assign({}, actorState), { initialize });
|
|
28
25
|
};
|
|
29
26
|
const useAuthStore = () => {
|
|
@@ -76,8 +73,9 @@ const createReActor = (options) => {
|
|
|
76
73
|
loginError,
|
|
77
74
|
};
|
|
78
75
|
};
|
|
79
|
-
const { useQueryCall, useUpdateCall } = (0, hooks_1.getCallHooks)(callMethod);
|
|
76
|
+
const { useQueryCall, useField, useUpdateCall } = (0, hooks_1.getCallHooks)(callMethod, actorStore);
|
|
80
77
|
return {
|
|
78
|
+
useField,
|
|
81
79
|
useActorStore,
|
|
82
80
|
useAuthStore,
|
|
83
81
|
useQueryCall,
|
package/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ExtractReActorMethodArgs, ExtractReActorMethodReturnType } from "@ic-reactor/store";
|
|
2
2
|
export type ReActorCallArgs<A, M extends keyof A> = {
|
|
3
|
-
functionName: M;
|
|
3
|
+
functionName: M & string;
|
|
4
4
|
args?: ExtractReActorMethodArgs<A[M]>;
|
|
5
5
|
onLoading?: (loading: boolean) => void;
|
|
6
6
|
onError?: (error: Error | unknown) => void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/react",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "A React library for interacting with Dfinity actors",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -28,25 +28,25 @@
|
|
|
28
28
|
"scripts": {
|
|
29
29
|
"test": "npx jest",
|
|
30
30
|
"start": "tsc watch",
|
|
31
|
-
"build": "npx tsc",
|
|
32
|
-
"clean": "rimraf dist"
|
|
31
|
+
"build": "npx rimraf dist && npx tsc",
|
|
32
|
+
"clean": "npx rimraf dist"
|
|
33
33
|
},
|
|
34
34
|
"engines": {
|
|
35
35
|
"node": ">=10"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
+
"@ic-reactor/store": "^0.2.2",
|
|
39
|
+
"zustand-utils": "^1.3"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
38
42
|
"@dfinity/agent": "^0.20",
|
|
39
43
|
"@dfinity/auth-client": "^0.20",
|
|
40
44
|
"@dfinity/candid": "0.20",
|
|
41
45
|
"@dfinity/identity": "^0.20",
|
|
42
46
|
"@dfinity/principal": "^0.20",
|
|
43
|
-
"@ic-reactor/store": "^0.2.0",
|
|
44
|
-
"zustand": "4.4",
|
|
45
|
-
"zustand-utils": "^1.3"
|
|
46
|
-
},
|
|
47
|
-
"peerDependencies": {
|
|
48
47
|
"@peculiar/webcrypto": "1.4",
|
|
49
|
-
"react": ">=16.8"
|
|
48
|
+
"react": ">=16.8",
|
|
49
|
+
"zustand": "4.4"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "97c96f8577f6cf748fca237932c3903638002d1e"
|
|
52
52
|
}
|