46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import { createAction } from '@reduxjs/toolkit';
|
|
import * as R from 'remeda';
|
|
import { withPayload } from './actions.js';
|
|
|
|
function resolveActions(configActions) {
|
|
return R.mapValues(configActions, (prepare, type: string) => {
|
|
if (typeof prepare === 'function' && prepare.type) return prepare;
|
|
|
|
return createAction(type, withPayload(prepare));
|
|
});
|
|
}
|
|
|
|
export function buildActions(localActions, subduxes) {
|
|
localActions = resolveActions(localActions);
|
|
|
|
let actions: Record<string, string> = {};
|
|
|
|
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<any, any>(['actions'], {})),
|
|
]) as any;
|
|
}
|