addSelector
This commit is contained in:
parent
0870767994
commit
1a653fac5b
11
src/Updux.js
11
src/Updux.js
@ -3,6 +3,7 @@ import u from '@yanick/updeep';
|
|||||||
|
|
||||||
import { buildInitial } from './buildInitial/index.js';
|
import { buildInitial } from './buildInitial/index.js';
|
||||||
import { buildActions } from './buildActions/index.js';
|
import { buildActions } from './buildActions/index.js';
|
||||||
|
import { buildSelectors } from './buildSelectors/index.js';
|
||||||
import { action } from './actions.js';
|
import { action } from './actions.js';
|
||||||
|
|
||||||
|
|
||||||
@ -17,17 +18,18 @@ export class Updux {
|
|||||||
#initial = {};
|
#initial = {};
|
||||||
#subduxes = {};
|
#subduxes = {};
|
||||||
#actions = {};
|
#actions = {};
|
||||||
|
#selectors = {};
|
||||||
|
|
||||||
constructor(config) {
|
constructor(config) {
|
||||||
this.#initial = config.initial ?? {};
|
this.#initial = config.initial ?? {};
|
||||||
|
|
||||||
this.#subduxes = config.subduxes ?? {};
|
this.#subduxes = config.subduxes ?? {};
|
||||||
|
|
||||||
this.#actions = config.actions ?? {};
|
this.#actions = config.actions ?? {};
|
||||||
|
this.#selectors = config.selectors ?? {};
|
||||||
}
|
}
|
||||||
|
|
||||||
#memoInitial = moize( buildInitial );
|
#memoInitial = moize( buildInitial );
|
||||||
#memoActions = moize(buildActions);
|
#memoActions = moize(buildActions);
|
||||||
|
#memoSelectors = moize(buildSelectors);
|
||||||
|
|
||||||
get initial() {
|
get initial() {
|
||||||
return this.#memoInitial(this.#initial,this.#subduxes);
|
return this.#memoInitial(this.#initial,this.#subduxes);
|
||||||
@ -38,7 +40,7 @@ export class Updux {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get selectors() {
|
get selectors() {
|
||||||
return {};
|
return this.#memoSelectors(this.#selectors,this.#subduxes);
|
||||||
}
|
}
|
||||||
|
|
||||||
addAction(type, payloadFunc) {
|
addAction(type, payloadFunc) {
|
||||||
@ -50,6 +52,7 @@ export class Updux {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addSelector(name, func) {
|
addSelector(name, func) {
|
||||||
return;
|
this.#selectors[name] = func;
|
||||||
|
return func;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
src/buildSelectors/index.js
Normal file
12
src/buildSelectors/index.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { map, mapValues, merge } from 'lodash-es';
|
||||||
|
|
||||||
|
export function buildSelectors(localSelectors, subduxes) {
|
||||||
|
const subSelectors = map(subduxes, ({ selectors }, slice) => {
|
||||||
|
if (!selectors) return {};
|
||||||
|
|
||||||
|
return mapValues(selectors, (func) => (state) => func(state[slice]));
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(subSelectors);
|
||||||
|
return merge({}, ...subSelectors, localSelectors);
|
||||||
|
}
|
@ -1,27 +0,0 @@
|
|||||||
import fp from 'lodash/fp';
|
|
||||||
import Updux from '..';
|
|
||||||
import { Dictionary, Selector } from '../types';
|
|
||||||
|
|
||||||
function subSelectors([slice, selectors]: [string, Function]): [string, Selector][] {
|
|
||||||
if (!selectors) return [];
|
|
||||||
|
|
||||||
return Object.entries(
|
|
||||||
fp.mapValues(selector => (state: any) =>
|
|
||||||
(selector as any)(state[slice])
|
|
||||||
)(selectors as any)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function buildSelectors(
|
|
||||||
localSelectors: Dictionary<Selector> = {},
|
|
||||||
coduxes: Dictionary<Selector>[] = [],
|
|
||||||
subduxes: Dictionary<Selector> = {}
|
|
||||||
) {
|
|
||||||
return Object.fromEntries(
|
|
||||||
[
|
|
||||||
Object.entries(subduxes).flatMap(subSelectors),
|
|
||||||
Object.entries(coduxes),
|
|
||||||
Object.entries(localSelectors),
|
|
||||||
].flat()
|
|
||||||
);
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user