@ic-reactor/react 1.12.1 → 1.13.0

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,24 +114,31 @@ const actorHooks = (actorManager) => {
114
114
  }, [functionName]);
115
115
  };
116
116
  const useSharedCall = (_a) => {
117
- var { args = [], functionName, throwOnError = false, onError, onLoading, onSuccess } = _a, options = __rest(_a, ["args", "functionName", "throwOnError", "onError", "onLoading", "onSuccess"]);
117
+ var { args = [], functionName, throwOnError = false, onError, onLoading, onSuccess, onSuccessResult } = _a, options = __rest(_a, ["args", "functionName", "throwOnError", "onError", "onLoading", "onSuccess", "onSuccessResult"]);
118
118
  const requestKey = React.useMemo(() => (0, utils_1.generateRequestHash)(args), [args]);
119
119
  const [sharedState, setSharedState] = useMethodState(functionName, requestKey);
120
- const reset = React.useCallback(() => updateMethodState(functionName, requestKey, DEFAULT_STATE), [functionName, requestKey]);
120
+ const latestDataRef = React.useRef();
121
+ const reset = React.useCallback(() => {
122
+ updateMethodState(functionName, requestKey, DEFAULT_STATE);
123
+ latestDataRef.current = undefined;
124
+ }, [functionName, requestKey]);
121
125
  const call = React.useCallback((eventOrReplaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
122
126
  setSharedState({ error: undefined, loading: true });
123
127
  onLoading === null || onLoading === void 0 ? void 0 : onLoading(true);
124
128
  try {
125
129
  const replaceArgs = eventOrReplaceArgs instanceof Array ? eventOrReplaceArgs : args;
126
130
  const data = yield callMethodWithOptions(options)(functionName, ...(replaceArgs !== null && replaceArgs !== void 0 ? replaceArgs : args));
131
+ latestDataRef.current = data;
127
132
  setSharedState({ data, error: undefined, loading: false });
128
- onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess((0, utils_1.createCompiledResult)(data));
133
+ onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
134
+ onSuccessResult === null || onSuccessResult === void 0 ? void 0 : onSuccessResult((0, utils_1.createCompiledResult)(data));
129
135
  onLoading === null || onLoading === void 0 ? void 0 : onLoading(false);
130
136
  return data;
131
137
  }
132
138
  catch (error) {
133
139
  // eslint-disable-next-line no-console
134
140
  console.error(`Error calling method ${functionName}:`, error);
141
+ latestDataRef.current = undefined;
135
142
  setSharedState({
136
143
  error: error,
137
144
  loading: false,
@@ -141,16 +148,30 @@ const actorHooks = (actorManager) => {
141
148
  if (throwOnError)
142
149
  throw error;
143
150
  }
144
- }), [args, functionName, options, onError, onLoading, onSuccess, throwOnError]);
145
- const compileResult = React.useCallback(() => (0, utils_1.createCompiledResult)(sharedState === null || sharedState === void 0 ? void 0 : sharedState.data), [sharedState === null || sharedState === void 0 ? void 0 : sharedState.data]);
146
- return Object.assign({ call, reset, compileResult, requestKey }, sharedState);
151
+ }), [
152
+ args,
153
+ functionName,
154
+ options,
155
+ onError,
156
+ onLoading,
157
+ onSuccess,
158
+ onSuccessResult,
159
+ throwOnError,
160
+ ]);
161
+ const compileResult = () => {
162
+ return (0, utils_1.createCompiledResult)(latestDataRef.current || (sharedState === null || sharedState === void 0 ? void 0 : sharedState.data));
163
+ };
164
+ return Object.assign({ call,
165
+ reset,
166
+ compileResult,
167
+ requestKey }, sharedState);
147
168
  };
148
169
  const useQueryCall = (_a) => {
149
170
  var { refetchOnMount = true, refetchInterval = false } = _a, rest = __rest(_a, ["refetchOnMount", "refetchInterval"]);
150
171
  const _b = useSharedCall(rest), { call, requestKey } = _b, state = __rest(_b, ["call", "requestKey"]);
151
172
  const intervalId = React.useRef();
152
173
  React.useEffect(() => {
153
- var _a;
174
+ var _a, _b;
154
175
  if (refetchInterval) {
155
176
  intervalId.current = setInterval(call, refetchInterval);
156
177
  }
@@ -158,7 +179,8 @@ const actorHooks = (actorManager) => {
158
179
  call();
159
180
  }
160
181
  else if (refetchOnMount && state.data !== undefined) {
161
- (_a = rest.onSuccess) === null || _a === void 0 ? void 0 : _a.call(rest, (0, utils_1.createCompiledResult)(state.data));
182
+ (_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));
162
184
  }
163
185
  return () => clearInterval(intervalId.current);
164
186
  }, [refetchInterval, refetchOnMount, requestKey]);
@@ -52,7 +52,8 @@ export interface UseSharedCallParameters<A, M extends FunctionName<A>> extends C
52
52
  args?: ActorMethodParameters<A[M]>;
53
53
  onLoading?: (loading: boolean) => void;
54
54
  onError?: (error: Error | undefined) => void;
55
- onSuccess?: (data: CompiledResult<ActorMethodReturnType<A[M]>>) => void;
55
+ onSuccess?: (data: ActorMethodReturnType<A[M]>) => void;
56
+ onSuccessResult?: (result: CompiledResult<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.12.1",
3
+ "version": "1.13.0",
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",
@@ -47,5 +47,5 @@
47
47
  "react": ">=16.8",
48
48
  "zustand": "4.5"
49
49
  },
50
- "gitHead": "9118e38b6596835b6aef84e8e653d05454d031b7"
50
+ "gitHead": "2336c03931dda3c24bdb3c13946ec63a9b8b0e99"
51
51
  }