@ic-reactor/core 0.0.1 → 0.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/index.js CHANGED
@@ -46,7 +46,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
46
46
  const store_1 = __importStar(require("@ic-reactor/store"));
47
47
  const defaultCreateReActorOptions = {
48
48
  initializeOnMount: true,
49
- host: process.env.NODE_ENV === "production"
49
+ host: process.env.NODE_ENV === "production" || process.env.DFX_NETWORK === "ic"
50
50
  ? "https://icp-api.io"
51
51
  : "http://localhost:4943",
52
52
  };
@@ -68,10 +68,16 @@ const createReActor = (actorInitializer, options = {}) => {
68
68
  console.error("Actor not initialized");
69
69
  return state;
70
70
  }
71
- const methodState = state.actorState[method] || { states: {} };
72
- const currentMethodState = methodState.states[hash] || {};
73
- const updatedStates = Object.assign(Object.assign({}, methodState.states), { [hash]: Object.assign(Object.assign({}, currentMethodState), newState) });
74
- return Object.assign(Object.assign({}, state), { actorState: Object.assign(Object.assign({}, state.actorState), { [method]: Object.assign(Object.assign({}, methodState), { states: updatedStates }) }) });
71
+ if (!state.actorState[method]) {
72
+ console.error(`Method ${String(method)} not found`);
73
+ return state;
74
+ }
75
+ const currentMethodState = state.actorState[method].states[hash] || {
76
+ loading: false,
77
+ data: undefined,
78
+ error: undefined,
79
+ };
80
+ return Object.assign(Object.assign({}, state), { actorState: Object.assign(Object.assign({}, state.actorState), { [method]: Object.assign(Object.assign({}, state.actorState[method]), { states: Object.assign(Object.assign({}, state.actorState[method].states), { [hash]: Object.assign(Object.assign({}, currentMethodState), newState) }) }) }) });
75
81
  });
76
82
  return hash;
77
83
  };
@@ -80,17 +86,15 @@ const createReActor = (actorInitializer, options = {}) => {
80
86
  const requestHash = updateMethodState(functionName, args);
81
87
  const getState = (key) => {
82
88
  const state = store.getState().actorState[functionName].states[requestHash];
83
- if (key === "data") {
84
- return state.data;
85
- }
86
- else if (key === "loading") {
87
- return state.loading;
88
- }
89
- else if (key === "error") {
90
- return state.error;
91
- }
92
- else {
93
- return state;
89
+ switch (key) {
90
+ case "data":
91
+ return state.data;
92
+ case "loading":
93
+ return state.loading;
94
+ case "error":
95
+ return state.error;
96
+ default:
97
+ return state;
94
98
  }
95
99
  };
96
100
  const subscribe = (callback) => {
package/dist/types.d.ts CHANGED
@@ -1,6 +1,11 @@
1
1
  /// <reference types="node" />
2
2
  import { ActorMethod, ActorSubclass, ExtractReActorMethodArgs, ExtractReActorMethodReturnType, ReActorMethodState } from "@ic-reactor/store";
3
- export type ReActorGetStateFunction<A, M extends keyof A> = (key?: keyof ReActorMethodState<A, M>["states"][string]) => ReActorMethodState<A, M>["states"][string] | ExtractReActorMethodReturnType<A[M]> | boolean | Error | undefined;
3
+ export type ReActorGetStateFunction<A, M extends keyof A> = {
4
+ (key: "data"): ExtractReActorMethodReturnType<A[M]> | undefined;
5
+ (key: "loading"): boolean;
6
+ (key: "error"): Error | undefined;
7
+ (): ReActorMethodState<A, M>["states"][string];
8
+ };
4
9
  export type ReActorSubscribeFunction<A, M extends keyof A> = (callback: (state: ReActorMethodState<A, M>["states"][string]) => void) => () => void;
5
10
  export type ReActorCallFunction<A, M extends keyof A> = (replaceArgs?: ExtractReActorMethodArgs<A[M]>) => Promise<ExtractReActorMethodReturnType<A[M]>>;
6
11
  export type ReActorQueryReturn<A, M extends keyof A> = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/core",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "A React library for interacting with Dfinity actors",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",
@@ -47,5 +47,5 @@
47
47
  "peerDependencies": {
48
48
  "@peculiar/webcrypto": "1.4"
49
49
  },
50
- "gitHead": "b1f673a150b591ad3cfbb8964794e4ea9b0ff1c2"
50
+ "gitHead": "b64c094f66a177bd410f9231e229b6f6da24f081"
51
51
  }