@ic-reactor/react 1.6.0-beta.0 → 1.6.0-beta.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.
|
@@ -82,13 +82,12 @@ function createActorContext(contextConfig = {}) {
|
|
|
82
82
|
const { canisterId: defaultCanisterId } = contextConfig, defaultConfig = __rest(contextConfig, ["canisterId"]);
|
|
83
83
|
const ActorContext = react_1.default.createContext(null);
|
|
84
84
|
const ActorProvider = (_a) => {
|
|
85
|
-
var { children, canisterId = defaultCanisterId, loadingComponent = react_1.default.createElement("div", null, "Fetching canister..."), authenticatingComponent = react_1.default.createElement("div", null, "Authenticating...")
|
|
85
|
+
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"]);
|
|
86
86
|
if (!canisterId) {
|
|
87
87
|
throw new Error("canisterId is required");
|
|
88
88
|
}
|
|
89
89
|
const config = react_1.default.useMemo(() => (Object.assign(Object.assign({}, defaultConfig), restConfig)), [defaultConfig, restConfig]);
|
|
90
|
-
const { fetchError, authenticating, hooks } = (0, useActor_1.useActor)(Object.assign({ canisterId,
|
|
91
|
-
candidString }, config));
|
|
90
|
+
const { fetchError, authenticating, hooks } = (0, useActor_1.useActor)(Object.assign({ canisterId }, config));
|
|
92
91
|
return (react_1.default.createElement(ActorContext.Provider, { value: hooks }, hooks === null
|
|
93
92
|
? fetchError
|
|
94
93
|
? fetchError
|
|
@@ -5,6 +5,6 @@ export type { CandidAdapter };
|
|
|
5
5
|
export interface CandidAdapterContextType extends CandidAdapter {
|
|
6
6
|
}
|
|
7
7
|
export interface CandidAdapterProviderProps extends PropsWithChildren, CandidAdapterParameters {
|
|
8
|
-
|
|
8
|
+
withParser?: boolean;
|
|
9
9
|
loadingComponent?: React.ReactNode;
|
|
10
10
|
}
|
package/dist/hooks/useActor.js
CHANGED
|
@@ -108,7 +108,7 @@ const useActor = (config) => {
|
|
|
108
108
|
const candidAdapter = (0, useCandidAdapter_1.useCandidAdapter)();
|
|
109
109
|
const authenticating = (0, agent_1.useAuthState)().authenticating;
|
|
110
110
|
const fetchCandid = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
111
|
-
if (fetching
|
|
111
|
+
if (fetching)
|
|
112
112
|
return;
|
|
113
113
|
setState({
|
|
114
114
|
fetching: true,
|
|
@@ -130,20 +130,8 @@ const useActor = (config) => {
|
|
|
130
130
|
fetching: false,
|
|
131
131
|
});
|
|
132
132
|
}
|
|
133
|
-
}), [canisterId
|
|
134
|
-
const
|
|
135
|
-
const initialActorManager = (0, react_1.useCallback)((idlFactory) => {
|
|
136
|
-
if (authenticating || !idlFactory)
|
|
137
|
-
return;
|
|
138
|
-
const actorManager = (0, core_1.createActorManager)(Object.assign({ agentManager,
|
|
139
|
-
idlFactory,
|
|
140
|
-
canisterId }, actorConfig));
|
|
141
|
-
setActorManager(actorManager);
|
|
142
|
-
}, [canisterId, agentManager, authenticating]);
|
|
143
|
-
const evaluateCandid = (0, react_1.useCallback)((candidString) => __awaiter(void 0, void 0, void 0, function* () {
|
|
144
|
-
if (!candidAdapter) {
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
133
|
+
}), [canisterId]);
|
|
134
|
+
const evaluateCandid = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
147
135
|
try {
|
|
148
136
|
const definition = yield candidAdapter.dynamicEvalJs(candidString);
|
|
149
137
|
if (typeof (definition === null || definition === void 0 ? void 0 : definition.idlFactory) !== "function") {
|
|
@@ -159,30 +147,34 @@ const useActor = (config) => {
|
|
|
159
147
|
fetching: false,
|
|
160
148
|
});
|
|
161
149
|
}
|
|
162
|
-
}), [
|
|
150
|
+
}), [candidString]);
|
|
151
|
+
const agentManager = (0, useAgentManager_1.useAgentManager)();
|
|
152
|
+
const initialActorManager = (0, react_1.useCallback)((idlFactory) => {
|
|
153
|
+
if (authenticating || !idlFactory)
|
|
154
|
+
return;
|
|
155
|
+
const actorManager = (0, core_1.createActorManager)(Object.assign({ agentManager,
|
|
156
|
+
idlFactory,
|
|
157
|
+
canisterId }, actorConfig));
|
|
158
|
+
setActorManager(actorManager);
|
|
159
|
+
}, [canisterId, agentManager, authenticating]);
|
|
163
160
|
const handleActorInitialization = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
161
|
+
if (authenticating)
|
|
162
|
+
return;
|
|
164
163
|
if (maybeIdlFactory) {
|
|
165
|
-
|
|
164
|
+
initialActorManager(maybeIdlFactory);
|
|
165
|
+
return;
|
|
166
166
|
}
|
|
167
167
|
if (!candidAdapter) {
|
|
168
|
-
throw new Error("CandidAdapter is
|
|
168
|
+
throw new Error("CandidAdapter is necessary to fetch the Candid interface. Please ensure your application is wrapped with the CandidAdapterProvider!");
|
|
169
169
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return initialActorManager(idlFactory);
|
|
174
|
-
}
|
|
175
|
-
const idlFactory = yield fetchCandid();
|
|
176
|
-
return initialActorManager(idlFactory);
|
|
170
|
+
let idlFactory;
|
|
171
|
+
if (candidString) {
|
|
172
|
+
idlFactory = yield evaluateCandid();
|
|
177
173
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
console.error(error);
|
|
181
|
-
setState({
|
|
182
|
-
fetchError: "Error occurred during actor initialization",
|
|
183
|
-
fetching: false,
|
|
184
|
-
});
|
|
174
|
+
else {
|
|
175
|
+
idlFactory = yield fetchCandid();
|
|
185
176
|
}
|
|
177
|
+
initialActorManager(idlFactory);
|
|
186
178
|
}), [fetchCandid, evaluateCandid, maybeIdlFactory, initialActorManager]);
|
|
187
179
|
(0, react_1.useEffect)(() => {
|
|
188
180
|
handleActorInitialization();
|
|
@@ -1,31 +1,26 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import { CandidAdapterProviderProps } from "../context/types";
|
|
3
3
|
/**
|
|
4
|
-
* `CandidAdapterProvider` is a React
|
|
5
|
-
* It
|
|
6
|
-
*
|
|
7
|
-
* The provider encapsulates the logic for initializing and managing an agent manager instance, which is then used to
|
|
8
|
-
* create various hooks related to agent operations and authentication processes. These hooks are made available to all
|
|
9
|
-
* child components through the context, facilitating a centralized and efficient way to interact with the Internet Computer (IC) blockchain.
|
|
10
|
-
*
|
|
11
|
-
* @param children - Child components that can consume the context.
|
|
12
|
-
* @param agentManager - An optional `AgentManager` instance to be used by the provider. If not provided, a new instance
|
|
13
|
-
* will be created based on the provided options combined with default configuration.
|
|
14
|
-
* @param options - Configuration options for the `AgentManager`. These options are merged with any default configurations
|
|
15
|
-
* specified during the context creation and can include custom settings for the agent, such as identity,
|
|
16
|
-
* host URL, etc.
|
|
4
|
+
* `CandidAdapterProvider` is a React component that provides the CandidAdapter to its children.
|
|
5
|
+
* It initializes the CandidAdapter with the provided options and makes it available to all children.
|
|
17
6
|
*
|
|
18
7
|
* @example
|
|
19
|
-
*
|
|
8
|
+
* ```tsx
|
|
9
|
+
* import { AgentProvider, CandidAdapterProvider, ActorProvider } from "@ic-reactor/react"
|
|
20
10
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
11
|
+
* const App = () => (
|
|
12
|
+
* <AgentProvider>
|
|
13
|
+
* <CandidAdapterProvider>
|
|
14
|
+
* Your Actors here, it will able to fetch the Candid interface
|
|
15
|
+
* you dont need to pass the idlFactory to the Actor component
|
|
16
|
+
* e.g.
|
|
17
|
+
* <ActorProvider canisterId="ryjl3-tyaaa-aaaaa-aaaba-cai" />
|
|
18
|
+
* </CandidAdapterProvider>
|
|
19
|
+
* </AgentProvider>
|
|
20
|
+
* )
|
|
26
21
|
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
22
|
+
* export default App
|
|
23
|
+
* ```
|
|
29
24
|
*/
|
|
30
25
|
declare const CandidAdapterProvider: React.FC<CandidAdapterProviderProps>;
|
|
31
26
|
export { CandidAdapterProvider };
|
package/dist/provider/adapter.js
CHANGED
|
@@ -20,41 +20,39 @@ const core_1 = require("@ic-reactor/core");
|
|
|
20
20
|
const adapter_1 = require("../context/adapter");
|
|
21
21
|
const hooks_1 = require("../hooks");
|
|
22
22
|
/**
|
|
23
|
-
* `CandidAdapterProvider` is a React
|
|
24
|
-
* It
|
|
25
|
-
*
|
|
26
|
-
* The provider encapsulates the logic for initializing and managing an agent manager instance, which is then used to
|
|
27
|
-
* create various hooks related to agent operations and authentication processes. These hooks are made available to all
|
|
28
|
-
* child components through the context, facilitating a centralized and efficient way to interact with the Internet Computer (IC) blockchain.
|
|
29
|
-
*
|
|
30
|
-
* @param children - Child components that can consume the context.
|
|
31
|
-
* @param agentManager - An optional `AgentManager` instance to be used by the provider. If not provided, a new instance
|
|
32
|
-
* will be created based on the provided options combined with default configuration.
|
|
33
|
-
* @param options - Configuration options for the `AgentManager`. These options are merged with any default configurations
|
|
34
|
-
* specified during the context creation and can include custom settings for the agent, such as identity,
|
|
35
|
-
* host URL, etc.
|
|
23
|
+
* `CandidAdapterProvider` is a React component that provides the CandidAdapter to its children.
|
|
24
|
+
* It initializes the CandidAdapter with the provided options and makes it available to all children.
|
|
36
25
|
*
|
|
37
26
|
* @example
|
|
38
|
-
*
|
|
27
|
+
* ```tsx
|
|
28
|
+
* import { AgentProvider, CandidAdapterProvider, ActorProvider } from "@ic-reactor/react"
|
|
39
29
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
30
|
+
* const App = () => (
|
|
31
|
+
* <AgentProvider>
|
|
32
|
+
* <CandidAdapterProvider>
|
|
33
|
+
* Your Actors here, it will able to fetch the Candid interface
|
|
34
|
+
* you dont need to pass the idlFactory to the Actor component
|
|
35
|
+
* e.g.
|
|
36
|
+
* <ActorProvider canisterId="ryjl3-tyaaa-aaaaa-aaaba-cai" />
|
|
37
|
+
* </CandidAdapterProvider>
|
|
38
|
+
* </AgentProvider>
|
|
39
|
+
* )
|
|
45
40
|
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
41
|
+
* export default App
|
|
42
|
+
* ```
|
|
48
43
|
*/
|
|
49
44
|
const CandidAdapterProvider = (_a) => {
|
|
50
|
-
var { children,
|
|
45
|
+
var { children, withParser, loadingComponent = react_1.default.createElement("div", null, "Loading Parser..."), didjsCanisterId } = _a, options = __rest(_a, ["children", "withParser", "loadingComponent", "didjsCanisterId"]);
|
|
51
46
|
const [initalized, setInitialized] = react_1.default.useState(false);
|
|
52
47
|
const agentManager = (0, hooks_1.useAgentManager)();
|
|
53
|
-
const candidAdapter = react_1.default.useMemo(() => (0, core_1.createCandidAdapter)(Object.assign({ agentManager }, options)), [
|
|
48
|
+
const candidAdapter = react_1.default.useMemo(() => (0, core_1.createCandidAdapter)(Object.assign({ agentManager, didjsCanisterId }, options)), [didjsCanisterId, agentManager]);
|
|
54
49
|
react_1.default.useEffect(() => {
|
|
55
|
-
if (
|
|
50
|
+
if (withParser) {
|
|
56
51
|
candidAdapter.initializeParser().then(() => setInitialized(true));
|
|
57
52
|
}
|
|
53
|
+
else {
|
|
54
|
+
setInitialized(true);
|
|
55
|
+
}
|
|
58
56
|
}, []);
|
|
59
57
|
return (react_1.default.createElement(adapter_1.CandidAdapterContext.Provider, { value: candidAdapter }, initalized ? children : loadingComponent));
|
|
60
58
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/react",
|
|
3
|
-
"version": "1.6.0-beta.
|
|
3
|
+
"version": "1.6.0-beta.2",
|
|
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.6.0-beta.
|
|
38
|
+
"@ic-reactor/core": "^1.6.0-beta.2",
|
|
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": "87e86273dfec57470aa0aca55fd476302b2a8d02"
|
|
51
51
|
}
|