@aitianyu.cn/tianyu-store
Version:
tianyu storage for nodejs.
72 lines • 4.11 kB
JavaScript
;
/** @format */
Object.defineProperty(exports, "__esModule", { value: true });
exports.dispatching = void 0;
const Message_1 = require("../../infra/Message");
const Action_1 = require("../../types/Action");
const StoreHandler_1 = require("../../types/StoreHandler");
const Selecting_1 = require("./Selecting");
const InstanceProcessor_1 = require("./InstanceProcessor");
const ActionProcessor_1 = require("./ActionProcessor");
async function dispatching(executor, manager, actions,
/* istanbul ignore next */
notRedoUndo = false) {
const ranActions = [];
const _entity = (0, InstanceProcessor_1.verifyActionInstances)(actions);
for (const rawAction of actions) {
const actionImpl = manager.getAction(rawAction.action, !!rawAction.template, rawAction.instanceId);
// due to in the internal case, there will execute action by action name only
// reget the action instance to ensure the action valid
const action = actionImpl(rawAction.instanceId, rawAction.params);
if (!(0, InstanceProcessor_1.verifyInstanceIdMatchStoreTypeOrParentStoreType)(action.storeType, action.instanceId)) {
// throw an error when try to use a different store type instance to run action
throw new Error(Message_1.MessageBundle.getText("DISPATCHING_ACTION_INSTANCE_NOT_MATCH", action.storeType, action.instanceId.storeType, action.action));
}
ranActions.push(action);
// execute external processing
if (actionImpl.external) {
await actionImpl.external(executor.getExternalRegister(action.instanceId, action.actionType === Action_1.ActionType.CREATE));
}
const iterator = actionImpl.handler(action);
const fnGetNextValue = async (handleResult) => {
switch (handleResult.type) {
case StoreHandler_1.StoreHandleType.ACTION:
const subAction = handleResult;
(0, InstanceProcessor_1.verifyInstanceSameAncestor)(action.instanceId, subAction.action.instanceId);
const subRanActions = await dispatching(executor, manager, [subAction.action], notRedoUndo);
ranActions.push(...subRanActions);
return subRanActions;
case StoreHandler_1.StoreHandleType.SELECTOR:
const selectedValue = handleResult.shouldThrow
? (0, Selecting_1.doSelectingWithThrow)(manager, handleResult.selector, false)
: (0, Selecting_1.doSelecting)(manager, handleResult.selector, false);
return selectedValue;
case StoreHandler_1.StoreHandleType.EXTERNAL_OBJ:
const externalGetter = handleResult.handler(executor.getExternalRegister(action.instanceId));
return externalGetter;
/* istanbul ignore next */
default:
throw new Error(Message_1.MessageBundle.getText("DISPATCHING_HANDLER_WITH_UNKNOWN_RESULT"));
}
};
const fnGeneratorRunner = async (value) => {
const result = await iterator.next(value);
if (result.done) {
// this is for action done
if (actionImpl.reducer) {
const newState = actionImpl.reducer(executor.getState((0, InstanceProcessor_1.getStoreTypeMatchedInstanceId)(action.storeType, action.instanceId), action.actionType === Action_1.ActionType.CREATE), result.value);
ActionProcessor_1.ActionProcessorMap[action.actionType](executor, manager, action, newState, notRedoUndo);
}
}
else {
const handleResult = result.value;
const nextValue = await fnGetNextValue(handleResult);
await fnGeneratorRunner(nextValue);
}
};
await fnGeneratorRunner(action);
}
return ranActions;
}
exports.dispatching = dispatching;
//# sourceMappingURL=Dispatching.js.map