@snipsonian/observable-state
Version:
Observable-state snippets (redux-like)
145 lines (144 loc) • 7.07 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.initAsyncEntityTriggers = void 0;
const isSet_1 = require("@snipsonian/core/cjs/is/isSet");
const types_1 = require("./types");
function initAsyncEntityTriggers({ asyncEntityActionCreators, getStore, }) {
return {
fetch(_a) {
var { asyncEntityKey, shouldFetch, refreshMode, resetDataOnTriggerMode, onSuccess: onSuccessInput, onError: onErrorInput } = _a, other = __rest(_a, ["asyncEntityKey", "shouldFetch", "refreshMode", "resetDataOnTriggerMode", "onSuccess", "onError"]);
if (!shouldFetchEntity({ asyncEntityKey, shouldFetch, refreshMode })) {
return Promise.resolve({
wasTriggered: false,
asyncResult: null,
});
}
const resetDataOnTrigger = shouldResetEntityOnTrigger({ asyncEntityKey, resetDataOnTriggerMode });
return new Promise((resolve, reject) => {
getStore().dispatch(asyncEntityActionCreators.fetchAsyncEntityAction(Object.assign(Object.assign({ asyncEntityKey,
resetDataOnTrigger }, other), { onSuccess: (onSuccessProps) => {
resolve({
wasTriggered: true,
asyncResult: onSuccessProps.apiResult,
});
if (onSuccessInput) {
onSuccessInput(onSuccessProps);
}
}, onError: (onErrorProps) => {
reject(onErrorProps.error);
if (onErrorInput) {
onErrorInput(onErrorProps);
}
} })));
});
},
create(_a) {
var { asyncEntityKey, updateDataOnSuccess, markAsFetchedOnSuccess, onSuccess: onSuccessInput, onError: onErrorInput } = _a, other = __rest(_a, ["asyncEntityKey", "updateDataOnSuccess", "markAsFetchedOnSuccess", "onSuccess", "onError"]);
return new Promise((resolve, reject) => {
getStore().dispatch(asyncEntityActionCreators.createAsyncEntityAction(Object.assign(Object.assign({ asyncEntityKey,
updateDataOnSuccess,
markAsFetchedOnSuccess }, other), { onSuccess: (onSuccessProps) => {
resolve({
wasTriggered: true,
asyncResult: onSuccessProps.apiResult,
});
if (onSuccessInput) {
onSuccessInput(onSuccessProps);
}
}, onError: (onErrorProps) => {
reject(onErrorProps.error);
if (onErrorInput) {
onErrorInput(onErrorProps);
}
} })));
});
},
update(_a) {
var { asyncEntityKey, updateDataOnSuccess, onSuccess: onSuccessInput, onError: onErrorInput } = _a, other = __rest(_a, ["asyncEntityKey", "updateDataOnSuccess", "onSuccess", "onError"]);
return new Promise((resolve, reject) => {
getStore().dispatch(asyncEntityActionCreators.updateAsyncEntityAction(Object.assign(Object.assign({ asyncEntityKey,
updateDataOnSuccess }, other), { onSuccess: (onSuccessProps) => {
resolve({
wasTriggered: true,
asyncResult: onSuccessProps.apiResult,
});
if (onSuccessInput) {
onSuccessInput(onSuccessProps);
}
}, onError: (onErrorProps) => {
reject(onErrorProps.error);
if (onErrorInput) {
onErrorInput(onErrorProps);
}
} })));
});
},
remove(_a) {
var { asyncEntityKey, markAsNotFetchedOnSuccess, onSuccess: onSuccessInput, onError: onErrorInput } = _a, other = __rest(_a, ["asyncEntityKey", "markAsNotFetchedOnSuccess", "onSuccess", "onError"]);
return new Promise((resolve, reject) => {
getStore().dispatch(asyncEntityActionCreators.removeAsyncEntityAction(Object.assign(Object.assign({ asyncEntityKey,
markAsNotFetchedOnSuccess }, other), { onSuccess: (onSuccessProps) => {
resolve({
wasTriggered: true,
asyncResult: onSuccessProps.apiResult,
});
if (onSuccessInput) {
onSuccessInput(onSuccessProps);
}
}, onError: (onErrorProps) => {
reject(onErrorProps.error);
if (onErrorInput) {
onErrorInput(onErrorProps);
}
} })));
});
},
};
function shouldFetchEntity({ asyncEntityKey, shouldFetch, refreshMode, }) {
const state = getStore().getState();
if (shouldFetch && !shouldFetch({ state })) {
return false;
}
const asyncEntityFetchOperation = asyncEntityActionCreators.getAsyncEntity(state, asyncEntityKey).fetch;
const isAlreadyFetched = asyncEntityFetchOperation.status === types_1.AsyncStatus.Success;
if (!isAlreadyFetched) {
return true;
}
if (!(0, isSet_1.default)(refreshMode)) {
return true;
}
if (refreshMode === 'always') {
return true;
}
if (refreshMode === 'never') {
return false;
}
return refreshMode({ state });
}
function shouldResetEntityOnTrigger({ asyncEntityKey, resetDataOnTriggerMode = 'always', }) {
if (resetDataOnTriggerMode === 'always') {
return true;
}
if (resetDataOnTriggerMode === 'never') {
return false;
}
const state = getStore().getState();
const currentEntityData = asyncEntityActionCreators.getAsyncEntity(state, asyncEntityKey).data;
if (!(0, isSet_1.default)(currentEntityData)) {
return false;
}
return resetDataOnTriggerMode({ state });
}
}
exports.initAsyncEntityTriggers = initAsyncEntityTriggers;