@ic-reactor/core 2.0.0-alpha.0 → 2.0.1
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 +2 -5
- package/dist/classes/actor/index.js +202 -139
- package/dist/classes/actor/types.d.ts +61 -5
- package/dist/classes/adapter/index.d.ts +1 -1
- package/dist/classes/adapter/index.js +80 -101
- package/dist/classes/agent/index.d.ts +5 -7
- package/dist/classes/agent/index.js +189 -183
- package/dist/classes/agent/types.d.ts +66 -6
- package/dist/classes/types.d.ts +12 -5
- package/dist/classes/types.js +1 -0
- package/dist/createReactorCore.js +14 -32
- package/dist/createReactorStore.js +5 -13
- package/dist/index.js +17 -7
- package/dist/types.d.ts +42 -3
- package/dist/utils/constants.d.ts +0 -8
- package/dist/utils/constants.js +1 -9
- package/dist/utils/hash.d.ts +12 -0
- package/dist/utils/hash.js +70 -0
- package/dist/utils/helper.d.ts +54 -6
- package/dist/utils/helper.js +139 -40
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +18 -7
- package/package.json +25 -25
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ yarn add @ic-reactor/core
|
|
|
19
19
|
or you can use the UMD version:
|
|
20
20
|
|
|
21
21
|
```html
|
|
22
|
-
<script src="https://github.com/B3Pay/ic-reactor/releases/download/
|
|
22
|
+
<script src="https://github.com/B3Pay/ic-reactor/releases/download/v2.0.0/ic-reactor-core.min.js"></script>
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
### Using `createReactorCore`
|
|
@@ -14,11 +14,7 @@ export declare class ActorManager<A = BaseActor> {
|
|
|
14
14
|
visitFunction: VisitService<A>;
|
|
15
15
|
methodAttributes: MethodAttributes<A>;
|
|
16
16
|
private updateState;
|
|
17
|
-
updateMethodState: (method: FunctionName<A>, hash: string, newState: Partial<
|
|
18
|
-
data: ActorMethodReturnType<A[FunctionName<A>]> | undefined;
|
|
19
|
-
loading: boolean;
|
|
20
|
-
error: Error | undefined;
|
|
21
|
-
}>) => void;
|
|
17
|
+
updateMethodState: (method: FunctionName<A>, hash: string, newState: Partial<ActorMethodState<A, typeof method>[string]>) => void;
|
|
22
18
|
constructor(actorConfig: ActorManagerParameters);
|
|
23
19
|
initialize: (options?: UpdateAgentParameters) => Promise<void>;
|
|
24
20
|
extractInterface: () => IDL.ServiceClass;
|
|
@@ -28,6 +24,7 @@ export declare class ActorManager<A = BaseActor> {
|
|
|
28
24
|
private _getActorMethod;
|
|
29
25
|
callMethod: <M extends FunctionName<A>>(functionName: M, ...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
30
26
|
callMethodWithOptions: (options: CallConfig) => <M extends FunctionName<A>>(functionName: M, ...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
27
|
+
call: <M extends FunctionName<A>>(functionName: M, ...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
31
28
|
get agentManager(): AgentManager;
|
|
32
29
|
getActor: () => A | null;
|
|
33
30
|
getState: ActorStore<A>["getState"];
|
|
@@ -1,161 +1,68 @@
|
|
|
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.ActorManager = void 0;
|
|
13
4
|
/* eslint-disable no-console */
|
|
14
5
|
const agent_1 = require("@dfinity/agent");
|
|
15
6
|
const helper_1 = require("../../utils/helper");
|
|
16
7
|
const candid_1 = require("@dfinity/candid");
|
|
17
|
-
const
|
|
8
|
+
const ACTOR_INITIAL_STATE = {
|
|
9
|
+
name: "",
|
|
10
|
+
version: 0,
|
|
11
|
+
methodState: {},
|
|
12
|
+
initializing: false,
|
|
13
|
+
isInitializing: false,
|
|
14
|
+
initialized: false,
|
|
15
|
+
isInitialized: false,
|
|
16
|
+
error: undefined,
|
|
17
|
+
};
|
|
18
18
|
class ActorManager {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
this.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
type: ((0, helper_1.isQuery)(func) ? "query" : "update"),
|
|
46
|
-
validate: (arg) => func.argTypes.some((t, i) => t.covariant(arg[i])),
|
|
19
|
+
_actor = null;
|
|
20
|
+
_idlFactory;
|
|
21
|
+
_agentManager;
|
|
22
|
+
_unsubscribeAgent;
|
|
23
|
+
_subscribers = [];
|
|
24
|
+
canisterId;
|
|
25
|
+
actorStore;
|
|
26
|
+
visitFunction;
|
|
27
|
+
methodAttributes;
|
|
28
|
+
updateState = (newState, action) => {
|
|
29
|
+
this.actorStore.setState((state) => ({ ...state, ...newState }), false, action);
|
|
30
|
+
};
|
|
31
|
+
updateMethodState = (method, hash, newState) => {
|
|
32
|
+
const actionName = `${method}:${newState.error ? "error" : newState.isLoading ? "loading..." : "loaded"}`;
|
|
33
|
+
this.actorStore.setState((state) => {
|
|
34
|
+
const methodState = state.methodState[method] || {};
|
|
35
|
+
const currentMethodState = methodState[hash] || DEFAULT_STATE;
|
|
36
|
+
const updatedMethodState = {
|
|
37
|
+
...methodState,
|
|
38
|
+
[hash]: { ...currentMethodState, ...newState },
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
...state,
|
|
42
|
+
methodState: {
|
|
43
|
+
...state.methodState,
|
|
44
|
+
[method]: updatedMethodState,
|
|
47
45
|
},
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
return a.attributes.type === "query" ? -1 : 1;
|
|
54
|
-
});
|
|
55
|
-
return methodAttributesArray.reduce((acc, { name, attributes }) => {
|
|
56
|
-
acc[name] = attributes;
|
|
57
|
-
return acc;
|
|
58
|
-
}, {});
|
|
59
|
-
};
|
|
60
|
-
this.extractVisitor = () => {
|
|
61
|
-
const iface = this.extractInterface();
|
|
62
|
-
return iface._fields.reduce((acc, service) => {
|
|
63
|
-
const functionName = service[0];
|
|
64
|
-
const type = service[1];
|
|
65
|
-
const visit = ((extractorClass, data) => {
|
|
66
|
-
return type.accept(extractorClass, data);
|
|
67
|
-
});
|
|
68
|
-
acc[functionName] = visit;
|
|
69
|
-
return acc;
|
|
70
|
-
}, {});
|
|
71
|
-
};
|
|
72
|
-
this.initializeActor = (agent) => {
|
|
73
|
-
const network = this._agentManager.getNetwork();
|
|
74
|
-
console.info(`Initializing actor ${this.canisterId} on ${network} network`);
|
|
75
|
-
const { _idlFactory, canisterId } = this;
|
|
76
|
-
this.updateState({
|
|
77
|
-
initializing: true,
|
|
78
|
-
initialized: false,
|
|
79
|
-
methodState: {},
|
|
80
|
-
}, "initializing");
|
|
81
|
-
try {
|
|
82
|
-
if (!agent) {
|
|
83
|
-
throw new Error("Agent not initialized");
|
|
84
|
-
}
|
|
85
|
-
this._actor = agent_1.Actor.createActor(_idlFactory, {
|
|
86
|
-
agent,
|
|
87
|
-
canisterId,
|
|
88
|
-
});
|
|
89
|
-
if (!this._actor) {
|
|
90
|
-
throw new Error("Failed to initialize actor");
|
|
91
|
-
}
|
|
92
|
-
this.updateState({
|
|
93
|
-
initializing: false,
|
|
94
|
-
initialized: true,
|
|
95
|
-
}, "initialized");
|
|
96
|
-
}
|
|
97
|
-
catch (error) {
|
|
98
|
-
console.error("Error in initializeActor:", error);
|
|
99
|
-
this.updateState({ error: error, initializing: false }, "error");
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
this._getActorMethod = (functionName) => {
|
|
103
|
-
if (!this._actor) {
|
|
104
|
-
throw new Error("Actor not initialized");
|
|
105
|
-
}
|
|
106
|
-
if (!this._actor[functionName] ||
|
|
107
|
-
typeof this._actor[functionName] !== "function") {
|
|
108
|
-
throw new Error(`Method ${String(functionName)} not found`);
|
|
109
|
-
}
|
|
110
|
-
return this._actor[functionName];
|
|
111
|
-
};
|
|
112
|
-
this.callMethod = (functionName, ...args) => __awaiter(this, void 0, void 0, function* () {
|
|
113
|
-
const method = this._getActorMethod(functionName);
|
|
114
|
-
const data = yield method(...args);
|
|
115
|
-
return data;
|
|
116
|
-
});
|
|
117
|
-
this.callMethodWithOptions = (options) => {
|
|
118
|
-
return (functionName, ...args) => __awaiter(this, void 0, void 0, function* () {
|
|
119
|
-
const method = this._getActorMethod(functionName);
|
|
120
|
-
const data = yield method.withOptions(options)(...args);
|
|
121
|
-
return data;
|
|
122
|
-
});
|
|
123
|
-
};
|
|
124
|
-
// actor store
|
|
125
|
-
this.getActor = () => {
|
|
126
|
-
return this._actor;
|
|
127
|
-
};
|
|
128
|
-
this.getState = () => {
|
|
129
|
-
return this.actorStore.getState();
|
|
130
|
-
};
|
|
131
|
-
this.subscribeActorState = (listener) => {
|
|
132
|
-
const unsubscribe = this.actorStore.subscribe(listener);
|
|
133
|
-
this._subscribers.push(unsubscribe);
|
|
134
|
-
return unsubscribe;
|
|
135
|
-
};
|
|
136
|
-
this.setState = (updater) => {
|
|
137
|
-
return this.actorStore.setState(updater);
|
|
138
|
-
};
|
|
139
|
-
this.cleanup = () => {
|
|
140
|
-
this._unsubscribeAgent();
|
|
141
|
-
this._subscribers.forEach((unsubscribe) => unsubscribe());
|
|
142
|
-
};
|
|
46
|
+
};
|
|
47
|
+
}, false, actionName);
|
|
48
|
+
};
|
|
49
|
+
constructor(actorConfig) {
|
|
143
50
|
const { agentManager, idlFactory, canisterId, name = canisterId.toString(), withVisitor = false, withDevtools = false, initializeOnCreate = true, } = actorConfig;
|
|
144
51
|
if (!canisterId) {
|
|
145
|
-
throw new
|
|
52
|
+
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode("CanisterId is required!"), agent_1.ErrorKindEnum.Unknown);
|
|
146
53
|
}
|
|
147
54
|
this.canisterId = canisterId.toString();
|
|
148
55
|
if (!idlFactory) {
|
|
149
|
-
throw new
|
|
56
|
+
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode("IDLFactory is required!"), agent_1.ErrorKindEnum.Unknown);
|
|
150
57
|
}
|
|
151
58
|
this._idlFactory = idlFactory;
|
|
152
59
|
this.methodAttributes = this.extractMethodAttributes();
|
|
153
60
|
if (!agentManager) {
|
|
154
|
-
throw new
|
|
61
|
+
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode("AgentManager is required!"), agent_1.ErrorKindEnum.Unknown);
|
|
155
62
|
}
|
|
156
63
|
this._agentManager = agentManager;
|
|
157
64
|
// Initialize stores
|
|
158
|
-
this.actorStore = (0, helper_1.createStoreWithOptionalDevtools)(
|
|
65
|
+
this.actorStore = (0, helper_1.createStoreWithOptionalDevtools)({ ...ACTOR_INITIAL_STATE, name }, {
|
|
159
66
|
withDevtools,
|
|
160
67
|
name: "reactor-actor",
|
|
161
68
|
store: canisterId.toString(),
|
|
@@ -168,15 +75,171 @@ class ActorManager {
|
|
|
168
75
|
this.visitFunction = emptyVisitor;
|
|
169
76
|
}
|
|
170
77
|
}
|
|
78
|
+
initialize = async (options) => {
|
|
79
|
+
await this._agentManager.updateAgent(options);
|
|
80
|
+
};
|
|
81
|
+
extractInterface = () => {
|
|
82
|
+
return this._idlFactory({ IDL: candid_1.IDL });
|
|
83
|
+
};
|
|
84
|
+
extractMethodAttributes = () => {
|
|
85
|
+
const iface = this.extractInterface();
|
|
86
|
+
const methodAttributesArray = iface._fields.map(([name, func]) => ({
|
|
87
|
+
name: name,
|
|
88
|
+
attributes: {
|
|
89
|
+
numberOfArgs: func.argTypes.length,
|
|
90
|
+
type: ((0, helper_1.isQuery)(func) ? "query" : "update"),
|
|
91
|
+
validate: (arg) => func.argTypes.some((t, i) => t.covariant(arg[i])),
|
|
92
|
+
},
|
|
93
|
+
}));
|
|
94
|
+
methodAttributesArray.sort((a, b) => {
|
|
95
|
+
if (a.attributes.type === b.attributes.type) {
|
|
96
|
+
return a.attributes.numberOfArgs - b.attributes.numberOfArgs;
|
|
97
|
+
}
|
|
98
|
+
return a.attributes.type === "query" ? -1 : 1;
|
|
99
|
+
});
|
|
100
|
+
return methodAttributesArray.reduce((acc, { name, attributes }) => {
|
|
101
|
+
acc[name] = attributes;
|
|
102
|
+
return acc;
|
|
103
|
+
}, {});
|
|
104
|
+
};
|
|
105
|
+
extractVisitor = () => {
|
|
106
|
+
const iface = this.extractInterface();
|
|
107
|
+
return iface._fields.reduce((acc, service) => {
|
|
108
|
+
const functionName = service[0];
|
|
109
|
+
const type = service[1];
|
|
110
|
+
const visit = ((extractorClass, data) => {
|
|
111
|
+
return type.accept(extractorClass, data);
|
|
112
|
+
});
|
|
113
|
+
acc[functionName] = visit;
|
|
114
|
+
return acc;
|
|
115
|
+
}, {});
|
|
116
|
+
};
|
|
117
|
+
initializeActor = (agent) => {
|
|
118
|
+
const network = this._agentManager.getNetwork();
|
|
119
|
+
console.info(`Initializing actor ${this.canisterId} on ${network} network`);
|
|
120
|
+
const { _idlFactory, canisterId } = this;
|
|
121
|
+
this.updateState({
|
|
122
|
+
initializing: true,
|
|
123
|
+
isInitializing: true,
|
|
124
|
+
initialized: false,
|
|
125
|
+
isInitialized: false,
|
|
126
|
+
methodState: {},
|
|
127
|
+
}, "initializing");
|
|
128
|
+
try {
|
|
129
|
+
if (!agent) {
|
|
130
|
+
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode("Agent not initialized"), agent_1.ErrorKindEnum.Unknown);
|
|
131
|
+
}
|
|
132
|
+
this._actor = agent_1.Actor.createActor(_idlFactory, {
|
|
133
|
+
agent,
|
|
134
|
+
canisterId,
|
|
135
|
+
});
|
|
136
|
+
if (!this._actor) {
|
|
137
|
+
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode("Failed to initialize actor"), agent_1.ErrorKindEnum.Unknown);
|
|
138
|
+
}
|
|
139
|
+
this.updateState({
|
|
140
|
+
initializing: false,
|
|
141
|
+
isInitializing: false,
|
|
142
|
+
initialized: true,
|
|
143
|
+
isInitialized: true,
|
|
144
|
+
}, "initialized");
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
console.error("Error in initializeActor:", error);
|
|
148
|
+
this.updateState({
|
|
149
|
+
error: error,
|
|
150
|
+
initializing: false,
|
|
151
|
+
isInitializing: false,
|
|
152
|
+
}, "error");
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
_getActorMethod = (functionName) => {
|
|
156
|
+
if (!this._actor) {
|
|
157
|
+
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode("Actor not initialized"), agent_1.ErrorKindEnum.Unknown);
|
|
158
|
+
}
|
|
159
|
+
if (!this._actor[functionName] ||
|
|
160
|
+
typeof this._actor[functionName] !== "function") {
|
|
161
|
+
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode(`Method ${String(functionName)} not found`), agent_1.ErrorKindEnum.Unknown);
|
|
162
|
+
}
|
|
163
|
+
return this._actor[functionName];
|
|
164
|
+
};
|
|
165
|
+
callMethod = async (functionName, ...args) => {
|
|
166
|
+
const method = this._getActorMethod(functionName);
|
|
167
|
+
const data = await method(...args);
|
|
168
|
+
return data;
|
|
169
|
+
};
|
|
170
|
+
callMethodWithOptions = (options) => {
|
|
171
|
+
return async (functionName, ...args) => {
|
|
172
|
+
const method = this._getActorMethod(functionName);
|
|
173
|
+
const data = await method.withOptions(options)(...args);
|
|
174
|
+
return data;
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
call = async (functionName, ...args) => {
|
|
178
|
+
const requestHash = (0, helper_1.generateRequestHash)(args);
|
|
179
|
+
try {
|
|
180
|
+
this.updateMethodState(functionName, requestHash, {
|
|
181
|
+
loading: true,
|
|
182
|
+
isLoading: true,
|
|
183
|
+
error: undefined,
|
|
184
|
+
});
|
|
185
|
+
const data = await this.callMethod(functionName, ...args);
|
|
186
|
+
this.updateMethodState(functionName, requestHash, {
|
|
187
|
+
loading: false,
|
|
188
|
+
isLoading: false,
|
|
189
|
+
data,
|
|
190
|
+
});
|
|
191
|
+
return data;
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
this.updateMethodState(functionName, requestHash, {
|
|
195
|
+
loading: false,
|
|
196
|
+
isLoading: false,
|
|
197
|
+
error: error,
|
|
198
|
+
data: undefined,
|
|
199
|
+
});
|
|
200
|
+
throw error;
|
|
201
|
+
}
|
|
202
|
+
};
|
|
171
203
|
// agent store
|
|
172
204
|
get agentManager() {
|
|
173
205
|
return this._agentManager;
|
|
174
206
|
}
|
|
207
|
+
// actor store
|
|
208
|
+
getActor = () => {
|
|
209
|
+
return this._actor;
|
|
210
|
+
};
|
|
211
|
+
getState = () => {
|
|
212
|
+
return this.actorStore.getState();
|
|
213
|
+
};
|
|
214
|
+
// @ts-expect-error: Overrides subscribe method signature
|
|
215
|
+
subscribeActorState = (selectorOrListener, listener, options) => {
|
|
216
|
+
let unsubscribe = helper_1.noop;
|
|
217
|
+
if (listener) {
|
|
218
|
+
unsubscribe = this.actorStore.subscribe(selectorOrListener, listener, options);
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
unsubscribe = this.actorStore.subscribe(selectorOrListener);
|
|
222
|
+
}
|
|
223
|
+
this._subscribers.push(unsubscribe);
|
|
224
|
+
return unsubscribe;
|
|
225
|
+
};
|
|
226
|
+
setState = (updater) => {
|
|
227
|
+
return this.actorStore.setState(updater);
|
|
228
|
+
};
|
|
229
|
+
cleanup = () => {
|
|
230
|
+
this._unsubscribeAgent();
|
|
231
|
+
this._subscribers.forEach((unsubscribe) => unsubscribe());
|
|
232
|
+
};
|
|
175
233
|
}
|
|
176
234
|
exports.ActorManager = ActorManager;
|
|
177
235
|
const emptyVisitor = new Proxy({}, {
|
|
178
236
|
get: function (_, prop) {
|
|
179
|
-
throw new
|
|
237
|
+
throw new agent_1.AgentError(new agent_1.UnexpectedErrorCode(`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.`), agent_1.ErrorKindEnum.Unknown);
|
|
180
238
|
},
|
|
181
239
|
});
|
|
182
|
-
const DEFAULT_STATE = {
|
|
240
|
+
const DEFAULT_STATE = {
|
|
241
|
+
data: undefined,
|
|
242
|
+
error: undefined,
|
|
243
|
+
loading: false,
|
|
244
|
+
isLoading: false,
|
|
245
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AgentManager } from "../agent";
|
|
2
|
-
import type { IDL,
|
|
3
|
-
import { CallConfig } from "@dfinity/agent";
|
|
2
|
+
import type { IDL, ActorMethod, ActorSubclass, Principal, StoreWithAllMiddleware } from "../../types";
|
|
3
|
+
import type { AgentError, CallConfig } from "@dfinity/agent";
|
|
4
4
|
export interface DefaultActorType {
|
|
5
5
|
[key: string]: ActorMethod;
|
|
6
6
|
}
|
|
@@ -26,11 +26,34 @@ export type VisitService<A = BaseActor, M extends FunctionName<A> = FunctionName
|
|
|
26
26
|
};
|
|
27
27
|
export type ActorMethodParameters<T> = T extends ActorMethod<infer Args, any> ? Args : never;
|
|
28
28
|
export type ActorMethodReturnType<T> = T extends ActorMethod<any, infer Ret> ? Ret : never;
|
|
29
|
+
/**
|
|
30
|
+
* Interface representing the state of each actor method.
|
|
31
|
+
*
|
|
32
|
+
* @template A - The actor type, defaulting to `BaseActor`.
|
|
33
|
+
* @template M - A specific method name of the actor.
|
|
34
|
+
*/
|
|
29
35
|
export interface ActorMethodState<A = BaseActor, M extends FunctionName<A> = FunctionName<A>> {
|
|
36
|
+
/**
|
|
37
|
+
* The per-method state object, keyed by the method name.
|
|
38
|
+
*/
|
|
30
39
|
[key: string]: {
|
|
40
|
+
/**
|
|
41
|
+
* The data returned from the actor method call, if available.
|
|
42
|
+
*/
|
|
31
43
|
data: ActorMethodReturnType<A[M]> | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* @deprecated Use `isLoading` instead.
|
|
46
|
+
* Flag indicating whether the actor method is in progress.
|
|
47
|
+
*/
|
|
32
48
|
loading: boolean;
|
|
33
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Flag indicating whether the actor method is in progress.
|
|
51
|
+
*/
|
|
52
|
+
isLoading: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Error thrown during the actor method invocation, if any.
|
|
55
|
+
*/
|
|
56
|
+
error: AgentError | undefined;
|
|
34
57
|
};
|
|
35
58
|
}
|
|
36
59
|
export type ActorMethodStates<A = BaseActor> = {
|
|
@@ -40,15 +63,48 @@ export type ActorMethodType<A, M extends keyof A> = {
|
|
|
40
63
|
(...args: ActorMethodParameters<A[M]>): Promise<ActorMethodReturnType<A[M]>>;
|
|
41
64
|
withOptions: (options: CallConfig) => (...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
42
65
|
};
|
|
66
|
+
/**
|
|
67
|
+
* Represents the state of an actor.
|
|
68
|
+
*
|
|
69
|
+
* @template A - The type of the actor, defaults to `BaseActor`.
|
|
70
|
+
*/
|
|
43
71
|
export type ActorState<A = BaseActor> = {
|
|
72
|
+
/**
|
|
73
|
+
* The name of the actor.
|
|
74
|
+
*/
|
|
44
75
|
name: string;
|
|
76
|
+
/**
|
|
77
|
+
* The version of the actor.
|
|
78
|
+
*/
|
|
45
79
|
version: number;
|
|
80
|
+
/**
|
|
81
|
+
* @deprecated Use `isInitialized` instead.
|
|
82
|
+
* Indicates whether the actor is initialized.
|
|
83
|
+
*/
|
|
46
84
|
initialized: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Indicates whether the actor is initialized.
|
|
87
|
+
*/
|
|
88
|
+
isInitialized: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* @deprecated Use `isInitializing` instead.
|
|
91
|
+
* Indicates whether the actor is in the process of initializing.
|
|
92
|
+
*/
|
|
47
93
|
initializing: boolean;
|
|
48
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Indicates whether the actor is in the process of initializing.
|
|
96
|
+
*/
|
|
97
|
+
isInitializing: boolean;
|
|
98
|
+
/**
|
|
99
|
+
* The error associated with the actor, if any.
|
|
100
|
+
*/
|
|
101
|
+
error: AgentError | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* The state of the actor's methods.
|
|
104
|
+
*/
|
|
49
105
|
methodState: ActorMethodStates<A>;
|
|
50
106
|
};
|
|
51
|
-
export type ActorStore<A = BaseActor> =
|
|
107
|
+
export type ActorStore<A = BaseActor> = StoreWithAllMiddleware<ActorState<A>>;
|
|
52
108
|
export type CallActorMethod<A = BaseActor> = <M extends FunctionName<A> = FunctionName<A>>(functionName: M, ...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
53
109
|
export type MethodAttributes<A = BaseActor> = Record<FunctionName<A>, {
|
|
54
110
|
type: FunctionType;
|
|
@@ -12,7 +12,7 @@ export declare class CandidAdapter {
|
|
|
12
12
|
getCandidDefinition(canisterId: CanisterId): Promise<CandidDefenition>;
|
|
13
13
|
getFromMetadata(canisterId: CanisterId): Promise<string | undefined>;
|
|
14
14
|
getFromTmpHack(canisterId: CanisterId): Promise<string>;
|
|
15
|
-
|
|
15
|
+
evaluateCandidDefinition(data: string): Promise<CandidDefenition>;
|
|
16
16
|
fetchDidTojs(candidSource: string, didjsCanisterId?: string): Promise<[string]>;
|
|
17
17
|
parseDidToJs(candidSource: string): string;
|
|
18
18
|
validateIDL(candidSource: string): boolean;
|