@ic-reactor/react 1.1.4 → 1.1.6
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.d.ts +1 -1
- package/dist/context/actor.js +3 -3
- package/dist/helpers/actorHooks.js +27 -11
- package/dist/helpers/extractActorContext.js +4 -0
- package/dist/helpers/types.d.ts +26 -8
- package/dist/hooks/actor/index.d.ts +2 -0
- package/dist/hooks/actor/index.js +2 -0
- package/dist/hooks/actor/useActorInterface.d.ts +4 -0
- package/dist/hooks/actor/useActorInterface.js +11 -0
- package/dist/hooks/actor/useMethod.d.ts +28 -0
- package/dist/hooks/actor/useMethod.js +32 -0
- package/dist/hooks/actor/useQueryCall.d.ts +1 -1
- package/dist/hooks/actor/useUpdateCall.d.ts +1 -1
- package/dist/hooks/types.d.ts +0 -1
- package/dist/hooks/useActor.d.ts +1 -1
- package/dist/hooks/useActor.js +28 -25
- package/package.json +3 -3
package/dist/context/actor.d.ts
CHANGED
|
@@ -61,4 +61,4 @@ import { CreateActorContextParameters, CreateActorContextReturnType } from "./ty
|
|
|
61
61
|
* to manage actor state and perform actions such as queries or updates. It abstracts away the complexities involved in directly
|
|
62
62
|
* managing IC agents and actors, providing a simple, declarative API for developers.
|
|
63
63
|
*/
|
|
64
|
-
export declare function createActorContext<A = BaseActor>(
|
|
64
|
+
export declare function createActorContext<A = BaseActor>(contextConfig?: CreateActorContextParameters): CreateActorContextReturnType<A>;
|
package/dist/context/actor.js
CHANGED
|
@@ -79,8 +79,8 @@ const extractActorContext_1 = require("../helpers/extractActorContext");
|
|
|
79
79
|
* to manage actor state and perform actions such as queries or updates. It abstracts away the complexities involved in directly
|
|
80
80
|
* managing IC agents and actors, providing a simple, declarative API for developers.
|
|
81
81
|
*/
|
|
82
|
-
function createActorContext(
|
|
83
|
-
const { canisterId: defaultCanisterId } =
|
|
82
|
+
function createActorContext(contextConfig = {}) {
|
|
83
|
+
const { canisterId: defaultCanisterId } = contextConfig, defaultConfig = __rest(contextConfig, ["canisterId"]);
|
|
84
84
|
const ActorContext = react_1.default.createContext(null);
|
|
85
85
|
const ActorProvider = (_a) => {
|
|
86
86
|
var { children, canisterId = defaultCanisterId, loadingComponent = react_1.default.createElement("div", null, "Fetching canister..."), authenticatingComponent = react_1.default.createElement("div", null, "Authenticating...") } = _a, restConfig = __rest(_a, ["children", "canisterId", "loadingComponent", "authenticatingComponent"]);
|
|
@@ -88,7 +88,7 @@ function createActorContext(config = {}) {
|
|
|
88
88
|
throw new Error("canisterId is required");
|
|
89
89
|
}
|
|
90
90
|
const config = react_1.default.useMemo(() => (Object.assign(Object.assign({}, defaultConfig), restConfig)), [defaultConfig, restConfig]);
|
|
91
|
-
const { fetchError, authenticating, hooks } = (0, useActor_1.useActor)(Object.assign({ canisterId
|
|
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
93
|
? (fetchError !== null && fetchError !== void 0 ? fetchError : authenticating)
|
|
94
94
|
? authenticatingComponent
|
|
@@ -47,26 +47,34 @@ 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, callMethod, initialize } = actorManager;
|
|
50
|
+
const { actorStore, canisterId, visitFunction, extractInterface, callMethod, initialize, } = actorManager;
|
|
51
51
|
const useActorState = () => (Object.assign(Object.assign({}, (0, zustand_1.useStore)(actorStore)), { canisterId }));
|
|
52
|
+
const useActorInterface = () => {
|
|
53
|
+
return extractInterface();
|
|
54
|
+
};
|
|
52
55
|
const useVisitService = () => {
|
|
53
56
|
return visitFunction;
|
|
54
57
|
};
|
|
55
58
|
const useVisitMethod = (functionName) => {
|
|
56
|
-
return react_1.default.useMemo(() =>
|
|
59
|
+
return react_1.default.useMemo(() => {
|
|
60
|
+
if (!visitFunction[functionName]) {
|
|
61
|
+
throw new Error(`Method ${functionName} not found`);
|
|
62
|
+
}
|
|
63
|
+
return visitFunction[functionName];
|
|
64
|
+
}, [functionName]);
|
|
57
65
|
};
|
|
58
|
-
const
|
|
66
|
+
const useSharedCall = (_a) => {
|
|
59
67
|
var { args = [], functionName, throwOnError = false } = _a, events = __rest(_a, ["args", "functionName", "throwOnError"]);
|
|
60
|
-
const [
|
|
61
|
-
const reset = react_1.default.useCallback(() =>
|
|
68
|
+
const [sharedState, setSharedState] = react_1.default.useState(DEFAULT_STATE);
|
|
69
|
+
const reset = react_1.default.useCallback(() => setSharedState(DEFAULT_STATE), []);
|
|
62
70
|
const call = react_1.default.useCallback((eventOrReplaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
63
71
|
var _b, _c, _d, _e, _f;
|
|
64
|
-
|
|
72
|
+
setSharedState((prev) => (Object.assign(Object.assign({}, prev), { error: undefined, loading: true })));
|
|
65
73
|
(_b = events === null || events === void 0 ? void 0 : events.onLoading) === null || _b === void 0 ? void 0 : _b.call(events, true);
|
|
66
74
|
try {
|
|
67
75
|
const replaceArgs = eventOrReplaceArgs instanceof Array ? eventOrReplaceArgs : args;
|
|
68
76
|
const data = yield callMethod(functionName, ...replaceArgs);
|
|
69
|
-
|
|
77
|
+
setSharedState({ data, error: undefined, loading: false });
|
|
70
78
|
(_c = events === null || events === void 0 ? void 0 : events.onSuccess) === null || _c === void 0 ? void 0 : _c.call(events, data);
|
|
71
79
|
(_d = events === null || events === void 0 ? void 0 : events.onLoading) === null || _d === void 0 ? void 0 : _d.call(events, false);
|
|
72
80
|
return data;
|
|
@@ -74,18 +82,18 @@ const actorHooks = (actorManager) => {
|
|
|
74
82
|
catch (error) {
|
|
75
83
|
// eslint-disable-next-line no-console
|
|
76
84
|
console.error("Error in call:", error);
|
|
77
|
-
|
|
85
|
+
setSharedState((prevState) => (Object.assign(Object.assign({}, prevState), { error: error, loading: false })));
|
|
78
86
|
(_e = events === null || events === void 0 ? void 0 : events.onError) === null || _e === void 0 ? void 0 : _e.call(events, error);
|
|
79
87
|
(_f = events === null || events === void 0 ? void 0 : events.onLoading) === null || _f === void 0 ? void 0 : _f.call(events, false);
|
|
80
88
|
if (throwOnError)
|
|
81
89
|
throw error;
|
|
82
90
|
}
|
|
83
91
|
}), [args, functionName, events]);
|
|
84
|
-
return Object.assign({ call, reset },
|
|
92
|
+
return Object.assign({ call, reset }, sharedState);
|
|
85
93
|
};
|
|
86
94
|
const useQueryCall = (_a) => {
|
|
87
95
|
var { refetchOnMount = true, refetchInterval = false } = _a, rest = __rest(_a, ["refetchOnMount", "refetchInterval"]);
|
|
88
|
-
const _b =
|
|
96
|
+
const _b = useSharedCall(rest), { call } = _b, state = __rest(_b, ["call"]);
|
|
89
97
|
const intervalId = react_1.default.useRef();
|
|
90
98
|
react_1.default.useEffect(() => {
|
|
91
99
|
if (refetchInterval) {
|
|
@@ -98,14 +106,22 @@ const actorHooks = (actorManager) => {
|
|
|
98
106
|
}, [refetchInterval, refetchOnMount]);
|
|
99
107
|
return Object.assign({ call }, state);
|
|
100
108
|
};
|
|
101
|
-
const useUpdateCall =
|
|
109
|
+
const useUpdateCall = useSharedCall;
|
|
110
|
+
const useMethod = (args) => {
|
|
111
|
+
const _a = useSharedCall(args), { call } = _a, state = __rest(_a, ["call"]);
|
|
112
|
+
const visit = useVisitMethod(args.functionName);
|
|
113
|
+
return Object.assign({ call,
|
|
114
|
+
visit }, state);
|
|
115
|
+
};
|
|
102
116
|
return {
|
|
103
117
|
initialize,
|
|
118
|
+
useMethod,
|
|
104
119
|
useQueryCall,
|
|
105
120
|
useUpdateCall,
|
|
106
121
|
useActorState,
|
|
107
122
|
useVisitMethod,
|
|
108
123
|
useVisitService,
|
|
124
|
+
useActorInterface,
|
|
109
125
|
};
|
|
110
126
|
};
|
|
111
127
|
exports.actorHooks = actorHooks;
|
|
@@ -40,16 +40,20 @@ function extractActorContext(actorContext) {
|
|
|
40
40
|
};
|
|
41
41
|
const initialize = () => useActorContext().initialize();
|
|
42
42
|
const useActorState = () => useActorContext().useActorState();
|
|
43
|
+
const useMethod = (args) => useActorContext().useMethod(args);
|
|
43
44
|
const useQueryCall = (args) => useActorContext().useQueryCall(args);
|
|
44
45
|
const useUpdateCall = (args) => useActorContext().useUpdateCall(args);
|
|
45
46
|
const useVisitMethod = (functionName) => useActorContext().useVisitMethod(functionName);
|
|
46
47
|
const useVisitService = () => useActorContext().useVisitService();
|
|
48
|
+
const useActorInterface = () => useActorContext().useActorInterface();
|
|
47
49
|
return {
|
|
48
50
|
useActorState,
|
|
51
|
+
useMethod,
|
|
49
52
|
useQueryCall,
|
|
50
53
|
useUpdateCall,
|
|
51
54
|
useVisitMethod,
|
|
52
55
|
useVisitService,
|
|
56
|
+
useActorInterface,
|
|
53
57
|
initialize,
|
|
54
58
|
};
|
|
55
59
|
}
|
package/dist/helpers/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
+
import type { ServiceClass } from "@dfinity/candid/lib/cjs/idl";
|
|
2
3
|
import type { ActorState, CanisterId, AuthClientLoginOptions, ActorMethodParameters, ActorMethodReturnType, Identity, Principal, FunctionName, VisitService, AuthState, HttpAgent, AgentState, BaseActor } from "@ic-reactor/core/dist/types";
|
|
3
4
|
export interface AgentHooksReturnType {
|
|
4
5
|
useAgent: () => HttpAgent | undefined;
|
|
@@ -40,7 +41,7 @@ export type LogoutParameters = {
|
|
|
40
41
|
export interface UseActorState extends Omit<ActorState, "methodState"> {
|
|
41
42
|
canisterId: CanisterId;
|
|
42
43
|
}
|
|
43
|
-
export type
|
|
44
|
+
export type UseSharedCallParameters<A, M extends FunctionName<A>> = {
|
|
44
45
|
functionName: M;
|
|
45
46
|
args?: ActorMethodParameters<A[M]>;
|
|
46
47
|
onLoading?: (loading: boolean) => void;
|
|
@@ -48,29 +49,46 @@ export type UseMethodCallParameters<A, M extends FunctionName<A>> = {
|
|
|
48
49
|
onSuccess?: (data: ActorMethodReturnType<A[M]> | undefined) => void;
|
|
49
50
|
throwOnError?: boolean;
|
|
50
51
|
};
|
|
51
|
-
export type
|
|
52
|
+
export type UseSharedCallState<A, M extends FunctionName<A>> = {
|
|
52
53
|
data: ActorMethodReturnType<A[M]> | undefined;
|
|
53
54
|
error: Error | undefined;
|
|
54
55
|
loading: boolean;
|
|
55
56
|
};
|
|
56
|
-
export interface
|
|
57
|
+
export interface UseSharedCallReturnType<A, M extends FunctionName<A> = FunctionName<A>> extends UseSharedCallState<A, M> {
|
|
58
|
+
reset: () => void;
|
|
59
|
+
call: (eventOrReplaceArgs?: React.MouseEvent | ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]> | undefined>;
|
|
60
|
+
}
|
|
61
|
+
export type UseSharedCall<A> = <M extends FunctionName<A>>(args: UseSharedCallParameters<A, M>) => UseSharedCallReturnType<A, M>;
|
|
62
|
+
export interface UseQueryCallParameters<A, M extends FunctionName<A>> extends UseSharedCallParameters<A, M> {
|
|
57
63
|
refetchOnMount?: boolean;
|
|
58
64
|
refetchInterval?: number | false;
|
|
59
65
|
}
|
|
60
|
-
export
|
|
66
|
+
export type UseQueryCall<A> = <M extends FunctionName<A>>(args: UseQueryCallParameters<A, M>) => UseSharedCallReturnType<A, M>;
|
|
67
|
+
export interface UseUpdateCallParameters<A, M extends FunctionName<A>> extends UseSharedCallParameters<A, M> {
|
|
68
|
+
}
|
|
69
|
+
export type UseUpdateCall<A> = <M extends FunctionName<A>>(args: UseUpdateCallParameters<A, M>) => UseSharedCallReturnType<A, M>;
|
|
70
|
+
export interface DynamicDataArgs<V = unknown> {
|
|
71
|
+
label: string;
|
|
72
|
+
value: V;
|
|
61
73
|
}
|
|
62
|
-
export interface
|
|
74
|
+
export interface UseMethodParameters<A, M extends FunctionName<A>> extends UseSharedCallParameters<A, M> {
|
|
75
|
+
}
|
|
76
|
+
export interface UseMethodReturnType<A, M extends FunctionName<A> = FunctionName<A>> {
|
|
77
|
+
loading: boolean;
|
|
78
|
+
error: Error | undefined;
|
|
79
|
+
data: ActorMethodReturnType<A[M]> | undefined;
|
|
80
|
+
visit: VisitService<A>[M];
|
|
63
81
|
reset: () => void;
|
|
64
82
|
call: (eventOrReplaceArgs?: React.MouseEvent | ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]> | undefined>;
|
|
65
83
|
}
|
|
66
|
-
export type
|
|
67
|
-
export type UseQueryCall<A> = <M extends FunctionName<A>>(args: UseQueryCallParameters<A, M>) => UseMethodCallReturnType<A, M>;
|
|
68
|
-
export type UseUpdateCall<A> = <M extends FunctionName<A>>(args: UseUpdateCallParameters<A, M>) => UseMethodCallReturnType<A, M>;
|
|
84
|
+
export type UseMethod<A> = <M extends FunctionName<A>>(args: UseMethodParameters<A, M>) => UseMethodReturnType<A, M>;
|
|
69
85
|
export type UseVisitMethod<A> = <M extends FunctionName<A>>(functionName: M) => VisitService<A>[M];
|
|
70
86
|
export type UseVisitService<A> = () => VisitService<A>;
|
|
71
87
|
export interface ActorHooksReturnType<A = BaseActor> {
|
|
72
88
|
initialize: () => Promise<void>;
|
|
73
89
|
useActorState: () => UseActorState;
|
|
90
|
+
useActorInterface: () => ServiceClass;
|
|
91
|
+
useMethod: UseMethod<A>;
|
|
74
92
|
useQueryCall: UseQueryCall<A>;
|
|
75
93
|
useUpdateCall: UseUpdateCall<A>;
|
|
76
94
|
useVisitMethod: UseVisitMethod<A>;
|
|
@@ -14,8 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./useMethod"), exports);
|
|
17
18
|
__exportStar(require("./useActorState"), exports);
|
|
18
19
|
__exportStar(require("./useQueryCall"), exports);
|
|
19
20
|
__exportStar(require("./useUpdateCall"), exports);
|
|
20
21
|
__exportStar(require("./useVisitMethod"), exports);
|
|
21
22
|
__exportStar(require("./useVisitService"), exports);
|
|
23
|
+
__exportStar(require("./useActorInterface"), exports);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useActorInterface = void 0;
|
|
4
|
+
const hooks_1 = require("./hooks");
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
function useActorInterface() {
|
|
9
|
+
return hooks_1.ActorHooks.useActorInterface();
|
|
10
|
+
}
|
|
11
|
+
exports.useActorInterface = useActorInterface;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook for making update calls to actors, handling loading states, and managing errors. It supports custom event handlers for loading, success, and error events.
|
|
3
|
+
*
|
|
4
|
+
* @param options Configuration object for the actor method call, including the method name, arguments, and event handlers.
|
|
5
|
+
* @returns An object containing the method call function, a reset function to reset the call state to its default, and the current call state (data, error, loading, call, reset).
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* function UpdateCallComponent() {
|
|
9
|
+
* const { call, data, loading } = useUpdateCall({
|
|
10
|
+
* functionName: 'updateUserProfile',
|
|
11
|
+
* args: ['123', { name: 'John Doe' }],
|
|
12
|
+
* onLoading: (loading) => console.log('Loading:', loading),
|
|
13
|
+
* onError: (error) => console.error('Error:', error),
|
|
14
|
+
* onSuccess: (data) => console.log('Success:', data),
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* if (loading) return <p>Updating profile...</p>;
|
|
18
|
+
*
|
|
19
|
+
* return (
|
|
20
|
+
* <div>
|
|
21
|
+
* <p>Updated Profile: {JSON.stringify(data)}</p>
|
|
22
|
+
* <button onClick={call}>Update</button>
|
|
23
|
+
* </div>
|
|
24
|
+
* );
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare const useMethod: import("../../types").UseMethod<import("@ic-reactor/core/dist/types").BaseActor>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useMethod = void 0;
|
|
4
|
+
const hooks_1 = require("./hooks");
|
|
5
|
+
/**
|
|
6
|
+
* Hook for making update calls to actors, handling loading states, and managing errors. It supports custom event handlers for loading, success, and error events.
|
|
7
|
+
*
|
|
8
|
+
* @param options Configuration object for the actor method call, including the method name, arguments, and event handlers.
|
|
9
|
+
* @returns An object containing the method call function, a reset function to reset the call state to its default, and the current call state (data, error, loading, call, reset).
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* function UpdateCallComponent() {
|
|
13
|
+
* const { call, data, loading } = useUpdateCall({
|
|
14
|
+
* functionName: 'updateUserProfile',
|
|
15
|
+
* args: ['123', { name: 'John Doe' }],
|
|
16
|
+
* onLoading: (loading) => console.log('Loading:', loading),
|
|
17
|
+
* onError: (error) => console.error('Error:', error),
|
|
18
|
+
* onSuccess: (data) => console.log('Success:', data),
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* if (loading) return <p>Updating profile...</p>;
|
|
22
|
+
*
|
|
23
|
+
* return (
|
|
24
|
+
* <div>
|
|
25
|
+
* <p>Updated Profile: {JSON.stringify(data)}</p>
|
|
26
|
+
* <button onClick={call}>Update</button>
|
|
27
|
+
* </div>
|
|
28
|
+
* );
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
exports.useMethod = hooks_1.ActorHooks.useMethod;
|
|
@@ -25,4 +25,4 @@ import { BaseActor, FunctionName, UseQueryCallParameters } from "../../types";
|
|
|
25
25
|
* }
|
|
26
26
|
* ```
|
|
27
27
|
*/
|
|
28
|
-
export declare function useQueryCall<A = BaseActor, M extends FunctionName<A> = FunctionName<A>>(args: UseQueryCallParameters<A, M>): import("../../types").
|
|
28
|
+
export declare function useQueryCall<A = BaseActor, M extends FunctionName<A> = FunctionName<A>>(args: UseQueryCallParameters<A, M>): import("../../types").UseSharedCallReturnType<A, M>;
|
|
@@ -26,4 +26,4 @@ import { BaseActor, FunctionName, UseUpdateCallParameters } from "../../types";
|
|
|
26
26
|
* }
|
|
27
27
|
* ```
|
|
28
28
|
*/
|
|
29
|
-
export declare function useUpdateCall<A = BaseActor, M extends FunctionName<A> = FunctionName<A>>(args: UseUpdateCallParameters<A, M>): import("../../types").
|
|
29
|
+
export declare function useUpdateCall<A = BaseActor, M extends FunctionName<A> = FunctionName<A>>(args: UseUpdateCallParameters<A, M>): import("../../types").UseSharedCallReturnType<A, M>;
|
package/dist/hooks/types.d.ts
CHANGED
|
@@ -6,7 +6,6 @@ export interface UseActorParameters extends Omit<ActorManagerParameters, "idlFac
|
|
|
6
6
|
canisterId: string;
|
|
7
7
|
idlFactory?: IDL.InterfaceFactory;
|
|
8
8
|
agentContext?: React.Context<AgentContext | null>;
|
|
9
|
-
fetchOnMount?: boolean;
|
|
10
9
|
didjsCanisterId?: string;
|
|
11
10
|
}
|
|
12
11
|
export interface UseActorReturn<A = BaseActor> {
|
package/dist/hooks/useActor.d.ts
CHANGED
package/dist/hooks/useActor.js
CHANGED
|
@@ -89,21 +89,25 @@ const agent_1 = require("./agent");
|
|
|
89
89
|
* ```
|
|
90
90
|
*/
|
|
91
91
|
const useActor = (config) => {
|
|
92
|
-
const { canisterId, idlFactory: maybeIdlFactory, agentContext,
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
const { canisterId, idlFactory: maybeIdlFactory, agentContext, didjsCanisterId } = config, actorConfig = __rest(config, ["canisterId", "idlFactory", "agentContext", "didjsCanisterId"]);
|
|
93
|
+
if (!canisterId) {
|
|
94
|
+
throw new Error("canisterId is required");
|
|
95
|
+
}
|
|
96
|
+
const [actorManager, setActorManager] = (0, react_1.useState)();
|
|
97
|
+
const [{ fetching, fetchError }, setState] = (0, react_1.useState)({
|
|
95
98
|
fetching: false,
|
|
96
99
|
fetchError: null,
|
|
97
100
|
});
|
|
98
101
|
const agentManager = (0, useAgentManager_1.useAgentManager)(agentContext);
|
|
99
|
-
const
|
|
100
|
-
|
|
102
|
+
const authenticating = (0, agent_1.useAuthState)().authenticating;
|
|
103
|
+
const fetchCandid = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
104
|
+
if (fetching || authenticating)
|
|
101
105
|
return;
|
|
102
106
|
setState({
|
|
103
|
-
idlFactory: undefined,
|
|
104
107
|
fetching: true,
|
|
105
108
|
fetchError: null,
|
|
106
109
|
});
|
|
110
|
+
const agent = agentManager.getAgent();
|
|
107
111
|
try {
|
|
108
112
|
const candidManager = (0, core_1.createCandidAdapter)({
|
|
109
113
|
agent,
|
|
@@ -111,7 +115,6 @@ const useActor = (config) => {
|
|
|
111
115
|
});
|
|
112
116
|
const { idlFactory } = yield candidManager.getCandidDefinition(canisterId);
|
|
113
117
|
setState({
|
|
114
|
-
idlFactory,
|
|
115
118
|
fetching: false,
|
|
116
119
|
fetchError: null,
|
|
117
120
|
});
|
|
@@ -121,35 +124,35 @@ const useActor = (config) => {
|
|
|
121
124
|
// eslint-disable-next-line no-console
|
|
122
125
|
console.error(err);
|
|
123
126
|
setState({
|
|
124
|
-
idlFactory: undefined,
|
|
125
127
|
fetchError: `Error fetching canister ${canisterId}`,
|
|
126
128
|
fetching: false,
|
|
127
129
|
});
|
|
128
130
|
}
|
|
129
|
-
}), [canisterId, didjsCanisterId]);
|
|
131
|
+
}), [canisterId, authenticating, didjsCanisterId]);
|
|
130
132
|
(0, react_1.useEffect)(() => {
|
|
131
133
|
if (maybeIdlFactory) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
fetching: false,
|
|
135
|
-
fetchError: null,
|
|
136
|
-
}));
|
|
137
|
-
return;
|
|
134
|
+
const actorManager = (0, core_1.createActorManager)(Object.assign({ agentManager, idlFactory: maybeIdlFactory, canisterId }, actorConfig));
|
|
135
|
+
setActorManager(() => actorManager);
|
|
138
136
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
137
|
+
else {
|
|
138
|
+
fetchCandid().then((idlFactory) => {
|
|
139
|
+
if (!idlFactory)
|
|
140
|
+
return;
|
|
141
|
+
const actorManager = (0, core_1.createActorManager)(Object.assign({ agentManager,
|
|
142
|
+
idlFactory,
|
|
143
|
+
canisterId }, actorConfig));
|
|
144
|
+
setActorManager(() => actorManager);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
return actorManager === null || actorManager === void 0 ? void 0 : actorManager.cleanup();
|
|
148
|
+
}, [fetchCandid, maybeIdlFactory, canisterId, agentManager]);
|
|
143
149
|
const hooks = (0, react_1.useMemo)(() => {
|
|
144
|
-
if (
|
|
150
|
+
if (fetching)
|
|
145
151
|
return null;
|
|
146
|
-
if (
|
|
152
|
+
if (!actorManager)
|
|
147
153
|
return null;
|
|
148
|
-
const actorManager = (0, core_1.createActorManager)(Object.assign({ agentManager,
|
|
149
|
-
idlFactory,
|
|
150
|
-
canisterId }, actorConfig));
|
|
151
154
|
return (0, helpers_1.actorHooks)(actorManager);
|
|
152
|
-
}, [canisterId, authenticating,
|
|
155
|
+
}, [canisterId, authenticating, actorManager, fetching]);
|
|
153
156
|
return { hooks, authenticating, fetching, fetchError };
|
|
154
157
|
};
|
|
155
158
|
exports.useActor = useActor;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/react",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.6",
|
|
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.1.
|
|
38
|
+
"@ic-reactor/core": "^1.1.3",
|
|
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": "4181bc6228d5efb98f14c48b2855a37bcada97dd"
|
|
51
51
|
}
|