@ic-reactor/core 1.6.4 → 1.7.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/README.md +1 -1
- package/dist/classes/actor/index.d.ts +3 -0
- package/dist/classes/actor/index.js +13 -3
- package/dist/classes/actor/types.d.ts +6 -1
- package/dist/classes/adapter/index.js +1 -1
- package/dist/createReactorCore.js +18 -10
- package/dist/types.d.ts +8 -8
- package/package.json +13 -13
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/v1.6.
|
|
22
|
+
<script src="https://github.com/B3Pay/ic-reactor/releases/download/v1.6.5/ic-reactor-core.min.js"></script>
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
### Using `createReactorCore`
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CallConfig } from "@dfinity/agent";
|
|
1
2
|
import type { ActorMethodParameters, ActorMethodReturnType, ActorStore, ActorManagerParameters, FunctionName, VisitService, BaseActor, ActorMethodState, MethodAttributes } from "./types";
|
|
2
3
|
import { IDL } from "@dfinity/candid";
|
|
3
4
|
import type { AgentManager } from "../agent";
|
|
@@ -25,7 +26,9 @@ export declare class ActorManager<A = BaseActor> {
|
|
|
25
26
|
extractMethodAttributes: () => MethodAttributes<A>;
|
|
26
27
|
extractVisitor: () => VisitService<A>;
|
|
27
28
|
private initializeActor;
|
|
29
|
+
private _getActorMethod;
|
|
28
30
|
callMethod: <M extends FunctionName<A>>(functionName: M, ...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
31
|
+
callMethodWithOptions: (options: CallConfig) => <M extends FunctionName<A>>(functionName: M, ...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
29
32
|
get agentManager(): AgentManager;
|
|
30
33
|
getActor: () => A | null;
|
|
31
34
|
getState: ActorStore<A>["getState"];
|
|
@@ -68,7 +68,7 @@ class ActorManager {
|
|
|
68
68
|
const functionName = service[0];
|
|
69
69
|
const type = service[1];
|
|
70
70
|
const visit = ((extractorClass, data) => {
|
|
71
|
-
return type.accept(extractorClass, data
|
|
71
|
+
return type.accept(extractorClass, data);
|
|
72
72
|
});
|
|
73
73
|
acc[functionName] = visit;
|
|
74
74
|
return acc;
|
|
@@ -103,7 +103,7 @@ class ActorManager {
|
|
|
103
103
|
this.updateState({ error: error, initializing: false }, "error");
|
|
104
104
|
}
|
|
105
105
|
};
|
|
106
|
-
this.
|
|
106
|
+
this._getActorMethod = (functionName) => {
|
|
107
107
|
if (!this._actor) {
|
|
108
108
|
throw new Error("Actor not initialized");
|
|
109
109
|
}
|
|
@@ -111,10 +111,20 @@ class ActorManager {
|
|
|
111
111
|
typeof this._actor[functionName] !== "function") {
|
|
112
112
|
throw new Error(`Method ${String(functionName)} not found`);
|
|
113
113
|
}
|
|
114
|
-
|
|
114
|
+
return this._actor[functionName];
|
|
115
|
+
};
|
|
116
|
+
this.callMethod = (functionName, ...args) => __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
const method = this._getActorMethod(functionName);
|
|
115
118
|
const data = yield method(...args);
|
|
116
119
|
return data;
|
|
117
120
|
});
|
|
121
|
+
this.callMethodWithOptions = (options) => {
|
|
122
|
+
return (functionName, ...args) => __awaiter(this, void 0, void 0, function* () {
|
|
123
|
+
const method = this._getActorMethod(functionName);
|
|
124
|
+
const data = yield method.withOptions(options)(...args);
|
|
125
|
+
return data;
|
|
126
|
+
});
|
|
127
|
+
};
|
|
118
128
|
// actor store
|
|
119
129
|
this.getActor = () => {
|
|
120
130
|
return this._actor;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { AgentManager } from "../agent";
|
|
2
2
|
import type { IDL, StoreApiWithDevtools, ActorMethod, ActorSubclass, Principal } from "../../types";
|
|
3
|
+
import { CallConfig } from "@dfinity/agent";
|
|
3
4
|
export interface DefaultActorType {
|
|
4
5
|
[key: string]: ActorMethod;
|
|
5
6
|
}
|
|
@@ -20,7 +21,7 @@ export type VisitorType<V> = V extends IDL.Visitor<infer D, infer R> ? {
|
|
|
20
21
|
returnType: R;
|
|
21
22
|
} : never;
|
|
22
23
|
export type VisitService<A = BaseActor, M extends FunctionName<A> = FunctionName<A>> = {
|
|
23
|
-
[K in M]: <V extends IDL.Visitor<unknown, unknown>>(extractorClass: V, data
|
|
24
|
+
[K in M]: <V extends IDL.Visitor<unknown, unknown>>(extractorClass: V, data: VisitorType<V>["data"]) => ReturnType<V["visitFunc"]>;
|
|
24
25
|
};
|
|
25
26
|
export type ActorMethodParameters<T> = T extends ActorMethod<infer Args, any> ? Args : never;
|
|
26
27
|
export type ActorMethodReturnType<T> = T extends ActorMethod<any, infer Ret> ? Ret : never;
|
|
@@ -34,6 +35,10 @@ export interface ActorMethodState<A = BaseActor, M extends FunctionName<A> = Fun
|
|
|
34
35
|
export type ActorMethodStates<A = BaseActor> = {
|
|
35
36
|
[M in FunctionName<A>]: ActorMethodState<A, M>;
|
|
36
37
|
};
|
|
38
|
+
export type ActorMethodType<A, M extends keyof A> = {
|
|
39
|
+
(...args: ActorMethodParameters<A[M]>): Promise<ActorMethodReturnType<A[M]>>;
|
|
40
|
+
withOptions: (options: CallConfig) => (...args: ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]>>;
|
|
41
|
+
};
|
|
37
42
|
export type ActorState<A = BaseActor> = {
|
|
38
43
|
initialized: boolean;
|
|
39
44
|
initializing: boolean;
|
|
@@ -149,7 +149,7 @@ class CandidAdapter {
|
|
|
149
149
|
if (!this.parserModule) {
|
|
150
150
|
throw new Error("Parser module not available");
|
|
151
151
|
}
|
|
152
|
-
return this.parserModule.
|
|
152
|
+
return this.parserModule.didToJs(candidSource);
|
|
153
153
|
}
|
|
154
154
|
}
|
|
155
155
|
exports.CandidAdapter = CandidAdapter;
|
|
@@ -32,8 +32,8 @@ const utils_1 = require("./utils");
|
|
|
32
32
|
* @includeExample ./packages/core/README.md:32-86
|
|
33
33
|
*/
|
|
34
34
|
const createReactorCore = (config) => {
|
|
35
|
-
const _a = (0, createReactorStore_1.createReactorStore)(config), { subscribeActorState, updateMethodState, callMethod, getState, agentManager } = _a, rest = __rest(_a, ["subscribeActorState", "updateMethodState", "callMethod", "getState", "agentManager"]);
|
|
36
|
-
const actorMethod = (functionName,
|
|
35
|
+
const _a = (0, createReactorStore_1.createReactorStore)(config), { subscribeActorState, updateMethodState, callMethodWithOptions, callMethod, getState, agentManager } = _a, rest = __rest(_a, ["subscribeActorState", "updateMethodState", "callMethodWithOptions", "callMethod", "getState", "agentManager"]);
|
|
36
|
+
const actorMethod = (functionName, args, options = {}) => {
|
|
37
37
|
const requestHash = (0, utils_1.generateRequestHash)(args);
|
|
38
38
|
const updateState = (newState = {}) => {
|
|
39
39
|
updateMethodState(functionName, requestHash, newState);
|
|
@@ -70,7 +70,7 @@ const createReactorCore = (config) => {
|
|
|
70
70
|
error: undefined,
|
|
71
71
|
});
|
|
72
72
|
try {
|
|
73
|
-
const data = yield
|
|
73
|
+
const data = yield callMethodWithOptions(options)(functionName, ...(replaceArgs !== null && replaceArgs !== void 0 ? replaceArgs : args));
|
|
74
74
|
updateState({ data, loading: false });
|
|
75
75
|
return data;
|
|
76
76
|
}
|
|
@@ -97,26 +97,34 @@ const createReactorCore = (config) => {
|
|
|
97
97
|
throw error;
|
|
98
98
|
}
|
|
99
99
|
};
|
|
100
|
-
const queryCall = (
|
|
100
|
+
const queryCall = (_a) => {
|
|
101
|
+
var { functionName, args = [], refetchOnMount = true, refetchInterval = false } = _a, options = __rest(_a, ["functionName", "args", "refetchOnMount", "refetchInterval"]);
|
|
101
102
|
let intervalId = null;
|
|
102
|
-
const
|
|
103
|
+
const _b = actorMethod(functionName, args, options), { call } = _b, rest = __rest(_b, ["call"]);
|
|
103
104
|
if (refetchInterval) {
|
|
104
105
|
intervalId = setInterval(() => {
|
|
105
106
|
call();
|
|
106
107
|
}, refetchInterval);
|
|
107
108
|
}
|
|
109
|
+
const clearRefetchInterval = () => {
|
|
110
|
+
if (intervalId) {
|
|
111
|
+
clearInterval(intervalId);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
108
114
|
let dataPromise = Promise.resolve();
|
|
109
115
|
if (refetchOnMount)
|
|
110
116
|
dataPromise = call();
|
|
111
|
-
return Object.assign(Object.assign({}, rest), { call, dataPromise, intervalId });
|
|
117
|
+
return Object.assign(Object.assign({}, rest), { call, dataPromise, intervalId, clearRefetchInterval });
|
|
112
118
|
};
|
|
113
|
-
const updateCall = (
|
|
114
|
-
|
|
119
|
+
const updateCall = (_a) => {
|
|
120
|
+
var { functionName, args = [] } = _a, options = __rest(_a, ["functionName", "args"]);
|
|
121
|
+
return actorMethod(functionName, args, options);
|
|
115
122
|
};
|
|
116
|
-
return Object.assign(Object.assign({
|
|
123
|
+
return Object.assign(Object.assign({ getState,
|
|
124
|
+
queryCall,
|
|
117
125
|
updateCall,
|
|
118
126
|
callMethod,
|
|
119
|
-
|
|
127
|
+
callMethodWithOptions,
|
|
120
128
|
subscribeActorState }, agentManager), rest);
|
|
121
129
|
};
|
|
122
130
|
exports.createReactorCore = createReactorCore;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import type { ActorMethod, ActorSubclass, HttpAgentOptions, HttpAgent, Identity } from "@dfinity/agent";
|
|
2
|
+
import type { ActorMethod, ActorSubclass, HttpAgentOptions, HttpAgent, Identity, CallConfig } from "@dfinity/agent";
|
|
3
3
|
import type { Principal } from "@dfinity/principal";
|
|
4
4
|
import type { IDL } from "@dfinity/candid";
|
|
5
5
|
import type { ActorManagerParameters, ActorMethodParameters, ActorMethodReturnType, ActorMethodState, BaseActor, FunctionName } from "./classes/actor/types";
|
|
@@ -36,19 +36,19 @@ export type ActorUpdateReturnType<A, M extends FunctionName<A>> = {
|
|
|
36
36
|
subscribe: ActorSubscribeFunction<A, M>;
|
|
37
37
|
call: ActorCallFunction<A, M>;
|
|
38
38
|
};
|
|
39
|
-
export
|
|
39
|
+
export interface ActorQueryParameters<A, M extends FunctionName<A>> extends CallConfig {
|
|
40
40
|
functionName: M;
|
|
41
41
|
args?: ActorMethodParameters<A[M]>;
|
|
42
42
|
refetchOnMount?: boolean;
|
|
43
43
|
refetchInterval?: number | false;
|
|
44
|
-
}
|
|
45
|
-
export
|
|
44
|
+
}
|
|
45
|
+
export interface ActorUpdateParameters<A, M extends FunctionName<A>> extends CallConfig {
|
|
46
46
|
functionName: M;
|
|
47
47
|
args?: ActorMethodParameters<A[M]>;
|
|
48
|
-
}
|
|
49
|
-
export type ActorMethodCall<A = Record<string, ActorMethod>> = <M extends FunctionName<A>>(functionName: M,
|
|
50
|
-
export type ActorQuery<A = Record<string, ActorMethod>> = <M extends FunctionName<A>>(
|
|
51
|
-
export type ActorUpdate<A = Record<string, ActorMethod>> = <M extends FunctionName<A>>(
|
|
48
|
+
}
|
|
49
|
+
export type ActorMethodCall<A = Record<string, ActorMethod>> = <M extends FunctionName<A>>(functionName: M, args: ActorMethodParameters<A[M]>, options?: CallConfig) => ActorUpdateReturnType<A, M>;
|
|
50
|
+
export type ActorQuery<A = Record<string, ActorMethod>> = <M extends FunctionName<A>>(params: ActorQueryParameters<A, M>) => ActorQueryReturnType<A, M>;
|
|
51
|
+
export type ActorUpdate<A = Record<string, ActorMethod>> = <M extends FunctionName<A>>(params: ActorUpdateParameters<A, M>) => ActorUpdateReturnType<A, M>;
|
|
52
52
|
export interface CreateReactorCoreParameters extends CreateReactorStoreParameters {
|
|
53
53
|
}
|
|
54
54
|
export interface CreateReactorCoreReturnType<A = BaseActor> extends AgentManager, Omit<ActorManager<A>, "updateMethodState"> {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
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",
|
|
@@ -26,22 +26,22 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://b3pay.github.io/ic-reactor/modules/core.html",
|
|
28
28
|
"peerDependencies": {
|
|
29
|
-
"@dfinity/agent": "^1.
|
|
30
|
-
"@dfinity/auth-client": "^1.
|
|
31
|
-
"@dfinity/candid": "^1.
|
|
32
|
-
"@dfinity/identity": "^1.
|
|
33
|
-
"@dfinity/principal": "^1.
|
|
29
|
+
"@dfinity/agent": "^1.3",
|
|
30
|
+
"@dfinity/auth-client": "^1.3",
|
|
31
|
+
"@dfinity/candid": "^1.3",
|
|
32
|
+
"@dfinity/identity": "^1.3",
|
|
33
|
+
"@dfinity/principal": "^1.3"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@dfinity/agent": "^1.
|
|
37
|
-
"@dfinity/auth-client": "^1.
|
|
38
|
-
"@dfinity/candid": "^1.
|
|
39
|
-
"@dfinity/identity": "^1.
|
|
40
|
-
"@dfinity/principal": "^1.
|
|
36
|
+
"@dfinity/agent": "^1.3",
|
|
37
|
+
"@dfinity/auth-client": "^1.3",
|
|
38
|
+
"@dfinity/candid": "^1.3",
|
|
39
|
+
"@dfinity/identity": "^1.3",
|
|
40
|
+
"@dfinity/principal": "^1.3",
|
|
41
41
|
"zustand": "^4.5"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@ic-reactor/parser": "^0.
|
|
44
|
+
"@ic-reactor/parser": "^0.3.0"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest",
|
|
@@ -56,5 +56,5 @@
|
|
|
56
56
|
"engines": {
|
|
57
57
|
"node": ">=10"
|
|
58
58
|
},
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "0fac1036d11ae1b2498e6568d5eff514bd657116"
|
|
60
60
|
}
|