@ic-reactor/core 0.0.9 → 0.1.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/dist/index.d.ts +7 -5
- package/dist/index.js +13 -24
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import type { HttpAgent, HttpAgentOptions } from "@dfinity/agent";
|
|
2
|
-
import type { ActorSubclass,
|
|
2
|
+
import type { ActorSubclass, ReActorAuthStore } from "@ic-reactor/store";
|
|
3
3
|
import { ReActorQuery, ReActorUpdate } from "./types";
|
|
4
|
-
export type ReActorContextType<A = ActorSubclass<any>> =
|
|
4
|
+
export type ReActorContextType<A = ActorSubclass<any>> = ReActorAuthStore<A>;
|
|
5
5
|
export interface CreateReActorOptions extends HttpAgentOptions {
|
|
6
6
|
initializeOnMount?: boolean;
|
|
7
7
|
}
|
|
8
8
|
export declare const createReActor: <A extends unknown>(actorInitializer: (agent: HttpAgent) => A, options?: CreateReActorOptions) => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
unsubscribe: () => void;
|
|
10
|
+
initialize: (agentOptions?: HttpAgentOptions | undefined, identity?: import("@dfinity/agent").Identity | undefined) => void;
|
|
11
|
+
authenticate: () => Promise<void>;
|
|
12
|
+
actorStore: import("@ic-reactor/store").ReActorActorStore<A>;
|
|
13
|
+
authStore: ReActorAuthStore<A>;
|
|
12
14
|
queryCall: ReActorQuery<A>;
|
|
13
15
|
updateCall: ReActorUpdate<A>;
|
|
14
16
|
};
|
package/dist/index.js
CHANGED
|
@@ -27,32 +27,24 @@ const defaultCreateReActorOptions = {
|
|
|
27
27
|
};
|
|
28
28
|
const createReActor = (actorInitializer, options = {}) => {
|
|
29
29
|
const optionsWithDefaults = Object.assign(Object.assign({}, defaultCreateReActorOptions), options);
|
|
30
|
-
const
|
|
31
|
-
if (optionsWithDefaults.initializeOnMount) {
|
|
32
|
-
try {
|
|
33
|
-
initializeActor();
|
|
34
|
-
}
|
|
35
|
-
catch (e) {
|
|
36
|
-
console.error(e);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
30
|
+
const _a = (0, store_1.createReActorStore)((agent) => actorInitializer(agent), optionsWithDefaults), { callMethod, actorStore, authStore } = _a, rest = __rest(_a, ["callMethod", "actorStore", "authStore"]);
|
|
39
31
|
const updateMethodState = (method, args = [], newState) => {
|
|
40
32
|
const hash = (0, store_1.generateRequestHash)(args);
|
|
41
|
-
|
|
42
|
-
if (!state.
|
|
33
|
+
actorStore.setState((state) => {
|
|
34
|
+
if (!state.methodState) {
|
|
43
35
|
console.error("Actor not initialized");
|
|
44
36
|
return state;
|
|
45
37
|
}
|
|
46
|
-
if (!state.
|
|
38
|
+
if (!state.methodState[method]) {
|
|
47
39
|
console.error(`Method ${String(method)} not found`);
|
|
48
40
|
return state;
|
|
49
41
|
}
|
|
50
|
-
const currentMethodState = state.
|
|
42
|
+
const currentMethodState = state.methodState[method].states[hash] || {
|
|
51
43
|
loading: false,
|
|
52
44
|
data: undefined,
|
|
53
45
|
error: undefined,
|
|
54
46
|
};
|
|
55
|
-
return Object.assign(Object.assign({}, state), {
|
|
47
|
+
return Object.assign(Object.assign({}, state), { methodState: Object.assign(Object.assign({}, state.methodState), { [method]: Object.assign(Object.assign({}, state.methodState[method]), { states: Object.assign(Object.assign({}, state.methodState[method].states), { [hash]: Object.assign(Object.assign({}, currentMethodState), newState) }) }) }) });
|
|
56
48
|
});
|
|
57
49
|
return hash;
|
|
58
50
|
};
|
|
@@ -60,7 +52,7 @@ const createReActor = (actorInitializer, options = {}) => {
|
|
|
60
52
|
try {
|
|
61
53
|
const requestHash = updateMethodState(functionName, args);
|
|
62
54
|
const getState = (key) => {
|
|
63
|
-
const state =
|
|
55
|
+
const state = actorStore.getState().methodState[functionName].states[requestHash];
|
|
64
56
|
switch (key) {
|
|
65
57
|
case "data":
|
|
66
58
|
return state.data;
|
|
@@ -73,8 +65,8 @@ const createReActor = (actorInitializer, options = {}) => {
|
|
|
73
65
|
}
|
|
74
66
|
};
|
|
75
67
|
const subscribe = (callback) => {
|
|
76
|
-
const unsubscribe =
|
|
77
|
-
const methodState = state.
|
|
68
|
+
const unsubscribe = actorStore.subscribe((state) => {
|
|
69
|
+
const methodState = state.methodState[functionName];
|
|
78
70
|
const methodStateHash = methodState.states[requestHash];
|
|
79
71
|
if (methodStateHash) {
|
|
80
72
|
callback(methodStateHash);
|
|
@@ -88,7 +80,7 @@ const createReActor = (actorInitializer, options = {}) => {
|
|
|
88
80
|
error: undefined,
|
|
89
81
|
});
|
|
90
82
|
try {
|
|
91
|
-
const data = yield
|
|
83
|
+
const data = yield callMethod(functionName, ...(replaceArgs !== null && replaceArgs !== void 0 ? replaceArgs : args));
|
|
92
84
|
updateMethodState(functionName, args, { data, loading: false });
|
|
93
85
|
return data;
|
|
94
86
|
}
|
|
@@ -131,12 +123,9 @@ const createReActor = (actorInitializer, options = {}) => {
|
|
|
131
123
|
const updateCall = ({ functionName, args = [] }) => {
|
|
132
124
|
return reActorMethod(functionName, ...args);
|
|
133
125
|
};
|
|
134
|
-
return {
|
|
135
|
-
|
|
136
|
-
actions,
|
|
137
|
-
initializeActor,
|
|
126
|
+
return Object.assign({ actorStore,
|
|
127
|
+
authStore,
|
|
138
128
|
queryCall,
|
|
139
|
-
updateCall,
|
|
140
|
-
};
|
|
129
|
+
updateCall }, rest);
|
|
141
130
|
};
|
|
142
131
|
exports.createReActor = createReActor;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "A React library for interacting with Dfinity actors",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -40,11 +40,11 @@
|
|
|
40
40
|
"@dfinity/candid": "0.20",
|
|
41
41
|
"@dfinity/identity": "^0.20",
|
|
42
42
|
"@dfinity/principal": "^0.20",
|
|
43
|
-
"@ic-reactor/store": "^0.
|
|
43
|
+
"@ic-reactor/store": "^0.1.1",
|
|
44
44
|
"zustand": "4.4"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"@peculiar/webcrypto": "1.4"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "4e63567f41291e0368febc1e19d9eda570828d67"
|
|
50
50
|
}
|