@ic-reactor/react 1.14.2 → 1.14.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.
@@ -114,7 +114,7 @@ const actorHooks = (actorManager) => {
114
114
  }, [functionName]);
115
115
  };
116
116
  const useSharedCall = (_a) => {
117
- var { args = [], functionName, throwOnError = false, onError, onLoading, onSuccess, onSuccessResult } = _a, options = __rest(_a, ["args", "functionName", "throwOnError", "onError", "onLoading", "onSuccess", "onSuccessResult"]);
117
+ var { args = [], functionName, throwOnError = false, onError, onLoading, onSuccess, onSuccessResult, onErrorResult } = _a, options = __rest(_a, ["args", "functionName", "throwOnError", "onError", "onLoading", "onSuccess", "onSuccessResult", "onErrorResult"]);
118
118
  const requestKey = React.useMemo(() => (0, utils_1.generateRequestHash)(args), [args]);
119
119
  const [sharedState, setSharedState] = useMethodState(functionName, requestKey);
120
120
  const latestDataRef = React.useRef();
@@ -130,9 +130,18 @@ const actorHooks = (actorManager) => {
130
130
  const data = yield callMethodWithOptions(options)(functionName, ...(replaceArgs !== null && replaceArgs !== void 0 ? replaceArgs : args));
131
131
  latestDataRef.current = data;
132
132
  setSharedState({ data, error: undefined, loading: false });
133
- onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
134
- onSuccessResult === null || onSuccessResult === void 0 ? void 0 : onSuccessResult((0, utils_1.createCompiledResult)(data));
135
133
  onLoading === null || onLoading === void 0 ? void 0 : onLoading(false);
134
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
135
+ const { isOk, value, error } = (0, utils_1.createCompiledResult)(data);
136
+ if (isOk) {
137
+ onSuccessResult === null || onSuccessResult === void 0 ? void 0 : onSuccessResult(value);
138
+ }
139
+ else {
140
+ onErrorResult === null || onErrorResult === void 0 ? void 0 : onErrorResult(error);
141
+ if (throwOnError) {
142
+ throw error;
143
+ }
144
+ }
136
145
  return data;
137
146
  }
138
147
  catch (error) {
@@ -171,7 +180,7 @@ const actorHooks = (actorManager) => {
171
180
  const _b = useSharedCall(rest), { call, requestKey } = _b, state = __rest(_b, ["call", "requestKey"]);
172
181
  const intervalId = React.useRef();
173
182
  React.useEffect(() => {
174
- var _a, _b;
183
+ var _a, _b, _c;
175
184
  if (refetchInterval) {
176
185
  intervalId.current = setInterval(call, refetchInterval);
177
186
  }
@@ -180,7 +189,13 @@ const actorHooks = (actorManager) => {
180
189
  }
181
190
  else if (refetchOnMount && state.data !== undefined) {
182
191
  (_a = rest.onSuccess) === null || _a === void 0 ? void 0 : _a.call(rest, state.data);
183
- (_b = rest.onSuccessResult) === null || _b === void 0 ? void 0 : _b.call(rest, (0, utils_1.createCompiledResult)(state.data));
192
+ const { isOk, value, error } = (0, utils_1.createCompiledResult)(state.data);
193
+ if (isOk) {
194
+ (_b = rest.onSuccessResult) === null || _b === void 0 ? void 0 : _b.call(rest, value);
195
+ }
196
+ else {
197
+ (_c = rest.onErrorResult) === null || _c === void 0 ? void 0 : _c.call(rest, error);
198
+ }
184
199
  }
185
200
  return () => clearInterval(intervalId.current);
186
201
  }, [refetchInterval, refetchOnMount, requestKey]);
@@ -1,5 +1,5 @@
1
1
  import { CallConfig } from "@dfinity/agent";
2
- import type { IDL, ActorState, AuthClientLoginOptions, ActorMethodParameters, ActorMethodReturnType, Identity, Principal, FunctionName, VisitService, AuthState, HttpAgent, AgentState, BaseActor, MethodAttributes, CompiledResult } from "../types";
2
+ import type { IDL, ActorState, AuthClientLoginOptions, ActorMethodParameters, ActorMethodReturnType, Identity, Principal, FunctionName, VisitService, AuthState, HttpAgent, AgentState, BaseActor, MethodAttributes, CompiledResult, ExtractOk, ExtractErr } from "../types";
3
3
  export interface AgentHooksReturnType {
4
4
  useAgent: () => HttpAgent | undefined;
5
5
  useAgentState: () => AgentState;
@@ -52,7 +52,8 @@ export interface UseSharedCallParameters<A, M extends FunctionName<A>> extends C
52
52
  onLoading?: (loading: boolean) => void;
53
53
  onError?: (error: Error | undefined) => void;
54
54
  onSuccess?: (data: ActorMethodReturnType<A[M]>) => void;
55
- onSuccessResult?: (result: CompiledResult<ActorMethodReturnType<A[M]>>) => void;
55
+ onSuccessResult?: (value: ExtractOk<ActorMethodReturnType<A[M]>>) => void;
56
+ onErrorResult?: (error: ExtractErr<ActorMethodReturnType<A[M]>>) => void;
56
57
  throwOnError?: boolean;
57
58
  compileResult?: boolean;
58
59
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/react",
3
- "version": "1.14.2",
3
+ "version": "1.14.3",
4
4
  "description": "A React library for interacting with Internet Computer canisters",
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/core": "^1.14.1",
38
+ "@ic-reactor/core": "^1.14.2",
39
39
  "zustand": "4.5",
40
40
  "zustand-utils": "^1.3"
41
41
  },
@@ -48,7 +48,7 @@
48
48
  "react": ">=16.8",
49
49
  "zustand": "4.5"
50
50
  },
51
- "gitHead": "aea7af668dd77ce24373f4103b6910cfc8cc03de",
51
+ "gitHead": "77d882f8af5d203240e82350171a57185df38e35",
52
52
  "devDependencies": {
53
53
  "@dfinity/agent": ">=2.1",
54
54
  "@dfinity/auth-client": ">=2.1",