@ic-reactor/core 1.0.6 → 1.0.8
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/README.md +1 -1
- package/dist/classes/actor/index.d.ts +1 -1
- package/dist/classes/actor/index.js +20 -18
- package/dist/classes/agent/index.js +6 -6
- package/dist/classes/agent/types.d.ts +1 -1
- package/dist/classes/candid/index.js +1 -5
- package/dist/createReactorStore.js +2 -2
- package/dist/types.d.ts +1 -1
- package/dist/utils/helper.js +4 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -238,7 +238,7 @@ For development purposes, you might want to connect to a local instance of the I
|
|
|
238
238
|
import { createAgentManager } from "@ic-reactor/core"
|
|
239
239
|
|
|
240
240
|
export const agentManager = createAgentManager({
|
|
241
|
-
|
|
241
|
+
withLocalEnv: true,
|
|
242
242
|
port: 8000, // Default port is 4943
|
|
243
243
|
})
|
|
244
244
|
```
|
|
@@ -17,7 +17,7 @@ export declare class ActorManager<A = BaseActor> {
|
|
|
17
17
|
}>) => void;
|
|
18
18
|
constructor(actorConfig: ActorManagerParameters);
|
|
19
19
|
initialize: (options?: UpdateAgentParameters) => Promise<void>;
|
|
20
|
-
extractService()
|
|
20
|
+
extractService: () => VisitService<A>;
|
|
21
21
|
private initializeActor;
|
|
22
22
|
callMethod: <M extends FunctionName<A>>(functionName: M, ...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
23
23
|
get agentManager(): AgentManager;
|
|
@@ -13,7 +13,6 @@ exports.ActorManager = void 0;
|
|
|
13
13
|
/* eslint-disable no-console */
|
|
14
14
|
const agent_1 = require("@dfinity/agent");
|
|
15
15
|
const helper_1 = require("../../utils/helper");
|
|
16
|
-
const candid_1 = require("@dfinity/candid");
|
|
17
16
|
class ActorManager {
|
|
18
17
|
constructor(actorConfig) {
|
|
19
18
|
this._actor = null;
|
|
@@ -37,6 +36,20 @@ class ActorManager {
|
|
|
37
36
|
this.initialize = (options) => __awaiter(this, void 0, void 0, function* () {
|
|
38
37
|
yield this._agentManager.updateAgent(options);
|
|
39
38
|
});
|
|
39
|
+
this.extractService = () => {
|
|
40
|
+
if (this._actor === null) {
|
|
41
|
+
throw new Error("For extracting service, actor must be initialized");
|
|
42
|
+
}
|
|
43
|
+
return agent_1.Actor.interfaceOf(this._actor)._fields.reduce((acc, service) => {
|
|
44
|
+
const functionName = service[0];
|
|
45
|
+
const type = service[1];
|
|
46
|
+
const visit = ((extractorClass, data) => {
|
|
47
|
+
return type.accept(extractorClass, data || functionName);
|
|
48
|
+
});
|
|
49
|
+
acc[functionName] = visit;
|
|
50
|
+
return acc;
|
|
51
|
+
}, {});
|
|
52
|
+
};
|
|
40
53
|
this.initializeActor = (agent) => {
|
|
41
54
|
console.info(`Initializing actor ${this.canisterId} on ${agent.isLocal() ? "local" : "ic"} network`);
|
|
42
55
|
const { _idlFactory: idlFactory, canisterId } = this;
|
|
@@ -95,29 +108,18 @@ class ActorManager {
|
|
|
95
108
|
this.canisterId = canisterId;
|
|
96
109
|
this._idlFactory = idlFactory;
|
|
97
110
|
this._agentManager = agentManager;
|
|
98
|
-
if (withVisitor) {
|
|
99
|
-
this.visitFunction = withVisitor ? this.extractService() : emptyVisitor;
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
this.visitFunction = emptyVisitor;
|
|
103
|
-
}
|
|
104
111
|
// Initialize stores
|
|
105
112
|
this.actorStore = (0, helper_1.createStoreWithOptionalDevtools)(this.initialState, {
|
|
106
113
|
withDevtools,
|
|
107
114
|
store: `actor-${String(canisterId)}`,
|
|
108
115
|
});
|
|
109
116
|
this._agentManager.subscribeAgent(this.initializeActor, initializeOnCreate);
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
return type.accept(extractorClass, data || functionName);
|
|
117
|
-
});
|
|
118
|
-
acc[functionName] = visit;
|
|
119
|
-
return acc;
|
|
120
|
-
}, {});
|
|
117
|
+
if (withVisitor) {
|
|
118
|
+
this.visitFunction = this.extractService();
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
this.visitFunction = emptyVisitor;
|
|
122
|
+
}
|
|
121
123
|
}
|
|
122
124
|
// agent store
|
|
123
125
|
get agentManager() {
|
|
@@ -90,9 +90,9 @@ class AgentManager {
|
|
|
90
90
|
this.unsubscribeAgent = (callback) => {
|
|
91
91
|
this._subscribers = this._subscribers.filter((sub) => sub !== callback);
|
|
92
92
|
};
|
|
93
|
-
this.notifySubscribers = () => {
|
|
94
|
-
this._subscribers.
|
|
95
|
-
};
|
|
93
|
+
this.notifySubscribers = () => __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
yield Promise.all(this._subscribers.map((callback) => __awaiter(this, void 0, void 0, function* () { return callback(this._agent); })));
|
|
95
|
+
});
|
|
96
96
|
this.updateAgent = (options) => __awaiter(this, void 0, void 0, function* () {
|
|
97
97
|
const { agent } = options || {};
|
|
98
98
|
if (agent) {
|
|
@@ -103,7 +103,7 @@ class AgentManager {
|
|
|
103
103
|
this.isLocalEnv = this._agent.isLocal();
|
|
104
104
|
yield this.initializeAgent();
|
|
105
105
|
}
|
|
106
|
-
this.notifySubscribers();
|
|
106
|
+
yield this.notifySubscribers();
|
|
107
107
|
});
|
|
108
108
|
this.authenticate = () => __awaiter(this, void 0, void 0, function* () {
|
|
109
109
|
this.updateAuthState({ authenticating: true });
|
|
@@ -157,8 +157,8 @@ class AgentManager {
|
|
|
157
157
|
const identity = this.authStore.getState().identity;
|
|
158
158
|
return identity ? identity.getPrincipal() : null;
|
|
159
159
|
};
|
|
160
|
-
const _a = options || {}, { withDevtools, port = 4943,
|
|
161
|
-
const host =
|
|
160
|
+
const _a = options || {}, { withDevtools, port = 4943, withLocalEnv, host: optionHost } = _a, agentParameters = __rest(_a, ["withDevtools", "port", "withLocalEnv", "host"]);
|
|
161
|
+
const host = withLocalEnv
|
|
162
162
|
? `http://127.0.0.1:${port}`
|
|
163
163
|
: optionHost
|
|
164
164
|
? optionHost.includes("localhost")
|
|
@@ -4,7 +4,7 @@ import type { StoreApi } from "zustand";
|
|
|
4
4
|
export { HttpAgentOptions, AuthClient, Identity };
|
|
5
5
|
export interface AgentManagerParameters extends HttpAgentOptions {
|
|
6
6
|
port?: number;
|
|
7
|
-
|
|
7
|
+
withLocalEnv?: boolean;
|
|
8
8
|
withDevtools?: boolean;
|
|
9
9
|
}
|
|
10
10
|
export interface AgentState {
|
|
@@ -11,7 +11,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.CandidAdapter = void 0;
|
|
13
13
|
const agent_1 = require("@dfinity/agent");
|
|
14
|
-
const principal_1 = require("@dfinity/principal");
|
|
15
14
|
const constants_1 = require("../../utils/constants");
|
|
16
15
|
class CandidAdapter {
|
|
17
16
|
constructor({ agentManager, agent, didjsCanisterId, }) {
|
|
@@ -49,12 +48,9 @@ class CandidAdapter {
|
|
|
49
48
|
}
|
|
50
49
|
getFromMetadata(canisterId) {
|
|
51
50
|
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
-
if (typeof canisterId === "string") {
|
|
53
|
-
canisterId = principal_1.Principal.fromText(canisterId);
|
|
54
|
-
}
|
|
55
51
|
const status = yield agent_1.CanisterStatus.request({
|
|
56
52
|
agent: this.agent,
|
|
57
|
-
canisterId,
|
|
53
|
+
canisterId: canisterId,
|
|
58
54
|
paths: ["candid"],
|
|
59
55
|
});
|
|
60
56
|
const did = status.get("candid");
|
|
@@ -24,13 +24,13 @@ const createAgentManager_1 = require("./createAgentManager");
|
|
|
24
24
|
* @includeExample ./packages/core/README.md:194-220
|
|
25
25
|
*/
|
|
26
26
|
const createReactorStore = (config) => {
|
|
27
|
-
const
|
|
27
|
+
const withLocalEnv = config.withProcessEnv
|
|
28
28
|
? (0, utils_1.isInLocalOrDevelopment)()
|
|
29
29
|
: undefined;
|
|
30
30
|
const { idlFactory, canisterId, withDevtools = false, initializeOnCreate = true, withVisitor = false, agentManager: maybeAgentManager } = config, agentParameters = __rest(config, ["idlFactory", "canisterId", "withDevtools", "initializeOnCreate", "withVisitor", "agentManager"]);
|
|
31
31
|
const agentManager = maybeAgentManager ||
|
|
32
32
|
(0, createAgentManager_1.createAgentManager)(Object.assign({ withDevtools,
|
|
33
|
-
|
|
33
|
+
withLocalEnv }, agentParameters));
|
|
34
34
|
const actorManager = (0, createActorManager_1.createActorManager)({
|
|
35
35
|
idlFactory,
|
|
36
36
|
canisterId,
|
package/dist/types.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export type { ActorMethod, AuthClientLoginOptions, HttpAgentOptions, ActorSubcla
|
|
|
14
14
|
export interface CreateReactorStoreParameters extends HttpAgentOptions, Omit<ActorManagerParameters, "agentManager"> {
|
|
15
15
|
agentManager?: AgentManager;
|
|
16
16
|
withProcessEnv?: boolean;
|
|
17
|
-
|
|
17
|
+
withLocalEnv?: boolean;
|
|
18
18
|
port?: number;
|
|
19
19
|
}
|
|
20
20
|
export type ActorGetStateFunction<A, M extends FunctionName<A>> = {
|
package/dist/utils/helper.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.stringToHash = exports.generateActorHash = exports.generateHash = exports.generateRequestHash = exports.jsonToString = exports.isInLocalOrDevelopment = exports.createStoreWithOptionalDevtools = void 0;
|
|
4
4
|
const agent_1 = require("@dfinity/agent");
|
|
5
|
-
const candid_1 = require("@dfinity/candid");
|
|
6
5
|
const middleware_1 = require("zustand/middleware");
|
|
7
6
|
const vanilla_1 = require("zustand/vanilla");
|
|
8
7
|
function createStoreWithOptionalDevtools(initialState, config) {
|
|
@@ -51,6 +50,9 @@ const generateActorHash = (actor) => {
|
|
|
51
50
|
exports.generateActorHash = generateActorHash;
|
|
52
51
|
const stringToHash = (str) => {
|
|
53
52
|
const hashBytes = (0, agent_1.hash)(new TextEncoder().encode(str));
|
|
54
|
-
return `0x${
|
|
53
|
+
return `0x${toHexString(hashBytes)}`;
|
|
55
54
|
};
|
|
56
55
|
exports.stringToHash = stringToHash;
|
|
56
|
+
function toHexString(bytes) {
|
|
57
|
+
return new Uint8Array(bytes).reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
|
|
58
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "A library for intracting with the Internet Computer canisters",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"engines": {
|
|
44
44
|
"node": ">=10"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "fc864ae3a99c94f41067e3a3d6ac80d1a66a98f0"
|
|
47
47
|
}
|