@snipsonian/observable-state
Version:
Observable-state snippets (redux-like)
114 lines (113 loc) • 7.96 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;
};
import { AsyncOperation, } from './types';
import { createAsyncEntityInitialState } from './createAsyncEntityInitialState';
import { ASYNC_OPERATION_2_ASYNC_ENTITY_UPDATERS } from './asyncEntityUpdaters';
import { initAsyncEntityActionCreators } from './asyncEntityActionCreators';
import { initAsyncEntityTriggers } from './asyncEntityTriggers';
export default function initAsyncEntitiesManager({ entitiesStateField = 'entities', getStore, }) {
const asyncEntityConfigs = {};
let entitiesInitialState = null;
const asyncEntityActionCreators = initAsyncEntityActionCreators({
entitiesStateField,
getEntitiesInitialState,
});
const asyncEntityTriggers = initAsyncEntityTriggers({
asyncEntityActionCreators,
getStore,
});
return {
registerEntity({ asyncEntityKey, operations, initialData, includeUpdaters = false, notificationsToTrigger, nrOfParentNotificationLevelsToTrigger, }) {
const initialState = createAsyncEntityInitialState({ operations, data: initialData });
asyncEntityConfigs[asyncEntityKey] = {
operations,
initialState,
};
return {
select: (state = getStore().getState()) => asyncEntityActionCreators.getAsyncEntity(state, asyncEntityKey),
updaters: includeUpdaters ? initOperationUpdaters() : undefined,
async: initAsyncTriggers(),
};
function updateThisEntityInState(entityUpdater, options = {}) {
return asyncEntityActionCreators.updateAsyncEntityInState({
asyncEntityKey,
entityUpdater,
options: {
notificationsToTrigger: options.notificationsToTrigger || notificationsToTrigger,
nrOfParentNotificationLevelsToTrigger: options.nrOfParentNotificationLevelsToTrigger || nrOfParentNotificationLevelsToTrigger,
},
setState: getStore().setState,
});
}
function initOperationUpdaters() {
return operations.reduce((accumulator, operation) => {
const operationUpdaters = ASYNC_OPERATION_2_ASYNC_ENTITY_UPDATERS[operation];
accumulator[operation] = {
trigger: (options) => updateThisEntityInState((entity) => operationUpdaters.trigger(entity, initialState.data), options),
triggerWithoutDataReset: (options) => updateThisEntityInState((entity) => operationUpdaters.triggerWithoutDataReset(entity), options),
succeeded: (data, options) => updateThisEntityInState((entity) => operationUpdaters.succeeded(entity, data), options),
succeededWithoutDataSet: (options) => updateThisEntityInState((entity) => operationUpdaters.succeededWithoutDataSet(entity), options),
failed: (error, options) => updateThisEntityInState((entity) => operationUpdaters.failed(entity, error), options),
cancel: (options) => updateThisEntityInState((entity) => operationUpdaters.cancel(entity), options),
reset: (options) => updateThisEntityInState((entity) => operationUpdaters.reset(entity, initialState.data), options),
resetWithoutDataReset: (options) => updateThisEntityInState((entity) => operationUpdaters.resetWithoutDataReset(entity), options),
};
return accumulator;
}, {});
}
function initAsyncTriggers() {
return operations.reduce((accumulator, operation) => {
if (operation === AsyncOperation.fetch) {
accumulator.fetch = (_a) => {
var { notificationsToTrigger: specificNotificationsToTrigger, nrOfParentNotificationLevelsToTrigger: specificNrOfParentNotificationLevelsToTrigger } = _a, other = __rest(_a, ["notificationsToTrigger", "nrOfParentNotificationLevelsToTrigger"]);
return asyncEntityTriggers.fetch(Object.assign({ asyncEntityKey, notificationsToTrigger: specificNotificationsToTrigger || notificationsToTrigger, nrOfParentNotificationLevelsToTrigger: specificNrOfParentNotificationLevelsToTrigger || nrOfParentNotificationLevelsToTrigger }, other));
};
}
else if (operation === AsyncOperation.create) {
accumulator.create = (_a) => {
var { notificationsToTrigger: specificNotificationsToTrigger, nrOfParentNotificationLevelsToTrigger: specificNrOfParentNotificationLevelsToTrigger } = _a, other = __rest(_a, ["notificationsToTrigger", "nrOfParentNotificationLevelsToTrigger"]);
return asyncEntityTriggers.create(Object.assign({ asyncEntityKey, notificationsToTrigger: specificNotificationsToTrigger || notificationsToTrigger, nrOfParentNotificationLevelsToTrigger: specificNrOfParentNotificationLevelsToTrigger || nrOfParentNotificationLevelsToTrigger }, other));
};
}
else if (operation === AsyncOperation.update) {
accumulator.update = (_a) => {
var { notificationsToTrigger: specificNotificationsToTrigger, nrOfParentNotificationLevelsToTrigger: specificNrOfParentNotificationLevelsToTrigger } = _a, other = __rest(_a, ["notificationsToTrigger", "nrOfParentNotificationLevelsToTrigger"]);
return asyncEntityTriggers.update(Object.assign({ asyncEntityKey, notificationsToTrigger: specificNotificationsToTrigger || notificationsToTrigger, nrOfParentNotificationLevelsToTrigger: specificNrOfParentNotificationLevelsToTrigger || nrOfParentNotificationLevelsToTrigger }, other));
};
}
else if (operation === AsyncOperation.remove) {
accumulator.remove = (_a) => {
var { notificationsToTrigger: specificNotificationsToTrigger, nrOfParentNotificationLevelsToTrigger: specificNrOfParentNotificationLevelsToTrigger } = _a, other = __rest(_a, ["notificationsToTrigger", "nrOfParentNotificationLevelsToTrigger"]);
return asyncEntityTriggers.remove(Object.assign({ asyncEntityKey, notificationsToTrigger: specificNotificationsToTrigger || notificationsToTrigger, nrOfParentNotificationLevelsToTrigger: specificNrOfParentNotificationLevelsToTrigger || nrOfParentNotificationLevelsToTrigger }, other));
};
}
return accumulator;
}, {});
}
},
getAsyncEntityConfig({ asyncEntityKey, }) {
return asyncEntityConfigs[asyncEntityKey];
},
getEntitiesInitialState,
};
function getEntitiesInitialState() {
if (!entitiesInitialState) {
entitiesInitialState = Object.keys(asyncEntityConfigs)
.reduce((accumulator, asyncEntityKey) => {
const asyncEntityKeyConfig = asyncEntityConfigs[asyncEntityKey];
accumulator[asyncEntityKey] = asyncEntityKeyConfig.initialState;
return accumulator;
}, {});
}
return entitiesInitialState;
}
}