@ic-reactor/react 2.0.0-alpha.0 → 2.0.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/context/actor/create.js +28 -27
- package/dist/context/actor/hooks/useActorInterface.d.ts +1 -1
- package/dist/context/actor/hooks/useActorInterface.js +1 -2
- package/dist/context/actor/hooks/useMethod.d.ts +3 -3
- package/dist/context/actor/hooks/useMethod.js +4 -5
- package/dist/context/actor/hooks/useQueryCall.d.ts +5 -5
- package/dist/context/actor/hooks/useQueryCall.js +4 -5
- package/dist/context/actor/hooks/useUpdateCall.d.ts +6 -6
- package/dist/context/actor/hooks/useUpdateCall.js +5 -6
- package/dist/context/actor/hooks/useVisitMethod.d.ts +1 -1
- package/dist/context/actor/hooks/useVisitMethod.js +1 -2
- package/dist/context/actor/hooks/useVisitService.js +1 -2
- package/dist/context/actor/provider.d.ts +22 -1
- package/dist/context/actor/provider.js +23 -1
- package/dist/context/actor/types.d.ts +6 -2
- package/dist/context/adapter/create.d.ts +2 -2
- package/dist/context/adapter/create.js +24 -32
- package/dist/context/adapter/index.d.ts +1 -2
- package/dist/context/adapter/provider.d.ts +0 -1
- package/dist/context/adapter/types.d.ts +33 -6
- package/dist/context/agent/create.js +11 -19
- package/dist/context/agent/hooks/useAuth.d.ts +1 -1
- package/dist/context/agent/index.d.ts +2 -1
- package/dist/context/agent/index.js +2 -1
- package/dist/context/agent/provider.d.ts +0 -1
- package/dist/createReactor.js +7 -2
- package/dist/helpers/actorHooks.js +111 -59
- package/dist/helpers/authHooks.js +46 -51
- package/dist/helpers/extractActorContext.d.ts +1 -1
- package/dist/helpers/extractActorContext.js +2 -3
- package/dist/helpers/types.d.ts +138 -20
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/index.js +1 -0
- package/dist/hooks/types.d.ts +9 -4
- package/dist/hooks/useActor.js +37 -38
- package/dist/hooks/useActorManager.d.ts +68 -0
- package/dist/hooks/useActorManager.js +75 -0
- package/dist/index.js +17 -7
- package/dist/types.d.ts +1 -2
- package/package.json +17 -16
|
@@ -1,24 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
-
var t = {};
|
|
4
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
-
t[p] = s[p];
|
|
6
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
-
t[p[i]] = s[p[i]];
|
|
10
|
-
}
|
|
11
|
-
return t;
|
|
12
|
-
};
|
|
13
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
4
|
};
|
|
16
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.createActorContext =
|
|
6
|
+
exports.createActorContext = createActorContext;
|
|
18
7
|
const react_1 = __importDefault(require("react"));
|
|
19
8
|
const useActor_1 = require("../../hooks/useActor");
|
|
20
9
|
const extractActorContext_1 = require("../../helpers/extractActorContext");
|
|
21
|
-
const
|
|
10
|
+
const useActorManager_1 = require("../../hooks/useActorManager");
|
|
22
11
|
/**
|
|
23
12
|
* Creates a React context specifically designed for managing the state and interactions with an actor on the Internet Computer (IC) blockchain.
|
|
24
13
|
* This context facilitates the dynamic creation and management of IC actors within React applications, leveraging the provided configuration options.
|
|
@@ -80,31 +69,34 @@ const agent_1 = require("../agent");
|
|
|
80
69
|
* it provides a type-safe and efficient way to manage actor state and interactions.
|
|
81
70
|
*/
|
|
82
71
|
function createActorContext(contextConfig = {}) {
|
|
83
|
-
const { canisterId: defaultCanisterId
|
|
72
|
+
const { canisterId: defaultCanisterId, ...defaultConfig } = contextConfig;
|
|
84
73
|
const ActorContext = react_1.default.createContext(null);
|
|
85
|
-
const ActorProvider = (
|
|
86
|
-
var { children, fetchingComponent, canisterId = defaultCanisterId, errorComponent = (error) => react_1.default.createElement("div", null, error), loadingComponent = react_1.default.createElement("div", null, "Loading canister..."), authenticatingComponent = react_1.default.createElement("div", null, "Authenticating...") } = _a, restConfig = __rest(_a, ["children", "fetchingComponent", "canisterId", "errorComponent", "loadingComponent", "authenticatingComponent"]);
|
|
74
|
+
const ActorProvider = ({ children, fetchingComponent, canisterId = defaultCanisterId, errorComponent = (error) => react_1.default.createElement("div", null, error), loadingComponent = react_1.default.createElement("div", null, "Loading canister..."), authenticatingComponent = react_1.default.createElement("div", null, "Authenticating..."), ...restConfig }) => {
|
|
87
75
|
if (!canisterId) {
|
|
88
76
|
throw new Error("canisterId is required");
|
|
89
77
|
}
|
|
90
|
-
const config = react_1.default.useMemo(() => (
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
78
|
+
const config = react_1.default.useMemo(() => ({
|
|
79
|
+
...defaultConfig,
|
|
80
|
+
...restConfig,
|
|
81
|
+
}), [defaultConfig, restConfig]);
|
|
82
|
+
const { fetchError, isAuthenticating: authenticating, initializeActor, hooks, } = (0, useActor_1.useActor)({
|
|
83
|
+
canisterId,
|
|
84
|
+
...config,
|
|
85
|
+
});
|
|
94
86
|
const useInitializeActor = react_1.default.useCallback(() => {
|
|
95
87
|
return initializeActor;
|
|
96
88
|
}, [initializeActor]);
|
|
97
89
|
const ActorChildren = ({ useActorState }) => {
|
|
98
|
-
const {
|
|
99
|
-
return
|
|
90
|
+
const { isInitializing, isInitialized, error } = useActorState();
|
|
91
|
+
return isInitializing
|
|
100
92
|
? loadingComponent
|
|
101
|
-
:
|
|
93
|
+
: isInitialized
|
|
102
94
|
? children
|
|
103
95
|
: error
|
|
104
96
|
? errorComponent(error.message)
|
|
105
97
|
: null;
|
|
106
98
|
};
|
|
107
|
-
return (react_1.default.createElement(ActorContext.Provider, { value:
|
|
99
|
+
return (react_1.default.createElement(ActorContext.Provider, { value: { ...hooks, useInitializeActor } },
|
|
108
100
|
fetchingComponent,
|
|
109
101
|
hooks === null ? (fetchError ? (errorComponent(fetchError)) : authenticating ? (authenticatingComponent) : (loadingComponent)) : (react_1.default.createElement(ActorChildren, { useActorState: hooks.useActorState }, children))));
|
|
110
102
|
};
|
|
@@ -112,7 +104,16 @@ function createActorContext(contextConfig = {}) {
|
|
|
112
104
|
const ActorHookProvider = ({ hooks, children, }) => {
|
|
113
105
|
return (react_1.default.createElement(ActorContext.Provider, { value: hooks }, children));
|
|
114
106
|
};
|
|
115
|
-
|
|
116
|
-
|
|
107
|
+
const ActorManagerProvider = ({ actorManager, children, }) => {
|
|
108
|
+
const { hooks } = (0, useActorManager_1.useActorManager)({
|
|
109
|
+
actorManager,
|
|
110
|
+
});
|
|
111
|
+
return (react_1.default.createElement(ActorContext.Provider, { value: hooks }, children));
|
|
112
|
+
};
|
|
113
|
+
return {
|
|
114
|
+
ActorProvider,
|
|
115
|
+
ActorHookProvider,
|
|
116
|
+
ActorManagerProvider,
|
|
117
|
+
...(0, extractActorContext_1.extractActorContext)(ActorContext),
|
|
118
|
+
};
|
|
117
119
|
}
|
|
118
|
-
exports.createActorContext = createActorContext;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useActorInterface =
|
|
3
|
+
exports.useActorInterface = useActorInterface;
|
|
4
4
|
const __1 = require("..");
|
|
5
5
|
/**
|
|
6
6
|
*
|
|
@@ -8,4 +8,3 @@ const __1 = require("..");
|
|
|
8
8
|
function useActorInterface() {
|
|
9
9
|
return __1.ActorHooks.useActorInterface();
|
|
10
10
|
}
|
|
11
|
-
exports.useActorInterface = useActorInterface;
|
|
@@ -7,15 +7,15 @@ import type { BaseActor, FunctionName, UseMethodParameters, UseMethodReturnType
|
|
|
7
7
|
* @example
|
|
8
8
|
* ```tsx
|
|
9
9
|
* function MethodCallComponent() {
|
|
10
|
-
* const { call, data,
|
|
10
|
+
* const { call, data, isLoading } = useMethod({
|
|
11
11
|
* functionName: 'updateUserProfile',
|
|
12
12
|
* args: ['123', { name: 'John Doe' }],
|
|
13
|
-
* onLoading: (
|
|
13
|
+
* onLoading: (isLoading) => console.log('Loading:', isLoading),
|
|
14
14
|
* onError: (error) => console.error('Error:', error),
|
|
15
15
|
* onSuccess: (data) => console.log('Success:', data),
|
|
16
16
|
* });
|
|
17
17
|
*
|
|
18
|
-
* if (
|
|
18
|
+
* if (isLoading) return <p>Updating profile...</p>;
|
|
19
19
|
*
|
|
20
20
|
* return (
|
|
21
21
|
* <div>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useMethod =
|
|
3
|
+
exports.useMethod = useMethod;
|
|
4
4
|
const __1 = require("..");
|
|
5
5
|
/**
|
|
6
6
|
* Hook for making dynamically update or query calls to actors, handling loading states, and managing errors. It supports custom event handlers for loading, success, and error events.
|
|
@@ -10,15 +10,15 @@ const __1 = require("..");
|
|
|
10
10
|
* @example
|
|
11
11
|
* ```tsx
|
|
12
12
|
* function MethodCallComponent() {
|
|
13
|
-
* const { call, data,
|
|
13
|
+
* const { call, data, isLoading } = useMethod({
|
|
14
14
|
* functionName: 'updateUserProfile',
|
|
15
15
|
* args: ['123', { name: 'John Doe' }],
|
|
16
|
-
* onLoading: (
|
|
16
|
+
* onLoading: (isLoading) => console.log('Loading:', isLoading),
|
|
17
17
|
* onError: (error) => console.error('Error:', error),
|
|
18
18
|
* onSuccess: (data) => console.log('Success:', data),
|
|
19
19
|
* });
|
|
20
20
|
*
|
|
21
|
-
* if (
|
|
21
|
+
* if (isLoading) return <p>Updating profile...</p>;
|
|
22
22
|
*
|
|
23
23
|
* return (
|
|
24
24
|
* <div>
|
|
@@ -32,4 +32,3 @@ const __1 = require("..");
|
|
|
32
32
|
function useMethod(args) {
|
|
33
33
|
return __1.ActorHooks.useMethod(args);
|
|
34
34
|
}
|
|
35
|
-
exports.useMethod = useMethod;
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import type { BaseActor, FunctionName, UseQueryCallParameters,
|
|
1
|
+
import type { BaseActor, FunctionName, UseQueryCallParameters, UseQueryCallReturnType } from "../../../types";
|
|
2
2
|
/**
|
|
3
3
|
* Hook for making query calls to actors. It supports automatic refetching on component mount and at specified intervals.
|
|
4
4
|
*
|
|
5
5
|
* @param args {@link UseQueryCallParameters}.
|
|
6
|
-
* @returns object {@link
|
|
6
|
+
* @returns object {@link UseQueryCallReturnType}.
|
|
7
7
|
* @example
|
|
8
8
|
* ```tsx
|
|
9
9
|
* function QueryCallComponent() {
|
|
10
|
-
* const { call, data,
|
|
10
|
+
* const { call, data, isLoading } = useQueryCall({
|
|
11
11
|
* functionName: 'getUserProfile',
|
|
12
12
|
* args: ['123'],
|
|
13
13
|
* refetchOnMount: true,
|
|
14
14
|
* refetchInterval: 5000, // refetch every 5 seconds
|
|
15
15
|
* });
|
|
16
16
|
*
|
|
17
|
-
* if (
|
|
17
|
+
* if (isLoading) return <p>isLoading profile...</p>;
|
|
18
18
|
*
|
|
19
19
|
* return (
|
|
20
20
|
* <div>
|
|
@@ -25,4 +25,4 @@ import type { BaseActor, FunctionName, UseQueryCallParameters, UseSharedCallRetu
|
|
|
25
25
|
* }
|
|
26
26
|
* ```
|
|
27
27
|
*/
|
|
28
|
-
export declare function useQueryCall<A = BaseActor, M extends FunctionName<A> = FunctionName<A>>(args: UseQueryCallParameters<A, M>):
|
|
28
|
+
export declare function useQueryCall<A = BaseActor, M extends FunctionName<A> = FunctionName<A>>(args: UseQueryCallParameters<A, M>): UseQueryCallReturnType<A, M>;
|
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useQueryCall =
|
|
3
|
+
exports.useQueryCall = useQueryCall;
|
|
4
4
|
const __1 = require("..");
|
|
5
5
|
/**
|
|
6
6
|
* Hook for making query calls to actors. It supports automatic refetching on component mount and at specified intervals.
|
|
7
7
|
*
|
|
8
8
|
* @param args {@link UseQueryCallParameters}.
|
|
9
|
-
* @returns object {@link
|
|
9
|
+
* @returns object {@link UseQueryCallReturnType}.
|
|
10
10
|
* @example
|
|
11
11
|
* ```tsx
|
|
12
12
|
* function QueryCallComponent() {
|
|
13
|
-
* const { call, data,
|
|
13
|
+
* const { call, data, isLoading } = useQueryCall({
|
|
14
14
|
* functionName: 'getUserProfile',
|
|
15
15
|
* args: ['123'],
|
|
16
16
|
* refetchOnMount: true,
|
|
17
17
|
* refetchInterval: 5000, // refetch every 5 seconds
|
|
18
18
|
* });
|
|
19
19
|
*
|
|
20
|
-
* if (
|
|
20
|
+
* if (isLoading) return <p>isLoading profile...</p>;
|
|
21
21
|
*
|
|
22
22
|
* return (
|
|
23
23
|
* <div>
|
|
@@ -31,4 +31,3 @@ const __1 = require("..");
|
|
|
31
31
|
function useQueryCall(args) {
|
|
32
32
|
return __1.ActorHooks.useQueryCall(args);
|
|
33
33
|
}
|
|
34
|
-
exports.useQueryCall = useQueryCall;
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import type { BaseActor, FunctionName, UseUpdateCallParameters,
|
|
1
|
+
import type { BaseActor, FunctionName, UseUpdateCallParameters, UseUpdateCallReturnType } from "../../../types";
|
|
2
2
|
/**
|
|
3
3
|
* Hook for making update calls to actors, handling loading states, and managing errors. It supports custom event handlers for loading, success, and error events.
|
|
4
4
|
*
|
|
5
5
|
* @param args {@link UseUpdateCallParameters}.
|
|
6
|
-
* @returns object {@link
|
|
6
|
+
* @returns object {@link UseUpdateCallReturnType}.
|
|
7
7
|
* @example
|
|
8
8
|
* ```tsx
|
|
9
9
|
* function UpdateCallComponent() {
|
|
10
|
-
* const { call, data,
|
|
10
|
+
* const { call, data, isLoading } = useUpdateCall({
|
|
11
11
|
* functionName: 'updateUserProfile',
|
|
12
12
|
* args: ['123', { name: 'John Doe' }],
|
|
13
|
-
* onLoading: (
|
|
13
|
+
* onLoading: (isLoading) => console.log('Loading:', isLoading),
|
|
14
14
|
* onError: (error) => console.error('Error:', error),
|
|
15
15
|
* onSuccess: (data) => console.log('Success:', data),
|
|
16
16
|
* });
|
|
17
17
|
*
|
|
18
|
-
* if (
|
|
18
|
+
* if (isLoading) return <p>Updating profile...</p>;
|
|
19
19
|
*
|
|
20
20
|
* return (
|
|
21
21
|
* <div>
|
|
@@ -26,4 +26,4 @@ import type { BaseActor, FunctionName, UseUpdateCallParameters, UseSharedCallRet
|
|
|
26
26
|
* }
|
|
27
27
|
* ```
|
|
28
28
|
*/
|
|
29
|
-
export declare function useUpdateCall<A = BaseActor, M extends FunctionName<A> = FunctionName<A>>(args: UseUpdateCallParameters<A, M>):
|
|
29
|
+
export declare function useUpdateCall<A = BaseActor, M extends FunctionName<A> = FunctionName<A>>(args: UseUpdateCallParameters<A, M>): UseUpdateCallReturnType<A, M>;
|
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useUpdateCall =
|
|
3
|
+
exports.useUpdateCall = useUpdateCall;
|
|
4
4
|
const __1 = require("..");
|
|
5
5
|
/**
|
|
6
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
7
|
*
|
|
8
8
|
* @param args {@link UseUpdateCallParameters}.
|
|
9
|
-
* @returns object {@link
|
|
9
|
+
* @returns object {@link UseUpdateCallReturnType}.
|
|
10
10
|
* @example
|
|
11
11
|
* ```tsx
|
|
12
12
|
* function UpdateCallComponent() {
|
|
13
|
-
* const { call, data,
|
|
13
|
+
* const { call, data, isLoading } = useUpdateCall({
|
|
14
14
|
* functionName: 'updateUserProfile',
|
|
15
15
|
* args: ['123', { name: 'John Doe' }],
|
|
16
|
-
* onLoading: (
|
|
16
|
+
* onLoading: (isLoading) => console.log('Loading:', isLoading),
|
|
17
17
|
* onError: (error) => console.error('Error:', error),
|
|
18
18
|
* onSuccess: (data) => console.log('Success:', data),
|
|
19
19
|
* });
|
|
20
20
|
*
|
|
21
|
-
* if (
|
|
21
|
+
* if (isLoading) return <p>Updating profile...</p>;
|
|
22
22
|
*
|
|
23
23
|
* return (
|
|
24
24
|
* <div>
|
|
@@ -32,4 +32,3 @@ const __1 = require("..");
|
|
|
32
32
|
function useUpdateCall(args) {
|
|
33
33
|
return __1.ActorHooks.useUpdateCall(args);
|
|
34
34
|
}
|
|
35
|
-
exports.useUpdateCall = useUpdateCall;
|
|
@@ -5,4 +5,4 @@ import type { FunctionName, BaseActor } from "../../../types";
|
|
|
5
5
|
* @param functionName The name of the actor method to visit.
|
|
6
6
|
* @returns The visit service function for the specified method.
|
|
7
7
|
*/
|
|
8
|
-
export declare function useVisitMethod<A = BaseActor>(functionName: FunctionName<A>): <V extends import("@dfinity/candid/lib/
|
|
8
|
+
export declare function useVisitMethod<A = BaseActor>(functionName: FunctionName<A>): <V extends import("@dfinity/candid/lib/esm/idl").Visitor<unknown, unknown>>(extractorClass: V, data: import("@ic-reactor/core/dist/classes/actor/types").VisitorType<V>["data"]) => ReturnType<V["visitFunc"]>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useVisitMethod =
|
|
3
|
+
exports.useVisitMethod = useVisitMethod;
|
|
4
4
|
const __1 = require("..");
|
|
5
5
|
/**
|
|
6
6
|
* Memoizes and returns a visit service function for a specific actor method.
|
|
@@ -11,4 +11,3 @@ const __1 = require("..");
|
|
|
11
11
|
function useVisitMethod(functionName) {
|
|
12
12
|
return __1.ActorHooks.useVisitMethod(functionName);
|
|
13
13
|
}
|
|
14
|
-
exports.useVisitMethod = useVisitMethod;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useVisitService =
|
|
3
|
+
exports.useVisitService = useVisitService;
|
|
4
4
|
const __1 = require("..");
|
|
5
5
|
/**
|
|
6
6
|
* Memoizes and returns a visit service function for a specific actor method.
|
|
@@ -11,4 +11,3 @@ const __1 = require("..");
|
|
|
11
11
|
function useVisitService() {
|
|
12
12
|
return __1.ActorHooks.useVisitService();
|
|
13
13
|
}
|
|
14
|
-
exports.useVisitService = useVisitService;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
1
|
/**
|
|
3
2
|
* `ActorProvider` is a React functional component that serves as a context provider for IC actor interactions within a React application.
|
|
4
3
|
* It wraps child components, providing them access to actor-specific hooks and functionalities based on the provided canister ID and configuration.
|
|
@@ -48,3 +47,25 @@ export declare const ActorProvider: import("react").FC<import("./types").ActorPr
|
|
|
48
47
|
* This setup ensures that `YourComponent` and any of its children can interact with the specified IC actor hooks through the context provided by `ActorHookProvider`.
|
|
49
48
|
*/
|
|
50
49
|
export declare const ActorHookProvider: import("react").FC<import("./types").ActorHookProviderProps<import("@ic-reactor/core/dist/types").BaseActor>>;
|
|
50
|
+
/**
|
|
51
|
+
* `ActorManagerProvider` is a React functional component that serves as a context provider for IC actor managers within a React application.
|
|
52
|
+
* It wraps child components, providing them access to actor-specific manager functionalities based on the provided actor manager and configuration.
|
|
53
|
+
*
|
|
54
|
+
* Props:
|
|
55
|
+
* - `actorManager`: ActorManager - The actor manager object containing the actor manager functionalities.
|
|
56
|
+
* - `children`: React Node - The child components that will have access to the actor manager context.
|
|
57
|
+
*
|
|
58
|
+
* Behavior:
|
|
59
|
+
* - Validates the presence of the `actorManager`. Throws an error if it is missing, ensuring that the actor manager is always available for actor operations.
|
|
60
|
+
* - Utilizes `useMemo` to memoize the `actorManager`, optimizing for performance by avoiding unnecessary recalculations.
|
|
61
|
+
* - Renders the child components once the `actorManager` is available, effectively providing them access to the actor manager context.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```jsx
|
|
65
|
+
* <ActorManagerProvider actorManager={yourActorManager}>
|
|
66
|
+
* <YourComponent />
|
|
67
|
+
* </ActorManagerProvider>
|
|
68
|
+
* ```
|
|
69
|
+
* This setup ensures that `YourComponent` and any of its children can interact with the specified IC actor manager through the context provided by `ActorManagerProvider`.
|
|
70
|
+
*/
|
|
71
|
+
export declare const ActorManagerProvider: import("react").FC<import("./types").ActorManagerProviderProps<import("@ic-reactor/core/dist/types").BaseActor>>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ActorHookProvider = exports.ActorProvider = void 0;
|
|
3
|
+
exports.ActorManagerProvider = exports.ActorHookProvider = exports.ActorProvider = void 0;
|
|
4
4
|
const _1 = require(".");
|
|
5
5
|
/**
|
|
6
6
|
* `ActorProvider` is a React functional component that serves as a context provider for IC actor interactions within a React application.
|
|
@@ -51,3 +51,25 @@ exports.ActorProvider = _1.ActorHooks.ActorProvider;
|
|
|
51
51
|
* This setup ensures that `YourComponent` and any of its children can interact with the specified IC actor hooks through the context provided by `ActorHookProvider`.
|
|
52
52
|
*/
|
|
53
53
|
exports.ActorHookProvider = _1.ActorHooks.ActorHookProvider;
|
|
54
|
+
/**
|
|
55
|
+
* `ActorManagerProvider` is a React functional component that serves as a context provider for IC actor managers within a React application.
|
|
56
|
+
* It wraps child components, providing them access to actor-specific manager functionalities based on the provided actor manager and configuration.
|
|
57
|
+
*
|
|
58
|
+
* Props:
|
|
59
|
+
* - `actorManager`: ActorManager - The actor manager object containing the actor manager functionalities.
|
|
60
|
+
* - `children`: React Node - The child components that will have access to the actor manager context.
|
|
61
|
+
*
|
|
62
|
+
* Behavior:
|
|
63
|
+
* - Validates the presence of the `actorManager`. Throws an error if it is missing, ensuring that the actor manager is always available for actor operations.
|
|
64
|
+
* - Utilizes `useMemo` to memoize the `actorManager`, optimizing for performance by avoiding unnecessary recalculations.
|
|
65
|
+
* - Renders the child components once the `actorManager` is available, effectively providing them access to the actor manager context.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```jsx
|
|
69
|
+
* <ActorManagerProvider actorManager={yourActorManager}>
|
|
70
|
+
* <YourComponent />
|
|
71
|
+
* </ActorManagerProvider>
|
|
72
|
+
* ```
|
|
73
|
+
* This setup ensures that `YourComponent` and any of its children can interact with the specified IC actor manager through the context provided by `ActorManagerProvider`.
|
|
74
|
+
*/
|
|
75
|
+
exports.ActorManagerProvider = _1.ActorHooks.ActorManagerProvider;
|
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
import type { IDL, ActorHooksReturnType, BaseActor, ActorManagerParameters, CanisterId, InitializeActor } from "../../types";
|
|
1
|
+
import type { IDL, ActorHooksReturnType, BaseActor, ActorManagerParameters, CanisterId, InitializeActor, ActorManager } from "../../types";
|
|
3
2
|
export interface CreateActorContextType<A = BaseActor> extends ActorHooksReturnType<A> {
|
|
4
3
|
useInitializeActor?: () => InitializeActor;
|
|
5
4
|
}
|
|
6
5
|
export interface CreateActorContextReturnType<A = BaseActor> extends ActorHooksReturnType<A> {
|
|
7
6
|
ActorProvider: React.FC<ActorProviderProps>;
|
|
8
7
|
ActorHookProvider: React.FC<ActorHookProviderProps<A>>;
|
|
8
|
+
ActorManagerProvider: React.FC<ActorManagerProviderProps<A>>;
|
|
9
9
|
useInitializeActor: () => InitializeActor;
|
|
10
10
|
}
|
|
11
11
|
export interface ActorHookProviderProps<A> {
|
|
12
12
|
hooks: ActorHooksReturnType<A>;
|
|
13
13
|
children?: React.ReactNode;
|
|
14
14
|
}
|
|
15
|
+
export interface ActorManagerProviderProps<A> {
|
|
16
|
+
children?: React.ReactNode | undefined;
|
|
17
|
+
actorManager: ActorManager<A>;
|
|
18
|
+
}
|
|
15
19
|
export interface ActorProviderProps extends CreateActorContextParameters {
|
|
16
20
|
children?: React.ReactNode | undefined;
|
|
17
21
|
candidString?: string;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare function createAdapterContext(config?:
|
|
1
|
+
import type { CreateCandidAdapterContextParameters, CreateCandidAdapterContextReturnType } from "./types";
|
|
2
|
+
export declare function createAdapterContext(config?: CreateCandidAdapterContextParameters): CreateCandidAdapterContextReturnType;
|
|
@@ -1,34 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
-
var t = {};
|
|
13
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
-
t[p] = s[p];
|
|
15
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
-
t[p[i]] = s[p[i]];
|
|
19
|
-
}
|
|
20
|
-
return t;
|
|
21
|
-
};
|
|
22
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
4
|
};
|
|
25
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.createAdapterContext =
|
|
6
|
+
exports.createAdapterContext = createAdapterContext;
|
|
27
7
|
const react_1 = __importDefault(require("react"));
|
|
28
8
|
const agent_1 = require("../agent");
|
|
29
9
|
const core_1 = require("@ic-reactor/core");
|
|
30
10
|
function createAdapterContext(config = {}) {
|
|
31
|
-
const { withParser: _withParser, didjsCanisterId: _didjsCanisterId
|
|
11
|
+
const { withParser: _withParser, didjsCanisterId: _didjsCanisterId, ...defaultConfig } = config;
|
|
32
12
|
const CandidAdapterContext = react_1.default.createContext(null);
|
|
33
13
|
const useCandidAdapter = () => {
|
|
34
14
|
const context = react_1.default.useContext(CandidAdapterContext);
|
|
@@ -40,6 +20,7 @@ function createAdapterContext(config = {}) {
|
|
|
40
20
|
const useCandidEvaluation = () => {
|
|
41
21
|
const [state, setState] = react_1.default.useState({
|
|
42
22
|
fetching: true,
|
|
23
|
+
isFetching: true,
|
|
43
24
|
fetchError: null,
|
|
44
25
|
});
|
|
45
26
|
const candidAdapter = useCandidAdapter();
|
|
@@ -47,6 +28,7 @@ function createAdapterContext(config = {}) {
|
|
|
47
28
|
setState({
|
|
48
29
|
fetchError: null,
|
|
49
30
|
fetching: false,
|
|
31
|
+
isFetching: false,
|
|
50
32
|
});
|
|
51
33
|
try {
|
|
52
34
|
return candidAdapter.validateIDL(candidString);
|
|
@@ -55,22 +37,29 @@ function createAdapterContext(config = {}) {
|
|
|
55
37
|
setState({
|
|
56
38
|
fetchError: `Error validating Candid definition, ${err}`,
|
|
57
39
|
fetching: false,
|
|
40
|
+
isFetching: false,
|
|
58
41
|
});
|
|
42
|
+
return false;
|
|
59
43
|
}
|
|
60
44
|
}, []);
|
|
61
|
-
const
|
|
45
|
+
const isCandidValid = react_1.default.useCallback((candidString) => {
|
|
46
|
+
return validateCandid(candidString) !== false;
|
|
47
|
+
}, [validateCandid]);
|
|
48
|
+
const evaluateCandid = react_1.default.useCallback(async (candidString) => {
|
|
62
49
|
setState({
|
|
63
50
|
fetchError: null,
|
|
64
51
|
fetching: true,
|
|
52
|
+
isFetching: true,
|
|
65
53
|
});
|
|
66
54
|
try {
|
|
67
|
-
const definition =
|
|
68
|
-
if (typeof
|
|
55
|
+
const definition = await candidAdapter.evaluateCandidDefinition(candidString);
|
|
56
|
+
if (typeof definition?.idlFactory !== "function") {
|
|
69
57
|
throw new Error("No Function found in Candid definition!");
|
|
70
58
|
}
|
|
71
59
|
setState({
|
|
72
60
|
fetchError: null,
|
|
73
61
|
fetching: false,
|
|
62
|
+
isFetching: false,
|
|
74
63
|
});
|
|
75
64
|
return definition.idlFactory;
|
|
76
65
|
}
|
|
@@ -78,17 +67,21 @@ function createAdapterContext(config = {}) {
|
|
|
78
67
|
setState({
|
|
79
68
|
fetchError: `Error evaluating Candid definition, ${err}`,
|
|
80
69
|
fetching: false,
|
|
70
|
+
isFetching: false,
|
|
81
71
|
});
|
|
72
|
+
return undefined;
|
|
82
73
|
}
|
|
83
|
-
}
|
|
84
|
-
return
|
|
74
|
+
}, []);
|
|
75
|
+
return { evaluateCandid, isCandidValid, validateCandid, ...state };
|
|
85
76
|
};
|
|
86
|
-
const CandidAdapterProvider = (
|
|
87
|
-
|
|
88
|
-
|
|
77
|
+
const CandidAdapterProvider = ({ children, withParser = _withParser, loadingComponent = react_1.default.createElement("div", null, "Loading Parser..."), didjsCanisterId = _didjsCanisterId, ...restConfig }) => {
|
|
78
|
+
const config = react_1.default.useMemo(() => ({
|
|
79
|
+
...defaultConfig,
|
|
80
|
+
...restConfig,
|
|
81
|
+
}), [defaultConfig, restConfig]);
|
|
89
82
|
const [initalized, setInitialized] = react_1.default.useState(false);
|
|
90
83
|
const agentManager = (0, agent_1.useAgentManager)();
|
|
91
|
-
const candidAdapter = react_1.default.useMemo(() => (0, core_1.createCandidAdapter)(
|
|
84
|
+
const candidAdapter = react_1.default.useMemo(() => (0, core_1.createCandidAdapter)({ agentManager, didjsCanisterId, ...config }), [didjsCanisterId, agentManager]);
|
|
92
85
|
react_1.default.useEffect(() => {
|
|
93
86
|
if (withParser) {
|
|
94
87
|
candidAdapter.initializeParser().then(() => setInitialized(true));
|
|
@@ -107,4 +100,3 @@ function createAdapterContext(config = {}) {
|
|
|
107
100
|
useCandidAdapter,
|
|
108
101
|
};
|
|
109
102
|
}
|
|
110
|
-
exports.createAdapterContext = createAdapterContext;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
1
|
/** @ignore */
|
|
3
2
|
export declare const AdapterHooks: import("./types").CreateCandidAdapterContextReturnType;
|
|
4
|
-
export declare const CandidAdapterContext: import("react").Context<import("
|
|
3
|
+
export declare const CandidAdapterContext: import("react").Context<import("@ic-reactor/core/dist/classes").CandidAdapter | null>;
|
|
5
4
|
export * from "./provider";
|
|
6
5
|
export * from "./hooks/useCandidAdapter";
|
|
7
6
|
export * from "./hooks/useCandidEvaluation";
|
|
@@ -1,20 +1,47 @@
|
|
|
1
1
|
import type { PropsWithChildren } from "react";
|
|
2
|
-
import type {
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
export
|
|
6
|
-
}
|
|
2
|
+
import type { IDL } from "@dfinity/candid";
|
|
3
|
+
import { CandidAdapter } from "@ic-reactor/core/dist/classes";
|
|
4
|
+
import { CandidAdapterParameters } from "../../types";
|
|
5
|
+
export type CandidAdapterContextType = CandidAdapter;
|
|
7
6
|
export interface CandidAdapterProviderProps extends PropsWithChildren, CandidAdapterParameters {
|
|
8
7
|
withParser?: boolean;
|
|
9
8
|
loadingComponent?: React.ReactNode;
|
|
10
9
|
}
|
|
11
|
-
export interface
|
|
10
|
+
export interface CreateCandidAdapterContextParameters extends CandidAdapterParameters {
|
|
12
11
|
withParser?: boolean;
|
|
13
12
|
}
|
|
14
13
|
export interface UseCandidEvaluationReturnType {
|
|
14
|
+
/**
|
|
15
|
+
* The error message encountered during the fetch operation, or `null` if no error occurred.
|
|
16
|
+
*/
|
|
15
17
|
fetchError: string | null;
|
|
18
|
+
/**
|
|
19
|
+
* @deprecated Use `isFetching` instead.
|
|
20
|
+
* Indicates whether a fetch operation is currently in progress.
|
|
21
|
+
*/
|
|
16
22
|
fetching: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Indicates whether a fetch operation is currently in progress.
|
|
25
|
+
*/
|
|
26
|
+
isFetching: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* @deprecated Use `isCandidValid` instead.
|
|
29
|
+
* Validates the provided Candid interface definition string.
|
|
30
|
+
* @param candidString - The Candid definition to validate.
|
|
31
|
+
* @returns `true` if the string is valid, `false` otherwise.
|
|
32
|
+
*/
|
|
17
33
|
validateCandid: (candidString: string) => boolean | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Validates the provided Candid interface definition string.
|
|
36
|
+
* @param candidString - The Candid definition to validate.
|
|
37
|
+
* @returns `true` if valid, `false` otherwise.
|
|
38
|
+
*/
|
|
39
|
+
isCandidValid: (candidString: string) => boolean | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Evaluates the provided Candid interface definition string.
|
|
42
|
+
* @param candidString - The Candid definition to evaluate.
|
|
43
|
+
* @returns A promise resolving to the corresponding InterfaceFactory if the evaluation succeeds, otherwise `undefined`.
|
|
44
|
+
*/
|
|
18
45
|
evaluateCandid: (candidString: string) => Promise<IDL.InterfaceFactory | undefined>;
|
|
19
46
|
}
|
|
20
47
|
export interface CreateCandidAdapterContextReturnType {
|