@ic-reactor/react 0.1.2 → 0.2.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 CHANGED
@@ -87,7 +87,3 @@ Contributions to `@ic-reactor/react` are welcome! Please read our [contributing
87
87
  ## License
88
88
 
89
89
  `@ic-reactor/react` is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.
90
-
91
- ---
92
-
93
- This README provides a foundational overview of your package. You should expand it with more detailed documentation, examples, and links to additional resources or documentation as needed. Replace placeholders with actual information and links relevant to your project.
package/dist/hooks.d.ts CHANGED
@@ -1,16 +1,18 @@
1
- import { CallMethod, ExtractReActorMethodArgs } from "@ic-reactor/store";
1
+ import { CallMethod, ExtractReActorMethodArgs, ReActorActorStore, ReActorMethodField } from "@ic-reactor/store";
2
2
  import { ReActorUseQueryArgs, ReActorUseUpdateArgs } from "./types";
3
- export declare const getCallHooks: <A extends unknown>(callMethod: CallMethod<A>) => {
3
+ export declare const getCallHooks: <A extends unknown>(callMethod: CallMethod<A>, actorStore: ReActorActorStore<A>) => {
4
4
  useQueryCall: <M extends keyof A>({ autoRefresh, refreshInterval, disableInitialCall, ...rest }: ReActorUseQueryArgs<A, M>) => {
5
5
  data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined;
6
6
  error: Error | undefined;
7
7
  loading: boolean;
8
- recall: (replaceArgs?: ExtractReActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined>;
8
+ field: ReActorMethodField<A> | undefined;
9
+ call: (replaceArgs?: ExtractReActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined>;
9
10
  };
10
11
  useUpdateCall: <M_1 extends keyof A>(args: ReActorUseUpdateArgs<A, M_1>) => {
11
12
  data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M_1]> | undefined;
12
13
  error: Error | undefined;
13
14
  loading: boolean;
14
15
  call: (replaceArgs?: ExtractReActorMethodArgs<A[M_1]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M_1]> | undefined>;
16
+ field: ReActorMethodField<A> | undefined;
15
17
  };
16
18
  };
package/dist/hooks.js CHANGED
@@ -22,7 +22,11 @@ var __rest = (this && this.__rest) || function (s, e) {
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
23
  exports.getCallHooks = void 0;
24
24
  const react_1 = require("react");
25
- const getCallHooks = (callMethod) => {
25
+ function findFieldByFunctionName(methodFields, functionName) {
26
+ return methodFields.find((f) => f.functionName === functionName);
27
+ }
28
+ const getCallHooks = (callMethod, actorStore) => {
29
+ const { methodFields } = actorStore.getState();
26
30
  const useReActorCall = ({ onError, onSuccess, onLoading, args = [], functionName, }) => {
27
31
  const [state, setState] = (0, react_1.useState)({
28
32
  data: undefined,
@@ -47,28 +51,29 @@ const getCallHooks = (callMethod) => {
47
51
  setState((prevState) => (Object.assign(Object.assign({}, prevState), { error: error, loading: false })));
48
52
  }
49
53
  }), [args, functionName, onError, onSuccess, onLoading]);
50
- return Object.assign({ call }, state);
54
+ const field = (0, react_1.useRef)(findFieldByFunctionName(methodFields, functionName));
55
+ return Object.assign({ call, field: field.current }, state);
51
56
  };
52
57
  const useQueryCall = (_a) => {
53
58
  var { autoRefresh, refreshInterval = 5000, disableInitialCall } = _a, rest = __rest(_a, ["autoRefresh", "refreshInterval", "disableInitialCall"]);
54
- const _b = useReActorCall(rest), { call: recall } = _b, state = __rest(_b, ["call"]);
59
+ const _b = useReActorCall(rest), { call } = _b, state = __rest(_b, ["call"]);
55
60
  let intervalId = (0, react_1.useRef)(undefined);
56
61
  (0, react_1.useEffect)(() => {
57
62
  // Auto-refresh logic
58
63
  if (autoRefresh) {
59
64
  intervalId.current = setInterval(() => {
60
- recall();
65
+ call();
61
66
  }, refreshInterval);
62
67
  }
63
68
  // Initial call logic
64
69
  if (!disableInitialCall) {
65
- recall();
70
+ call();
66
71
  }
67
72
  return () => {
68
73
  clearInterval(intervalId.current);
69
74
  };
70
75
  }, [disableInitialCall, autoRefresh, refreshInterval]);
71
- return Object.assign({ recall }, state);
76
+ return Object.assign({ call }, state);
72
77
  };
73
78
  const useUpdateCall = (args) => {
74
79
  return useReActorCall(args);
package/dist/index.d.ts CHANGED
@@ -1,27 +1,36 @@
1
- import type { HttpAgent } from "@dfinity/agent";
2
1
  import { AuthClientLoginOptions } from "@dfinity/auth-client";
3
2
  import type { ActorSubclass, ReActorAuthStore, ReActorOptions } from "@ic-reactor/store";
4
3
  export type ReActorContextType<A = ActorSubclass<any>> = ReActorAuthStore<A>;
5
- export declare const createReActor: <A extends unknown>(actorInitializer: (agent: HttpAgent) => A, options?: ReActorOptions) => {
6
- useActorStore: () => import("@ic-reactor/store").ReActorActorState<A>;
4
+ export declare const createReActor: <A extends unknown>(options: ReActorOptions) => {
5
+ useActorStore: () => {
6
+ initialize: (agentOptions?: import("@dfinity/agent").HttpAgentOptions | undefined, identity?: import("@ic-reactor/store").Identity | undefined) => void;
7
+ actor: A | null;
8
+ initialized: boolean;
9
+ initializing: boolean;
10
+ error: Error | undefined;
11
+ methodState: import("@ic-reactor/store").ReActorMethodStates<A>;
12
+ methodFields: import("@ic-reactor/store").ReActorMethodField<A>[];
13
+ };
7
14
  useAuthStore: () => import("@ic-reactor/store").ReActorAuthState<A>;
8
15
  useQueryCall: <M extends keyof A>({ autoRefresh, refreshInterval, disableInitialCall, ...rest }: import("./types").ReActorUseQueryArgs<A, M>) => {
9
16
  data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined;
10
17
  error: Error | undefined;
11
18
  loading: boolean;
12
- recall: (replaceArgs?: import("@ic-reactor/store").ExtractReActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined>;
19
+ field: import("@ic-reactor/store").ReActorMethodField<A> | undefined;
20
+ call: (replaceArgs?: import("@ic-reactor/store").ExtractReActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined>;
13
21
  };
14
22
  useUpdateCall: <M_1 extends keyof A>(args: import("./types").ReActorUseUpdateArgs<A, M_1>) => {
15
23
  data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M_1]> | undefined;
16
24
  error: Error | undefined;
17
25
  loading: boolean;
18
26
  call: (replaceArgs?: import("@ic-reactor/store").ExtractReActorMethodArgs<A[M_1]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M_1]> | undefined>;
27
+ field: import("@ic-reactor/store").ReActorMethodField<A> | undefined;
19
28
  };
20
29
  useAuthClient: () => {
21
30
  authClient: import("@dfinity/auth-client").AuthClient | null;
22
31
  authenticated: boolean;
23
32
  authenticating: boolean;
24
- identity: import("@dfinity/agent").Identity | null;
33
+ identity: import("@ic-reactor/store").Identity | null;
25
34
  login: (options?: AuthClientLoginOptions) => Promise<void>;
26
35
  logout: (options?: {
27
36
  returnTo?: string;
package/dist/index.js CHANGED
@@ -14,18 +14,14 @@ const store_1 = require("@ic-reactor/store");
14
14
  const react_1 = require("react");
15
15
  const zustand_1 = require("zustand");
16
16
  const hooks_1 = require("./hooks");
17
- const defaultCreateReActorOptions = {
18
- initializeOnMount: true,
19
- host: (process === null || process === void 0 ? void 0 : process.env.NODE_ENV) === "production" || (process === null || process === void 0 ? void 0 : process.env.DFX_NETWORK) === "ic"
20
- ? "https://icp-api.io"
21
- : "http://localhost:4943",
22
- };
23
- const createReActor = (actorInitializer, options = {}) => {
24
- const optionsWithDefaults = Object.assign(Object.assign({}, defaultCreateReActorOptions), options);
25
- const { callMethod, authenticate, authStore, actorStore } = (0, store_1.createReActorStore)((agent) => actorInitializer(agent), optionsWithDefaults);
17
+ const createReActor = (options) => {
18
+ const isLocal = typeof process !== "undefined" &&
19
+ (process.env.NODE_ENV === "development" ||
20
+ process.env.DFX_NETWORK === "local");
21
+ const { callMethod, initialize, authenticate, authStore, actorStore } = (0, store_1.createReActorStore)(Object.assign({ isLocal }, options));
26
22
  const useActorStore = () => {
27
23
  const actorState = (0, zustand_1.useStore)(actorStore, (state) => state);
28
- return actorState;
24
+ return Object.assign(Object.assign({}, actorState), { initialize });
29
25
  };
30
26
  const useAuthStore = () => {
31
27
  const authState = (0, zustand_1.useStore)(authStore, (state) => state);
@@ -77,7 +73,7 @@ const createReActor = (actorInitializer, options = {}) => {
77
73
  loginError,
78
74
  };
79
75
  };
80
- const { useQueryCall, useUpdateCall } = (0, hooks_1.getCallHooks)(callMethod);
76
+ const { useQueryCall, useUpdateCall } = (0, hooks_1.getCallHooks)(callMethod, actorStore);
81
77
  return {
82
78
  useActorStore,
83
79
  useAuthStore,
package/dist/types.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { ExtractReActorMethodArgs, ExtractReActorMethodReturnType } from "@ic-reactor/store";
2
2
  export type ReActorCallArgs<A, M extends keyof A> = {
3
- functionName: M;
3
+ functionName: M & string;
4
4
  args?: ExtractReActorMethodArgs<A[M]>;
5
5
  onLoading?: (loading: boolean) => void;
6
6
  onError?: (error: Error | unknown) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/react",
3
- "version": "0.1.2",
3
+ "version": "0.2.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",
@@ -28,25 +28,25 @@
28
28
  "scripts": {
29
29
  "test": "npx jest",
30
30
  "start": "tsc watch",
31
- "build": "npx tsc",
32
- "clean": "rimraf dist"
31
+ "build": "npx rimraf dist && npx tsc",
32
+ "clean": "npx rimraf dist"
33
33
  },
34
34
  "engines": {
35
35
  "node": ">=10"
36
36
  },
37
37
  "dependencies": {
38
+ "@ic-reactor/store": "^0.2.1",
39
+ "zustand-utils": "^1.3"
40
+ },
41
+ "peerDependencies": {
38
42
  "@dfinity/agent": "^0.20",
39
43
  "@dfinity/auth-client": "^0.20",
40
44
  "@dfinity/candid": "0.20",
41
45
  "@dfinity/identity": "^0.20",
42
46
  "@dfinity/principal": "^0.20",
43
- "@ic-reactor/store": "^0.1.2",
44
- "zustand": "4.4",
45
- "zustand-utils": "^1.3"
46
- },
47
- "peerDependencies": {
48
47
  "@peculiar/webcrypto": "1.4",
49
- "react": ">=16.8"
48
+ "react": ">=16.8",
49
+ "zustand": "4.4"
50
50
  },
51
- "gitHead": "6710ddba717421913c2a9765e7c626fea204e2e7"
51
+ "gitHead": "fcc3b98a8d2f1590a7631225eaec246c231b1815"
52
52
  }