44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
import R from 'remeda';
|
|
|
|
export function buildSelectors(
|
|
localSelectors,
|
|
findSelectors = {},
|
|
subduxes = {},
|
|
) {
|
|
const subSelectors = Object.entries(subduxes).map(
|
|
([slice, { selectors }]) => {
|
|
if (!selectors) return {};
|
|
if (slice === '*') return {};
|
|
|
|
return R.mapValues(
|
|
selectors,
|
|
(func) => (state) => func(state[slice]),
|
|
);
|
|
},
|
|
);
|
|
|
|
let splat = {};
|
|
|
|
for (const name in findSelectors) {
|
|
splat[name] =
|
|
(mainState) =>
|
|
(...args) => {
|
|
const state = findSelectors[name](mainState)(...args);
|
|
|
|
return R.merge(
|
|
{ state },
|
|
R.mapValues(
|
|
subduxes['*']?.selectors ?? {},
|
|
(selector) =>
|
|
(...args) => {
|
|
let value = selector(state);
|
|
if (typeof value !== 'function') return value;
|
|
return value(...args);
|
|
},
|
|
),
|
|
);
|
|
};
|
|
}
|
|
return R.mergeAll([...subSelectors, localSelectors, splat]);
|
|
}
|