@ic-reactor/react 0.2.2 → 0.2.4

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/hooks.d.ts CHANGED
@@ -1,16 +1,11 @@
1
1
  import { CallMethod, ExtractReActorMethodArgs, ReActorActorStore, ReActorMethodField } from "@ic-reactor/store";
2
2
  import { ReActorUseQueryArgs, ReActorUseUpdateArgs } from "./types";
3
3
  export declare const getCallHooks: <A extends unknown>(callMethod: CallMethod<A>, actorStore: ReActorActorStore<A>) => {
4
- useField: (functionName: keyof A & string) => {
5
- current: ReActorMethodField<A> | undefined;
6
- };
7
4
  useQueryCall: <M extends keyof A>({ autoRefresh, refreshInterval, disableInitialCall, ...rest }: ReActorUseQueryArgs<A, M>) => {
8
5
  data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined;
9
6
  error: Error | undefined;
10
7
  loading: boolean;
11
- field: {
12
- current: ReActorMethodField<A> | undefined;
13
- };
8
+ field: ReActorMethodField<A> | undefined;
14
9
  call: (replaceArgs?: ExtractReActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined>;
15
10
  };
16
11
  useUpdateCall: <M_1 extends keyof A>(args: ReActorUseUpdateArgs<A, M_1>) => {
@@ -18,8 +13,8 @@ export declare const getCallHooks: <A extends unknown>(callMethod: CallMethod<A>
18
13
  error: Error | undefined;
19
14
  loading: boolean;
20
15
  call: (replaceArgs?: ExtractReActorMethodArgs<A[M_1]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M_1]> | undefined>;
21
- field: {
22
- current: ReActorMethodField<A> | undefined;
23
- };
16
+ field: ReActorMethodField<A> | undefined;
24
17
  };
18
+ useMethodField: (functionName: keyof A & string) => ReActorMethodField<A> | undefined;
19
+ useMethodFields: () => ReActorMethodField<A>[];
25
20
  };
package/dist/hooks.js CHANGED
@@ -22,21 +22,17 @@ 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 zustand_1 = require("zustand");
25
26
  const getCallHooks = (callMethod, actorStore) => {
26
- const useField = (functionName) => {
27
- const [field, setField] = (0, react_1.useState)(undefined);
28
- (0, react_1.useEffect)(() => {
29
- const unsubscribe = actorStore.subscribe((state) => {
30
- if (state.methodFields) {
31
- const field = state.methodFields.find((f) => f.functionName === functionName);
32
- setField(field);
33
- }
34
- });
35
- return () => {
36
- unsubscribe();
37
- };
38
- }, [functionName]);
39
- return { current: field };
27
+ const useMethodFields = () => {
28
+ return (0, zustand_1.useStore)(actorStore, (state) => state.methodFields);
29
+ };
30
+ const useMethodField = (functionName) => {
31
+ const methodFields = useMethodFields();
32
+ const field = (0, react_1.useMemo)(() => {
33
+ return methodFields.find((f) => f.functionName === functionName);
34
+ }, [methodFields, functionName]);
35
+ return field;
40
36
  };
41
37
  const useReActorCall = ({ onError, onSuccess, onLoading, args = [], functionName, }) => {
42
38
  const [state, setState] = (0, react_1.useState)({
@@ -62,7 +58,7 @@ const getCallHooks = (callMethod, actorStore) => {
62
58
  setState((prevState) => (Object.assign(Object.assign({}, prevState), { error: error, loading: false })));
63
59
  }
64
60
  }), [args, functionName, onError, onSuccess, onLoading]);
65
- const field = useField(functionName);
61
+ const field = useMethodField(functionName);
66
62
  return Object.assign({ call, field }, state);
67
63
  };
68
64
  const useQueryCall = (_a) => {
@@ -90,9 +86,10 @@ const getCallHooks = (callMethod, actorStore) => {
90
86
  return useReActorCall(args);
91
87
  };
92
88
  return {
93
- useField,
94
89
  useQueryCall,
95
90
  useUpdateCall,
91
+ useMethodField,
92
+ useMethodFields,
96
93
  };
97
94
  };
98
95
  exports.getCallHooks = getCallHooks;
package/dist/index.d.ts CHANGED
@@ -2,9 +2,8 @@ import { AuthClientLoginOptions } from "@dfinity/auth-client";
2
2
  import type { ActorSubclass, ReActorAuthStore, ReActorOptions } from "@ic-reactor/store";
3
3
  export type ReActorContextType<A = ActorSubclass<any>> = ReActorAuthStore<A>;
4
4
  export declare const createReActor: <A extends unknown>(options: ReActorOptions) => {
5
- useField: (functionName: keyof A & string) => {
6
- current: import("@ic-reactor/store").ReActorMethodField<A> | undefined;
7
- };
5
+ useMethodFields: () => import("@ic-reactor/store").ReActorMethodField<A>[];
6
+ useMethodField: (functionName: keyof A & string) => import("@ic-reactor/store").ReActorMethodField<A> | undefined;
8
7
  useActorStore: () => {
9
8
  initialize: (agentOptions?: import("@dfinity/agent").HttpAgentOptions | undefined, identity?: import("@ic-reactor/store").Identity | undefined) => void;
10
9
  actor: A | null;
@@ -19,9 +18,7 @@ export declare const createReActor: <A extends unknown>(options: ReActorOptions)
19
18
  data: import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined;
20
19
  error: Error | undefined;
21
20
  loading: boolean;
22
- field: {
23
- current: import("@ic-reactor/store").ReActorMethodField<A> | undefined;
24
- };
21
+ field: import("@ic-reactor/store").ReActorMethodField<A> | undefined;
25
22
  call: (replaceArgs?: import("@ic-reactor/store").ExtractReActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M]> | undefined>;
26
23
  };
27
24
  useUpdateCall: <M_1 extends keyof A>(args: import("./types").ReActorUseUpdateArgs<A, M_1>) => {
@@ -29,9 +26,7 @@ export declare const createReActor: <A extends unknown>(options: ReActorOptions)
29
26
  error: Error | undefined;
30
27
  loading: boolean;
31
28
  call: (replaceArgs?: import("@ic-reactor/store").ExtractReActorMethodArgs<A[M_1]> | undefined) => Promise<import("@ic-reactor/store").ExtractReActorMethodReturnType<A[M_1]> | undefined>;
32
- field: {
33
- current: import("@ic-reactor/store").ReActorMethodField<A> | undefined;
34
- };
29
+ field: import("@ic-reactor/store").ReActorMethodField<A> | undefined;
35
30
  };
36
31
  useAuthClient: () => {
37
32
  authClient: import("@dfinity/auth-client").AuthClient | null;
package/dist/index.js CHANGED
@@ -73,9 +73,10 @@ const createReActor = (options) => {
73
73
  loginError,
74
74
  };
75
75
  };
76
- const { useQueryCall, useField, useUpdateCall } = (0, hooks_1.getCallHooks)(callMethod, actorStore);
76
+ const { useQueryCall, useMethodField, useMethodFields, useUpdateCall } = (0, hooks_1.getCallHooks)(callMethod, actorStore);
77
77
  return {
78
- useField,
78
+ useMethodFields,
79
+ useMethodField,
79
80
  useActorStore,
80
81
  useAuthStore,
81
82
  useQueryCall,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/react",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "A React library for interacting with Dfinity actors",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -35,7 +35,7 @@
35
35
  "node": ">=10"
36
36
  },
37
37
  "dependencies": {
38
- "@ic-reactor/store": "^0.2.2",
38
+ "@ic-reactor/store": "^0.2.3",
39
39
  "zustand-utils": "^1.3"
40
40
  },
41
41
  "peerDependencies": {
@@ -48,5 +48,5 @@
48
48
  "react": ">=16.8",
49
49
  "zustand": "4.4"
50
50
  },
51
- "gitHead": "97c96f8577f6cf748fca237932c3903638002d1e"
51
+ "gitHead": "ab1f1be917266cfa0c452a3139d0c2263fc59c0a"
52
52
  }