diff --git a/src/Updux.js b/src/Updux.js index d9dc51d..9ea46e2 100644 --- a/src/Updux.js +++ b/src/Updux.js @@ -3,6 +3,7 @@ import u from '@yanick/updeep'; import { buildInitial } from './buildInitial/index.js'; import { buildActions } from './buildActions/index.js'; +import { buildSelectors } from './buildSelectors/index.js'; import { action } from './actions.js'; @@ -17,17 +18,18 @@ export class Updux { #initial = {}; #subduxes = {}; #actions = {}; + #selectors = {}; constructor(config) { this.#initial = config.initial ?? {}; - this.#subduxes = config.subduxes ?? {}; - this.#actions = config.actions ?? {}; + this.#selectors = config.selectors ?? {}; } #memoInitial = moize( buildInitial ); #memoActions = moize(buildActions); + #memoSelectors = moize(buildSelectors); get initial() { return this.#memoInitial(this.#initial,this.#subduxes); @@ -38,7 +40,7 @@ export class Updux { } get selectors() { - return {}; + return this.#memoSelectors(this.#selectors,this.#subduxes); } addAction(type, payloadFunc) { @@ -50,6 +52,7 @@ export class Updux { } addSelector(name, func) { - return; + this.#selectors[name] = func; + return func; } } diff --git a/src/buildSelectors/index.js b/src/buildSelectors/index.js new file mode 100644 index 0000000..9e03942 --- /dev/null +++ b/src/buildSelectors/index.js @@ -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); +} diff --git a/src/buildSelectors/index.ts b/src/buildSelectors/index.ts deleted file mode 100644 index 1c8ce3a..0000000 --- a/src/buildSelectors/index.ts +++ /dev/null @@ -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 = {}, - coduxes: Dictionary[] = [], - subduxes: Dictionary = {} -) { - return Object.fromEntries( - [ - Object.entries(subduxes).flatMap(subSelectors), - Object.entries(coduxes), - Object.entries(localSelectors), - ].flat() - ); -}