35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
|
import { createAction } from '@reduxjs/toolkit';
|
||
|
import * as R from 'remeda';
|
||
|
import { withPayload } from './actions.js';
|
||
|
function resolveActions(configActions) {
|
||
|
return R.mapValues(configActions, (prepare, type) => {
|
||
|
if (typeof prepare === 'function' && prepare.type)
|
||
|
return prepare;
|
||
|
return createAction(type, withPayload(prepare));
|
||
|
});
|
||
|
}
|
||
|
export function buildActions(localActions, subduxes) {
|
||
|
localActions = resolveActions(localActions);
|
||
|
let actions = {};
|
||
|
for (const slice in subduxes) {
|
||
|
const subdux = subduxes[slice].actions;
|
||
|
if (!subdux)
|
||
|
continue;
|
||
|
for (const a in subdux) {
|
||
|
if (actions[a] && subduxes[actions[a]].actions[a] !== subdux[a]) {
|
||
|
throw new Error(`action '${a}' defined both in subduxes '${actions[a]}' and '${slice}'`);
|
||
|
}
|
||
|
actions[a] = slice;
|
||
|
}
|
||
|
}
|
||
|
for (const a in localActions) {
|
||
|
if (actions[a]) {
|
||
|
throw new Error(`action '${a}' defined both locally and in subdux '${actions[a]}'`);
|
||
|
}
|
||
|
}
|
||
|
return R.mergeAll([
|
||
|
localActions,
|
||
|
...Object.values(subduxes).map(R.pathOr(['actions'], {})),
|
||
|
]);
|
||
|
}
|