UNPKG

@snipsonian/observable-state

Version:

Observable-state snippets (redux-like)

141 lines (140 loc) 6.86 kB
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; }; import isSet from '@snipsonian/core/es/is/isSet'; import { AsyncStatus, } from './types'; export 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 === AsyncStatus.Success; if (!isAlreadyFetched) { return true; } if (!isSet(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 (!isSet(currentEntityData)) { return false; } return resetDataOnTriggerMode({ state }); } }