2022-08-26 14:37:17 +00:00
|
|
|
import R from 'remeda';
|
|
|
|
|
|
|
|
export function buildSelectors(
|
|
|
|
localSelectors,
|
2022-08-30 14:09:24 +00:00
|
|
|
findSelectors = {},
|
2022-08-28 16:47:24 +00:00
|
|
|
subduxes = {},
|
2022-08-26 14:37:17 +00:00
|
|
|
) {
|
2022-08-28 16:47:24 +00:00
|
|
|
const subSelectors = Object.entries(subduxes).map(
|
|
|
|
([slice, { selectors }]) => {
|
|
|
|
if (!selectors) return {};
|
|
|
|
if (slice === '*') return {};
|
2022-08-26 14:37:17 +00:00
|
|
|
|
2022-08-28 16:47:24 +00:00
|
|
|
return R.mapValues(
|
|
|
|
selectors,
|
|
|
|
(func) => (state) => func(state[slice]),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2022-08-26 14:37:17 +00:00
|
|
|
|
|
|
|
let splat = {};
|
|
|
|
|
2022-08-30 14:09:24 +00:00
|
|
|
for (const name in findSelectors) {
|
2022-08-26 14:37:17 +00:00
|
|
|
splat[name] =
|
2022-08-30 14:09:24 +00:00
|
|
|
(mainState) =>
|
2022-08-26 14:37:17 +00:00
|
|
|
(...args) => {
|
2022-08-30 14:09:24 +00:00
|
|
|
const state = findSelectors[name](mainState)(...args);
|
2022-08-26 14:37:17 +00:00
|
|
|
|
2022-08-30 14:09:24 +00:00
|
|
|
return R.merge(
|
|
|
|
{ state },
|
|
|
|
R.mapValues(
|
2022-09-06 15:35:06 +00:00
|
|
|
subduxes['*']?.selectors ?? {},
|
2022-08-30 14:09:24 +00:00
|
|
|
(selector) =>
|
|
|
|
(...args) => {
|
|
|
|
let value = selector(state);
|
|
|
|
if (typeof value !== 'function') return value;
|
|
|
|
return value(...args);
|
|
|
|
},
|
2022-08-28 16:47:24 +00:00
|
|
|
),
|
2022-08-26 14:37:17 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
2022-08-28 16:47:24 +00:00
|
|
|
return R.mergeAll([...subSelectors, localSelectors, splat]);
|
2022-08-26 14:37:17 +00:00
|
|
|
}
|