@ic-reactor/core 1.0.2 → 1.0.3
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/actor/index.d.ts +0 -3
- package/dist/actor/index.js +4 -10
- package/dist/actor/types.d.ts +5 -5
- package/dist/index.d.ts +3 -51
- package/dist/index.js +4 -207
- package/dist/main.d.ts +10 -0
- package/dist/main.js +146 -0
- package/dist/other.d.ts +33 -0
- package/dist/other.js +44 -0
- package/dist/store.d.ts +11 -0
- package/dist/store.js +38 -0
- package/dist/types.d.ts +1 -1
- package/package.json +2 -2
package/dist/actor/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { HttpAgent } from "@dfinity/agent";
|
|
2
1
|
import type { CanisterId, ActorMethodArgs, ActorMethodReturnType, ActorStore, ActorManagerOptions, FunctionName, VisitService, BaseActor, ActorMethodState } from "./types";
|
|
3
2
|
import type { AgentManager } from "../agent";
|
|
4
3
|
import type { UpdateAgentOptions } from "../types";
|
|
@@ -10,7 +9,6 @@ export declare class ActorManager<A = BaseActor> {
|
|
|
10
9
|
actorStore: ActorStore<A>;
|
|
11
10
|
visitFunction: VisitService<A>;
|
|
12
11
|
private initialState;
|
|
13
|
-
unsubscribeActor: () => void;
|
|
14
12
|
private updateState;
|
|
15
13
|
updateMethodState: (method: FunctionName<A>, hash: string, newState: Partial<{
|
|
16
14
|
data: ActorMethodReturnType<A[FunctionName<A>]> | undefined;
|
|
@@ -23,7 +21,6 @@ export declare class ActorManager<A = BaseActor> {
|
|
|
23
21
|
private initializeActor;
|
|
24
22
|
callMethod: <M extends FunctionName<A>>(functionName: M, ...args: ActorMethodArgs<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
25
23
|
get agentManager(): AgentManager;
|
|
26
|
-
getAgent: () => HttpAgent;
|
|
27
24
|
getActor: () => A | null;
|
|
28
25
|
getState: ActorStore<A>["getState"];
|
|
29
26
|
subscribeActorState: ActorStore<A>["subscribe"];
|
package/dist/actor/index.js
CHANGED
|
@@ -29,11 +29,7 @@ class ActorManager {
|
|
|
29
29
|
this.updateMethodState = (method, hash, newState) => {
|
|
30
30
|
this.actorStore.setState((state) => {
|
|
31
31
|
const methodState = state.methodState[method] || {};
|
|
32
|
-
const currentMethodState = methodState[hash] ||
|
|
33
|
-
loading: false,
|
|
34
|
-
data: undefined,
|
|
35
|
-
error: undefined,
|
|
36
|
-
};
|
|
32
|
+
const currentMethodState = methodState[hash] || DEFAULT_STATE;
|
|
37
33
|
const updatedMethodState = Object.assign(Object.assign({}, methodState), { [hash]: Object.assign(Object.assign({}, currentMethodState), newState) });
|
|
38
34
|
return Object.assign(Object.assign({}, state), { methodState: Object.assign(Object.assign({}, state.methodState), { [method]: updatedMethodState }) });
|
|
39
35
|
});
|
|
@@ -82,9 +78,6 @@ class ActorManager {
|
|
|
82
78
|
const data = yield method(...args);
|
|
83
79
|
return data;
|
|
84
80
|
});
|
|
85
|
-
this.getAgent = () => {
|
|
86
|
-
return this._agentManager.getAgent();
|
|
87
|
-
};
|
|
88
81
|
// actor store
|
|
89
82
|
this.getActor = () => {
|
|
90
83
|
return this._actor;
|
|
@@ -100,11 +93,11 @@ class ActorManager {
|
|
|
100
93
|
};
|
|
101
94
|
const { agentManager, canisterId, idlFactory, withVisitor = false, withDevtools = false, initializeOnCreate = true, } = actorConfig;
|
|
102
95
|
this._agentManager = agentManager;
|
|
103
|
-
this.
|
|
96
|
+
this._agentManager.subscribeAgent(this.initializeActor);
|
|
104
97
|
this.canisterId = canisterId;
|
|
105
98
|
this._idlFactory = idlFactory;
|
|
106
99
|
if (withVisitor) {
|
|
107
|
-
this.visitFunction = this.extractService();
|
|
100
|
+
this.visitFunction = withVisitor ? this.extractService() : emptyVisitor;
|
|
108
101
|
}
|
|
109
102
|
else {
|
|
110
103
|
this.visitFunction = emptyVisitor;
|
|
@@ -140,3 +133,4 @@ const emptyVisitor = new Proxy({}, {
|
|
|
140
133
|
throw new Error(`Cannot visit function "${String(prop)}" without initializing the actor with the visitor option, please set the withVisitor option to true when creating the actor manager.`);
|
|
141
134
|
},
|
|
142
135
|
});
|
|
136
|
+
const DEFAULT_STATE = { data: undefined, error: undefined, loading: false };
|
package/dist/actor/types.d.ts
CHANGED
|
@@ -26,21 +26,21 @@ export type VisitService<A = BaseActor, M extends FunctionName<A> = FunctionName
|
|
|
26
26
|
};
|
|
27
27
|
export type ActorMethodArgs<T> = T extends ActorMethod<infer Args, any> ? Args : never;
|
|
28
28
|
export type ActorMethodReturnType<T> = T extends ActorMethod<any, infer Ret> ? Ret : never;
|
|
29
|
-
export interface ActorMethodState<A, M extends
|
|
29
|
+
export interface ActorMethodState<A = BaseActor, M extends FunctionName<A> = FunctionName<A>> {
|
|
30
30
|
[key: string]: {
|
|
31
31
|
data: ActorMethodReturnType<A[M]> | undefined;
|
|
32
32
|
loading: boolean;
|
|
33
33
|
error: Error | undefined;
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
|
-
export type ActorMethodStates<A> = {
|
|
37
|
-
[M in
|
|
36
|
+
export type ActorMethodStates<A = BaseActor> = {
|
|
37
|
+
[M in FunctionName<A>]: ActorMethodState<A, M>;
|
|
38
38
|
};
|
|
39
|
-
export type ActorState<A> = {
|
|
39
|
+
export type ActorState<A = BaseActor> = {
|
|
40
40
|
initialized: boolean;
|
|
41
41
|
initializing: boolean;
|
|
42
42
|
error: Error | undefined;
|
|
43
43
|
methodState: ActorMethodStates<A>;
|
|
44
44
|
};
|
|
45
45
|
export type ActorStore<A = BaseActor> = StoreApi<ActorState<A>>;
|
|
46
|
-
export type CallActorMethod<A = BaseActor> = <M extends
|
|
46
|
+
export type CallActorMethod<A = BaseActor> = <M extends FunctionName<A> = FunctionName<A>>(functionName: M, ...args: ActorMethodArgs<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,54 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { CandidAdapter } from "./candid";
|
|
5
|
-
/**
|
|
6
|
-
* The Core module is the main entry point for the library.
|
|
7
|
-
* Create a new actor manager with the given options.
|
|
8
|
-
* Its create a new agent manager if not provided.
|
|
9
|
-
*
|
|
10
|
-
* @category Main
|
|
11
|
-
* @includeExample ./packages/core/README.md:26-80
|
|
12
|
-
*/
|
|
13
|
-
export declare const createReactorCore: <A = BaseActor>({ isLocalEnv, withProcessEnv, ...options }: CreateReactorOptions) => ReactorCore<A>;
|
|
14
|
-
/**
|
|
15
|
-
* The `CandidAdapter` class is used to interact with a canister and retrieve its Candid interface definition.
|
|
16
|
-
* It provides methods to fetch the Candid definition either from the canister's metadata or by using a temporary hack method.
|
|
17
|
-
* If both methods fail, it throws an error.
|
|
18
|
-
*
|
|
19
|
-
* @category Main
|
|
20
|
-
* @includeExample ./packages/core/README.md:145-186
|
|
21
|
-
*/
|
|
22
|
-
export declare const createCandidAdapter: (options: CandidAdapterOptions) => CandidAdapter;
|
|
23
|
-
/**
|
|
24
|
-
* Create a new actor manager with the given options.
|
|
25
|
-
* Its create a new agent manager if not provided.
|
|
26
|
-
* It also creates a new actor manager with the given options.
|
|
27
|
-
*
|
|
28
|
-
* @category Main
|
|
29
|
-
* @includeExample ./packages/core/README.md:194-220
|
|
30
|
-
*/
|
|
31
|
-
export declare const createReactorStore: <A = BaseActor>(options: CreateReactorStoreOptions) => ActorManager<A>;
|
|
32
|
-
/**
|
|
33
|
-
* Agent manager handles the lifecycle of the `@dfinity/agent`.
|
|
34
|
-
* It is responsible for creating agent and managing the agent's state.
|
|
35
|
-
* You can use it to subscribe to the agent changes.
|
|
36
|
-
* login and logout to the internet identity.
|
|
37
|
-
*
|
|
38
|
-
* @category Main
|
|
39
|
-
* @includeExample ./packages/core/README.md:226-254
|
|
40
|
-
*/
|
|
41
|
-
export declare const createAgentManager: (options?: AgentManagerOptions) => AgentManager;
|
|
42
|
-
/**
|
|
43
|
-
* Actor manager handles the lifecycle of the actors.
|
|
44
|
-
* It is responsible for creating and managing the actors.
|
|
45
|
-
* You can use it to call and visit the actor's methods.
|
|
46
|
-
* It also provides a way to interact with the actor's state.
|
|
47
|
-
*
|
|
48
|
-
* @category Main
|
|
49
|
-
* @includeExample ./packages/core/README.md:262-277
|
|
50
|
-
*/
|
|
51
|
-
export declare const createActorManager: <A = BaseActor>(options: ActorManagerOptions) => ActorManager<A>;
|
|
1
|
+
export * from "./main";
|
|
2
|
+
export * from "./store";
|
|
3
|
+
export * from "./other";
|
|
52
4
|
export * from "./actor";
|
|
53
5
|
export * from "./agent";
|
|
54
6
|
export * from "./candid";
|
package/dist/index.js
CHANGED
|
@@ -25,214 +25,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
25
25
|
__setModuleDefault(result, mod);
|
|
26
26
|
return result;
|
|
27
27
|
};
|
|
28
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
29
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
31
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
32
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
33
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
34
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
38
|
-
var t = {};
|
|
39
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
40
|
-
t[p] = s[p];
|
|
41
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
42
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
43
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
44
|
-
t[p[i]] = s[p[i]];
|
|
45
|
-
}
|
|
46
|
-
return t;
|
|
47
|
-
};
|
|
48
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
49
|
-
exports.tools = exports.types =
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const tools_1 = require("./tools");
|
|
54
|
-
const candid_1 = require("./candid");
|
|
55
|
-
/**
|
|
56
|
-
* The Core module is the main entry point for the library.
|
|
57
|
-
* Create a new actor manager with the given options.
|
|
58
|
-
* Its create a new agent manager if not provided.
|
|
59
|
-
*
|
|
60
|
-
* @category Main
|
|
61
|
-
* @includeExample ./packages/core/README.md:26-80
|
|
62
|
-
*/
|
|
63
|
-
const createReactorCore = (_a) => {
|
|
64
|
-
var { isLocalEnv, withProcessEnv = false } = _a, options = __rest(_a, ["isLocalEnv", "withProcessEnv"]);
|
|
65
|
-
isLocalEnv = isLocalEnv || (withProcessEnv ? (0, tools_1.isInLocalOrDevelopment)() : false);
|
|
66
|
-
const _b = (0, exports.createReactorStore)(Object.assign({ isLocalEnv }, options)), { subscribeActorState, updateMethodState, callMethod, getState, agentManager } = _b, rest = __rest(_b, ["subscribeActorState", "updateMethodState", "callMethod", "getState", "agentManager"]);
|
|
67
|
-
const reActorMethod = (functionName, ...args) => {
|
|
68
|
-
const requestHash = (0, tools_1.generateRequestHash)(args);
|
|
69
|
-
const updateState = (newState = {}) => {
|
|
70
|
-
updateMethodState(functionName, requestHash, newState);
|
|
71
|
-
};
|
|
72
|
-
updateState();
|
|
73
|
-
try {
|
|
74
|
-
const methodState = ((key) => {
|
|
75
|
-
const state = getState().methodState[functionName][requestHash];
|
|
76
|
-
switch (key) {
|
|
77
|
-
case "data":
|
|
78
|
-
return state.data;
|
|
79
|
-
case "loading":
|
|
80
|
-
return state.loading;
|
|
81
|
-
case "error":
|
|
82
|
-
return state.error;
|
|
83
|
-
default:
|
|
84
|
-
return state;
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
const subscribe = (callback) => {
|
|
88
|
-
const unsubscribe = subscribeActorState((state) => {
|
|
89
|
-
const methodState = state.methodState[functionName];
|
|
90
|
-
const methodStateHash = methodState[requestHash];
|
|
91
|
-
if (methodStateHash) {
|
|
92
|
-
callback(methodStateHash);
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
return unsubscribe;
|
|
96
|
-
};
|
|
97
|
-
const call = (replaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
98
|
-
updateState({
|
|
99
|
-
loading: true,
|
|
100
|
-
error: undefined,
|
|
101
|
-
});
|
|
102
|
-
try {
|
|
103
|
-
const data = yield callMethod(functionName, ...(replaceArgs !== null && replaceArgs !== void 0 ? replaceArgs : args));
|
|
104
|
-
updateState({ data, loading: false });
|
|
105
|
-
return data;
|
|
106
|
-
}
|
|
107
|
-
catch (error) {
|
|
108
|
-
updateState({
|
|
109
|
-
error: error,
|
|
110
|
-
loading: false,
|
|
111
|
-
});
|
|
112
|
-
throw error;
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
return {
|
|
116
|
-
requestHash,
|
|
117
|
-
subscribe,
|
|
118
|
-
getState: methodState,
|
|
119
|
-
call,
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
catch (error) {
|
|
123
|
-
updateState({
|
|
124
|
-
error: error,
|
|
125
|
-
loading: false,
|
|
126
|
-
});
|
|
127
|
-
throw error;
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
const queryCall = ({ functionName, args = [], refetchOnMount = true, refetchInterval = false, }) => {
|
|
131
|
-
let intervalId = null;
|
|
132
|
-
const _a = reActorMethod(functionName, ...args), { call } = _a, rest = __rest(_a, ["call"]);
|
|
133
|
-
if (refetchInterval) {
|
|
134
|
-
intervalId = setInterval(() => {
|
|
135
|
-
call();
|
|
136
|
-
}, refetchInterval);
|
|
137
|
-
}
|
|
138
|
-
let dataPromise = Promise.resolve();
|
|
139
|
-
if (refetchOnMount)
|
|
140
|
-
dataPromise = call();
|
|
141
|
-
return Object.assign(Object.assign({}, rest), { call, dataPromise, intervalId });
|
|
142
|
-
};
|
|
143
|
-
const updateCall = ({ functionName, args = [] }) => {
|
|
144
|
-
return reActorMethod(functionName, ...args);
|
|
145
|
-
};
|
|
146
|
-
const login = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
147
|
-
const authClient = agentManager.getAuthClient();
|
|
148
|
-
if (!authClient) {
|
|
149
|
-
yield agentManager.authenticate();
|
|
150
|
-
}
|
|
151
|
-
if (!authClient) {
|
|
152
|
-
throw new Error("Auth client not initialized");
|
|
153
|
-
}
|
|
154
|
-
yield authClient.login(Object.assign({ identityProvider: isLocalEnv
|
|
155
|
-
? constants_1.IC_INTERNET_IDENTITY_PROVIDER
|
|
156
|
-
: constants_1.LOCAL_INTERNET_IDENTITY_PROVIDER }, options));
|
|
157
|
-
});
|
|
158
|
-
const logout = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
159
|
-
const authClient = agentManager.getAuthClient();
|
|
160
|
-
if (!authClient) {
|
|
161
|
-
throw new Error("Auth client not initialized");
|
|
162
|
-
}
|
|
163
|
-
yield authClient.logout(options);
|
|
164
|
-
yield agentManager.authenticate();
|
|
165
|
-
});
|
|
166
|
-
return Object.assign(Object.assign({ queryCall,
|
|
167
|
-
updateCall,
|
|
168
|
-
callMethod,
|
|
169
|
-
getState,
|
|
170
|
-
login,
|
|
171
|
-
logout,
|
|
172
|
-
subscribeActorState }, agentManager), rest);
|
|
173
|
-
};
|
|
174
|
-
exports.createReactorCore = createReactorCore;
|
|
175
|
-
/**
|
|
176
|
-
* The `CandidAdapter` class is used to interact with a canister and retrieve its Candid interface definition.
|
|
177
|
-
* It provides methods to fetch the Candid definition either from the canister's metadata or by using a temporary hack method.
|
|
178
|
-
* If both methods fail, it throws an error.
|
|
179
|
-
*
|
|
180
|
-
* @category Main
|
|
181
|
-
* @includeExample ./packages/core/README.md:145-186
|
|
182
|
-
*/
|
|
183
|
-
const createCandidAdapter = (options) => {
|
|
184
|
-
return new candid_1.CandidAdapter(options);
|
|
185
|
-
};
|
|
186
|
-
exports.createCandidAdapter = createCandidAdapter;
|
|
187
|
-
/**
|
|
188
|
-
* Create a new actor manager with the given options.
|
|
189
|
-
* Its create a new agent manager if not provided.
|
|
190
|
-
* It also creates a new actor manager with the given options.
|
|
191
|
-
*
|
|
192
|
-
* @category Main
|
|
193
|
-
* @includeExample ./packages/core/README.md:194-220
|
|
194
|
-
*/
|
|
195
|
-
const createReactorStore = (options) => {
|
|
196
|
-
const { idlFactory, canisterId, withDevtools = false, initializeOnCreate = true, withVisitor = false, agentManager: maybeAgentManager } = options, agentOptions = __rest(options, ["idlFactory", "canisterId", "withDevtools", "initializeOnCreate", "withVisitor", "agentManager"]);
|
|
197
|
-
const agentManager = maybeAgentManager ||
|
|
198
|
-
(0, exports.createAgentManager)(Object.assign({ withDevtools }, agentOptions));
|
|
199
|
-
const actorManager = (0, exports.createActorManager)({
|
|
200
|
-
idlFactory,
|
|
201
|
-
canisterId,
|
|
202
|
-
agentManager,
|
|
203
|
-
withVisitor,
|
|
204
|
-
withDevtools,
|
|
205
|
-
initializeOnCreate,
|
|
206
|
-
});
|
|
207
|
-
return actorManager;
|
|
208
|
-
};
|
|
209
|
-
exports.createReactorStore = createReactorStore;
|
|
210
|
-
/**
|
|
211
|
-
* Agent manager handles the lifecycle of the `@dfinity/agent`.
|
|
212
|
-
* It is responsible for creating agent and managing the agent's state.
|
|
213
|
-
* You can use it to subscribe to the agent changes.
|
|
214
|
-
* login and logout to the internet identity.
|
|
215
|
-
*
|
|
216
|
-
* @category Main
|
|
217
|
-
* @includeExample ./packages/core/README.md:226-254
|
|
218
|
-
*/
|
|
219
|
-
const createAgentManager = (options) => {
|
|
220
|
-
return new agent_1.AgentManager(options);
|
|
221
|
-
};
|
|
222
|
-
exports.createAgentManager = createAgentManager;
|
|
223
|
-
/**
|
|
224
|
-
* Actor manager handles the lifecycle of the actors.
|
|
225
|
-
* It is responsible for creating and managing the actors.
|
|
226
|
-
* You can use it to call and visit the actor's methods.
|
|
227
|
-
* It also provides a way to interact with the actor's state.
|
|
228
|
-
*
|
|
229
|
-
* @category Main
|
|
230
|
-
* @includeExample ./packages/core/README.md:262-277
|
|
231
|
-
*/
|
|
232
|
-
const createActorManager = (options) => {
|
|
233
|
-
return new actor_1.ActorManager(options);
|
|
234
|
-
};
|
|
235
|
-
exports.createActorManager = createActorManager;
|
|
29
|
+
exports.tools = exports.types = void 0;
|
|
30
|
+
__exportStar(require("./main"), exports);
|
|
31
|
+
__exportStar(require("./store"), exports);
|
|
32
|
+
__exportStar(require("./other"), exports);
|
|
236
33
|
__exportStar(require("./actor"), exports);
|
|
237
34
|
__exportStar(require("./agent"), exports);
|
|
238
35
|
__exportStar(require("./candid"), exports);
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BaseActor, ReactorCore, CreateReactorOptions } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* The Core module is the main entry point for the library.
|
|
4
|
+
* Create a new actor manager with the given options.
|
|
5
|
+
* Its create a new agent manager if not provided.
|
|
6
|
+
*
|
|
7
|
+
* @category Main
|
|
8
|
+
* @includeExample ./packages/core/README.md:26-80
|
|
9
|
+
*/
|
|
10
|
+
export declare const createReactorCore: <A = BaseActor>({ isLocalEnv, withProcessEnv, ...options }: CreateReactorOptions) => ReactorCore<A>;
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
exports.createReactorCore = void 0;
|
|
24
|
+
const constants_1 = require("./tools/constants");
|
|
25
|
+
const tools_1 = require("./tools");
|
|
26
|
+
const store_1 = require("./store");
|
|
27
|
+
/**
|
|
28
|
+
* The Core module is the main entry point for the library.
|
|
29
|
+
* Create a new actor manager with the given options.
|
|
30
|
+
* Its create a new agent manager if not provided.
|
|
31
|
+
*
|
|
32
|
+
* @category Main
|
|
33
|
+
* @includeExample ./packages/core/README.md:26-80
|
|
34
|
+
*/
|
|
35
|
+
const createReactorCore = (_a) => {
|
|
36
|
+
var { isLocalEnv, withProcessEnv = false } = _a, options = __rest(_a, ["isLocalEnv", "withProcessEnv"]);
|
|
37
|
+
isLocalEnv = isLocalEnv || (withProcessEnv ? (0, tools_1.isInLocalOrDevelopment)() : false);
|
|
38
|
+
const _b = (0, store_1.createReactorStore)(Object.assign({ isLocalEnv }, options)), { subscribeActorState, updateMethodState, callMethod, getState, agentManager } = _b, rest = __rest(_b, ["subscribeActorState", "updateMethodState", "callMethod", "getState", "agentManager"]);
|
|
39
|
+
const reActorMethod = (functionName, ...args) => {
|
|
40
|
+
const requestHash = (0, tools_1.generateRequestHash)(args);
|
|
41
|
+
const updateState = (newState = {}) => {
|
|
42
|
+
updateMethodState(functionName, requestHash, newState);
|
|
43
|
+
};
|
|
44
|
+
updateState();
|
|
45
|
+
try {
|
|
46
|
+
const methodState = ((key) => {
|
|
47
|
+
const state = getState().methodState[functionName][requestHash];
|
|
48
|
+
switch (key) {
|
|
49
|
+
case "data":
|
|
50
|
+
return state.data;
|
|
51
|
+
case "loading":
|
|
52
|
+
return state.loading;
|
|
53
|
+
case "error":
|
|
54
|
+
return state.error;
|
|
55
|
+
default:
|
|
56
|
+
return state;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
const subscribe = (callback) => {
|
|
60
|
+
const unsubscribe = subscribeActorState((state) => {
|
|
61
|
+
const methodState = state.methodState[functionName];
|
|
62
|
+
const methodStateHash = methodState[requestHash];
|
|
63
|
+
if (methodStateHash) {
|
|
64
|
+
callback(methodStateHash);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
return unsubscribe;
|
|
68
|
+
};
|
|
69
|
+
const call = (replaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
70
|
+
updateState({
|
|
71
|
+
loading: true,
|
|
72
|
+
error: undefined,
|
|
73
|
+
});
|
|
74
|
+
try {
|
|
75
|
+
const data = yield callMethod(functionName, ...(replaceArgs !== null && replaceArgs !== void 0 ? replaceArgs : args));
|
|
76
|
+
updateState({ data, loading: false });
|
|
77
|
+
return data;
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
updateState({
|
|
81
|
+
error: error,
|
|
82
|
+
loading: false,
|
|
83
|
+
});
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
return {
|
|
88
|
+
requestHash,
|
|
89
|
+
subscribe,
|
|
90
|
+
getState: methodState,
|
|
91
|
+
call,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
updateState({
|
|
96
|
+
error: error,
|
|
97
|
+
loading: false,
|
|
98
|
+
});
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const queryCall = ({ functionName, args = [], refetchOnMount = true, refetchInterval = false, }) => {
|
|
103
|
+
let intervalId = null;
|
|
104
|
+
const _a = reActorMethod(functionName, ...args), { call } = _a, rest = __rest(_a, ["call"]);
|
|
105
|
+
if (refetchInterval) {
|
|
106
|
+
intervalId = setInterval(() => {
|
|
107
|
+
call();
|
|
108
|
+
}, refetchInterval);
|
|
109
|
+
}
|
|
110
|
+
let dataPromise = Promise.resolve();
|
|
111
|
+
if (refetchOnMount)
|
|
112
|
+
dataPromise = call();
|
|
113
|
+
return Object.assign(Object.assign({}, rest), { call, dataPromise, intervalId });
|
|
114
|
+
};
|
|
115
|
+
const updateCall = ({ functionName, args = [] }) => {
|
|
116
|
+
return reActorMethod(functionName, ...args);
|
|
117
|
+
};
|
|
118
|
+
const login = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
119
|
+
const authClient = agentManager.getAuthClient();
|
|
120
|
+
if (!authClient) {
|
|
121
|
+
yield agentManager.authenticate();
|
|
122
|
+
}
|
|
123
|
+
if (!authClient) {
|
|
124
|
+
throw new Error("Auth client not initialized");
|
|
125
|
+
}
|
|
126
|
+
yield authClient.login(Object.assign({ identityProvider: isLocalEnv
|
|
127
|
+
? constants_1.IC_INTERNET_IDENTITY_PROVIDER
|
|
128
|
+
: constants_1.LOCAL_INTERNET_IDENTITY_PROVIDER }, options));
|
|
129
|
+
});
|
|
130
|
+
const logout = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
131
|
+
const authClient = agentManager.getAuthClient();
|
|
132
|
+
if (!authClient) {
|
|
133
|
+
throw new Error("Auth client not initialized");
|
|
134
|
+
}
|
|
135
|
+
yield authClient.logout(options);
|
|
136
|
+
yield agentManager.authenticate();
|
|
137
|
+
});
|
|
138
|
+
return Object.assign(Object.assign({ queryCall,
|
|
139
|
+
updateCall,
|
|
140
|
+
callMethod,
|
|
141
|
+
getState,
|
|
142
|
+
login,
|
|
143
|
+
logout,
|
|
144
|
+
subscribeActorState }, agentManager), rest);
|
|
145
|
+
};
|
|
146
|
+
exports.createReactorCore = createReactorCore;
|
package/dist/other.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ActorManager } from "./actor";
|
|
2
|
+
import { AgentManager } from "./agent";
|
|
3
|
+
import { CandidAdapter } from "./candid";
|
|
4
|
+
import { ActorManagerOptions, AgentManagerOptions, BaseActor, CandidAdapterOptions } from "./types";
|
|
5
|
+
/**
|
|
6
|
+
* The `CandidAdapter` class is used to interact with a canister and retrieve its Candid interface definition.
|
|
7
|
+
* It provides methods to fetch the Candid definition either from the canister's metadata or by using a temporary hack method.
|
|
8
|
+
* If both methods fail, it throws an error.
|
|
9
|
+
*
|
|
10
|
+
* @category Main
|
|
11
|
+
* @includeExample ./packages/core/README.md:145-186
|
|
12
|
+
*/
|
|
13
|
+
export declare const createCandidAdapter: (options: CandidAdapterOptions) => CandidAdapter;
|
|
14
|
+
/**
|
|
15
|
+
* Agent manager handles the lifecycle of the `@dfinity/agent`.
|
|
16
|
+
* It is responsible for creating agent and managing the agent's state.
|
|
17
|
+
* You can use it to subscribe to the agent changes.
|
|
18
|
+
* login and logout to the internet identity.
|
|
19
|
+
*
|
|
20
|
+
* @category Main
|
|
21
|
+
* @includeExample ./packages/core/README.md:226-254
|
|
22
|
+
*/
|
|
23
|
+
export declare const createAgentManager: (options?: AgentManagerOptions) => AgentManager;
|
|
24
|
+
/**
|
|
25
|
+
* Actor manager handles the lifecycle of the actors.
|
|
26
|
+
* It is responsible for creating and managing the actors.
|
|
27
|
+
* You can use it to call and visit the actor's methods.
|
|
28
|
+
* It also provides a way to interact with the actor's state.
|
|
29
|
+
*
|
|
30
|
+
* @category Main
|
|
31
|
+
* @includeExample ./packages/core/README.md:262-277
|
|
32
|
+
*/
|
|
33
|
+
export declare const createActorManager: <A = BaseActor>(options: ActorManagerOptions) => ActorManager<A>;
|
package/dist/other.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createActorManager = exports.createAgentManager = exports.createCandidAdapter = void 0;
|
|
4
|
+
const actor_1 = require("./actor");
|
|
5
|
+
const agent_1 = require("./agent");
|
|
6
|
+
const candid_1 = require("./candid");
|
|
7
|
+
/**
|
|
8
|
+
* The `CandidAdapter` class is used to interact with a canister and retrieve its Candid interface definition.
|
|
9
|
+
* It provides methods to fetch the Candid definition either from the canister's metadata or by using a temporary hack method.
|
|
10
|
+
* If both methods fail, it throws an error.
|
|
11
|
+
*
|
|
12
|
+
* @category Main
|
|
13
|
+
* @includeExample ./packages/core/README.md:145-186
|
|
14
|
+
*/
|
|
15
|
+
const createCandidAdapter = (options) => {
|
|
16
|
+
return new candid_1.CandidAdapter(options);
|
|
17
|
+
};
|
|
18
|
+
exports.createCandidAdapter = createCandidAdapter;
|
|
19
|
+
/**
|
|
20
|
+
* Agent manager handles the lifecycle of the `@dfinity/agent`.
|
|
21
|
+
* It is responsible for creating agent and managing the agent's state.
|
|
22
|
+
* You can use it to subscribe to the agent changes.
|
|
23
|
+
* login and logout to the internet identity.
|
|
24
|
+
*
|
|
25
|
+
* @category Main
|
|
26
|
+
* @includeExample ./packages/core/README.md:226-254
|
|
27
|
+
*/
|
|
28
|
+
const createAgentManager = (options) => {
|
|
29
|
+
return new agent_1.AgentManager(options);
|
|
30
|
+
};
|
|
31
|
+
exports.createAgentManager = createAgentManager;
|
|
32
|
+
/**
|
|
33
|
+
* Actor manager handles the lifecycle of the actors.
|
|
34
|
+
* It is responsible for creating and managing the actors.
|
|
35
|
+
* You can use it to call and visit the actor's methods.
|
|
36
|
+
* It also provides a way to interact with the actor's state.
|
|
37
|
+
*
|
|
38
|
+
* @category Main
|
|
39
|
+
* @includeExample ./packages/core/README.md:262-277
|
|
40
|
+
*/
|
|
41
|
+
const createActorManager = (options) => {
|
|
42
|
+
return new actor_1.ActorManager(options);
|
|
43
|
+
};
|
|
44
|
+
exports.createActorManager = createActorManager;
|
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ActorManager } from "./actor";
|
|
2
|
+
import type { BaseActor, CreateReactorStoreOptions } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Create a new actor manager with the given options.
|
|
5
|
+
* Its create a new agent manager if not provided.
|
|
6
|
+
* It also creates a new actor manager with the given options.
|
|
7
|
+
*
|
|
8
|
+
* @category Main
|
|
9
|
+
* @includeExample ./packages/core/README.md:194-220
|
|
10
|
+
*/
|
|
11
|
+
export declare const createReactorStore: <A = BaseActor>(options: CreateReactorStoreOptions) => ActorManager<A>;
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.createReactorStore = void 0;
|
|
15
|
+
const other_1 = require("./other");
|
|
16
|
+
/**
|
|
17
|
+
* Create a new actor manager with the given options.
|
|
18
|
+
* Its create a new agent manager if not provided.
|
|
19
|
+
* It also creates a new actor manager with the given options.
|
|
20
|
+
*
|
|
21
|
+
* @category Main
|
|
22
|
+
* @includeExample ./packages/core/README.md:194-220
|
|
23
|
+
*/
|
|
24
|
+
const createReactorStore = (options) => {
|
|
25
|
+
const { idlFactory, canisterId, withDevtools = false, initializeOnCreate = true, withVisitor = false, agentManager: maybeAgentManager } = options, agentOptions = __rest(options, ["idlFactory", "canisterId", "withDevtools", "initializeOnCreate", "withVisitor", "agentManager"]);
|
|
26
|
+
const agentManager = maybeAgentManager ||
|
|
27
|
+
(0, other_1.createAgentManager)(Object.assign({ withDevtools }, agentOptions));
|
|
28
|
+
const actorManager = (0, other_1.createActorManager)({
|
|
29
|
+
idlFactory,
|
|
30
|
+
canisterId,
|
|
31
|
+
agentManager,
|
|
32
|
+
withVisitor,
|
|
33
|
+
withDevtools,
|
|
34
|
+
initializeOnCreate,
|
|
35
|
+
});
|
|
36
|
+
return actorManager;
|
|
37
|
+
};
|
|
38
|
+
exports.createReactorStore = createReactorStore;
|
package/dist/types.d.ts
CHANGED
|
@@ -11,10 +11,10 @@ export * from "./actor/types";
|
|
|
11
11
|
export * from "./candid/types";
|
|
12
12
|
export type { ActorMethod, HttpAgentOptions, ActorSubclass, Principal, HttpAgent, Identity, IDL, };
|
|
13
13
|
export interface CreateReactorOptions extends CreateReactorStoreOptions {
|
|
14
|
+
withProcessEnv?: boolean;
|
|
14
15
|
}
|
|
15
16
|
export interface CreateReactorStoreOptions extends HttpAgentOptions, Omit<ActorManagerOptions, "agentManager"> {
|
|
16
17
|
agentManager?: AgentManager;
|
|
17
|
-
withProcessEnv?: boolean;
|
|
18
18
|
isLocalEnv?: boolean;
|
|
19
19
|
port?: number;
|
|
20
20
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
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": "acf8b5da61713482264c9eb5c227dc94a7e43cbc"
|
|
47
47
|
}
|