@helenejs/react
Version:
Real-time Web Apps for Node.js
55 lines • 1.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useMethodRefresh = void 0;
const react_1 = require("react");
const isFunction_1 = __importDefault(require("lodash/isFunction"));
const useMethodRefresh = ({ authenticated, caller, client, params, method, setError, setLoading, setResult, shouldCall, startLoading, methodOptions, defaultValue, deps, }) => {
return (0, react_1.useCallback)((callback) => {
if (!method)
return;
if (!shouldCall)
return;
if (!caller)
return;
if (authenticated && !client.authenticated) {
setLoading(false);
setResult(defaultValue);
return;
}
startLoading();
let successful = false;
caller
.call(client, method, params, methodOptions)
.then(_result => {
setResult(_result);
setError(undefined);
successful = true;
})
.catch(e => {
console.error(e);
setError(e);
setResult(undefined);
})
.finally(() => {
startLoading.cancel();
setLoading(false);
(0, isFunction_1.default)(callback) && callback();
});
}, [
client,
method,
params,
setResult,
setLoading,
setError,
client?.authenticated,
defaultValue,
...Object.values(methodOptions),
...deps,
]);
};
exports.useMethodRefresh = useMethodRefresh;
//# sourceMappingURL=use-method-refresh.js.map