@ic-reactor/react 1.2.3 → 1.3.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.
- package/dist/context/actor.js +5 -3
- package/dist/helpers/actorHooks.js +26 -5
- package/dist/helpers/types.d.ts +3 -2
- package/dist/hooks/actor/index.d.ts +1 -0
- package/dist/hooks/actor/index.js +1 -0
- package/dist/hooks/actor/useMethodNames.d.ts +2 -1
- package/dist/hooks/actor/useMethodNames.js +2 -1
- package/dist/hooks/useActor.js +13 -10
- package/package.json +3 -3
package/dist/context/actor.js
CHANGED
|
@@ -90,9 +90,11 @@ function createActorContext(contextConfig = {}) {
|
|
|
90
90
|
const config = react_1.default.useMemo(() => (Object.assign(Object.assign({}, defaultConfig), restConfig)), [defaultConfig, restConfig]);
|
|
91
91
|
const { fetchError, authenticating, hooks } = (0, useActor_1.useActor)(Object.assign({ canisterId }, config));
|
|
92
92
|
return (react_1.default.createElement(ActorContext.Provider, { value: hooks }, hooks === null
|
|
93
|
-
?
|
|
94
|
-
?
|
|
95
|
-
:
|
|
93
|
+
? fetchError
|
|
94
|
+
? fetchError
|
|
95
|
+
: authenticating
|
|
96
|
+
? authenticatingComponent
|
|
97
|
+
: loadingComponent
|
|
96
98
|
: children));
|
|
97
99
|
};
|
|
98
100
|
ActorProvider.displayName = "ActorProvider";
|
|
@@ -47,10 +47,10 @@ const DEFAULT_STATE = {
|
|
|
47
47
|
* Each hook is designed to simplify the process of interacting with actors in IC projects by abstracting away the complexity of state management, error handling, and method invocation.
|
|
48
48
|
*/
|
|
49
49
|
const actorHooks = (actorManager) => {
|
|
50
|
-
const { actorStore, canisterId, visitFunction,
|
|
50
|
+
const { actorStore, canisterId, visitFunction, methodAttributes, extractMethodAttributes, extractInterface, callMethod, initialize, } = actorManager;
|
|
51
51
|
const useActorState = () => (Object.assign(Object.assign({}, (0, zustand_1.useStore)(actorStore)), { canisterId }));
|
|
52
52
|
const useMethodNames = () => {
|
|
53
|
-
return react_1.default.useMemo(() =>
|
|
53
|
+
return react_1.default.useMemo(() => Object.keys(extractMethodAttributes()), []);
|
|
54
54
|
};
|
|
55
55
|
const useActorInterface = () => {
|
|
56
56
|
return react_1.default.useMemo(() => extractInterface(), []);
|
|
@@ -111,10 +111,31 @@ const actorHooks = (actorManager) => {
|
|
|
111
111
|
};
|
|
112
112
|
const useUpdateCall = useSharedCall;
|
|
113
113
|
const useMethod = (args) => {
|
|
114
|
-
const _a = useSharedCall(args), { call } = _a, state = __rest(_a, ["call"]);
|
|
115
114
|
const visit = useVisitMethod(args.functionName);
|
|
116
|
-
|
|
117
|
-
|
|
115
|
+
const attributes = methodAttributes[args.functionName];
|
|
116
|
+
let refetchOnMount = args.refetchOnMount;
|
|
117
|
+
let refetchInterval = args.refetchInterval;
|
|
118
|
+
let formRequired = true;
|
|
119
|
+
switch (attributes.type) {
|
|
120
|
+
case "query":
|
|
121
|
+
try {
|
|
122
|
+
if (attributes.numberOfArgs > 0 && args.args === undefined) {
|
|
123
|
+
throw new Error("Args required");
|
|
124
|
+
}
|
|
125
|
+
attributes.validate((args.args || []));
|
|
126
|
+
formRequired = args.refetchOnMount === false ? true : false;
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
refetchOnMount = false;
|
|
130
|
+
refetchInterval = false;
|
|
131
|
+
}
|
|
132
|
+
return Object.assign(Object.assign({ visit }, useQueryCall(Object.assign(Object.assign({}, args), { refetchOnMount,
|
|
133
|
+
refetchInterval }))), { formRequired });
|
|
134
|
+
case "update":
|
|
135
|
+
return Object.assign(Object.assign({ visit }, useUpdateCall(args)), { formRequired });
|
|
136
|
+
default:
|
|
137
|
+
throw new Error(`Method type ${attributes.type} not found`);
|
|
138
|
+
}
|
|
118
139
|
};
|
|
119
140
|
return {
|
|
120
141
|
initialize,
|
package/dist/helpers/types.d.ts
CHANGED
|
@@ -71,10 +71,11 @@ export interface DynamicDataArgs<V = unknown> {
|
|
|
71
71
|
label: string;
|
|
72
72
|
value: V;
|
|
73
73
|
}
|
|
74
|
-
export interface UseMethodParameters<A, M extends FunctionName<A>> extends
|
|
74
|
+
export interface UseMethodParameters<A, M extends FunctionName<A>> extends UseQueryCallParameters<A, M> {
|
|
75
75
|
}
|
|
76
76
|
export interface UseMethodReturnType<A, M extends FunctionName<A> = FunctionName<A>> {
|
|
77
77
|
loading: boolean;
|
|
78
|
+
formRequired: boolean;
|
|
78
79
|
error: Error | undefined;
|
|
79
80
|
data: ActorMethodReturnType<A[M]> | undefined;
|
|
80
81
|
visit: VisitService<A>[M];
|
|
@@ -88,7 +89,7 @@ export interface ActorHooksReturnType<A = BaseActor> {
|
|
|
88
89
|
initialize: () => Promise<void>;
|
|
89
90
|
useActorState: () => UseActorState;
|
|
90
91
|
useActorInterface: () => ServiceClass;
|
|
91
|
-
useMethodNames: () => FunctionName<
|
|
92
|
+
useMethodNames: <Actor = A>() => FunctionName<Actor>[];
|
|
92
93
|
useMethod: UseMethod<A>;
|
|
93
94
|
useQueryCall: UseQueryCall<A>;
|
|
94
95
|
useUpdateCall: UseUpdateCall<A>;
|
|
@@ -2,6 +2,7 @@ export * from "./useMethod";
|
|
|
2
2
|
export * from "./useActorState";
|
|
3
3
|
export * from "./useQueryCall";
|
|
4
4
|
export * from "./useUpdateCall";
|
|
5
|
+
export * from "./useMethodNames";
|
|
5
6
|
export * from "./useVisitMethod";
|
|
6
7
|
export * from "./useVisitService";
|
|
7
8
|
export * from "./useActorInterface";
|
|
@@ -18,6 +18,7 @@ __exportStar(require("./useMethod"), exports);
|
|
|
18
18
|
__exportStar(require("./useActorState"), exports);
|
|
19
19
|
__exportStar(require("./useQueryCall"), exports);
|
|
20
20
|
__exportStar(require("./useUpdateCall"), exports);
|
|
21
|
+
__exportStar(require("./useMethodNames"), exports);
|
|
21
22
|
__exportStar(require("./useVisitMethod"), exports);
|
|
22
23
|
__exportStar(require("./useVisitService"), exports);
|
|
23
24
|
__exportStar(require("./useActorInterface"), exports);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { BaseActor } from "../../types";
|
|
1
2
|
/**
|
|
2
3
|
* Hook for accessing the method names of an actor.
|
|
3
4
|
*
|
|
4
5
|
* @returns An array of method names for the actor.
|
|
5
6
|
*/
|
|
6
|
-
export declare const useMethodNames: () => string[];
|
|
7
|
+
export declare const useMethodNames: <A = BaseActor>() => Extract<keyof A, string>[];
|
|
@@ -7,4 +7,5 @@ const hooks_1 = require("./hooks");
|
|
|
7
7
|
*
|
|
8
8
|
* @returns An array of method names for the actor.
|
|
9
9
|
*/
|
|
10
|
-
|
|
10
|
+
const useMethodNames = () => hooks_1.ActorHooks.useMethodNames();
|
|
11
|
+
exports.useMethodNames = useMethodNames;
|
package/dist/hooks/useActor.js
CHANGED
|
@@ -129,30 +129,33 @@ const useActor = (config) => {
|
|
|
129
129
|
});
|
|
130
130
|
}
|
|
131
131
|
}), [canisterId, authenticating, didjsCanisterId]);
|
|
132
|
+
const initialActorManager = (0, react_1.useCallback)((idlFactory) => {
|
|
133
|
+
if (authenticating)
|
|
134
|
+
return;
|
|
135
|
+
const actorManager = (0, core_1.createActorManager)(Object.assign({ agentManager,
|
|
136
|
+
idlFactory,
|
|
137
|
+
canisterId }, actorConfig));
|
|
138
|
+
setActorManager(() => actorManager);
|
|
139
|
+
}, [canisterId, agentManager, authenticating]);
|
|
132
140
|
(0, react_1.useEffect)(() => {
|
|
133
141
|
if (maybeIdlFactory) {
|
|
134
|
-
|
|
135
|
-
setActorManager(() => actorManager);
|
|
142
|
+
initialActorManager(maybeIdlFactory);
|
|
136
143
|
}
|
|
137
144
|
else {
|
|
145
|
+
setActorManager(undefined);
|
|
138
146
|
fetchCandid().then((idlFactory) => {
|
|
139
147
|
if (!idlFactory)
|
|
140
148
|
return;
|
|
141
|
-
|
|
142
|
-
idlFactory,
|
|
143
|
-
canisterId }, actorConfig));
|
|
144
|
-
setActorManager(() => actorManager);
|
|
149
|
+
initialActorManager(idlFactory);
|
|
145
150
|
});
|
|
146
151
|
}
|
|
147
152
|
return actorManager === null || actorManager === void 0 ? void 0 : actorManager.cleanup();
|
|
148
|
-
}, [fetchCandid, maybeIdlFactory,
|
|
153
|
+
}, [fetchCandid, maybeIdlFactory, initialActorManager]);
|
|
149
154
|
const hooks = (0, react_1.useMemo)(() => {
|
|
150
|
-
if (fetching)
|
|
151
|
-
return null;
|
|
152
155
|
if (!actorManager)
|
|
153
156
|
return null;
|
|
154
157
|
return (0, helpers_1.actorHooks)(actorManager);
|
|
155
|
-
}, [
|
|
158
|
+
}, [actorManager]);
|
|
156
159
|
return { hooks, authenticating, fetching, fetchError };
|
|
157
160
|
};
|
|
158
161
|
exports.useActor = useActor;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "A React library for interacting with Internet Computer canisters",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"node": ">=10"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@ic-reactor/core": "^1.
|
|
38
|
+
"@ic-reactor/core": "^1.3.1",
|
|
39
39
|
"zustand-utils": "^1.3"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"react": ">=16.8",
|
|
48
48
|
"zustand": "4.5"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "05130e2581edfafd6390cf04ba618cb084bafc31"
|
|
51
51
|
}
|