From 1825e8d2690bab00a9fed45d4bfd0d18786e6b83 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 3 Jan 2020 12:00:24 -0500 Subject: [PATCH] selectors added to class --- src/buildSelectors/index.ts | 24 ++++++++++++++++++++++++ src/selectors.test.ts | 4 +++- src/types.ts | 2 ++ src/updux.ts | 18 +++++++++++++++++- 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/buildSelectors/index.ts diff --git a/src/buildSelectors/index.ts b/src/buildSelectors/index.ts new file mode 100644 index 0000000..45d4cd6 --- /dev/null +++ b/src/buildSelectors/index.ts @@ -0,0 +1,24 @@ +import fp from 'lodash/fp'; +import Updux from '..'; +import { Dictionary, Selector } from '../types'; + +function subSelectors([slice, subdux]: [string, Updux]): [ string, Selector ][] { + const selectors = subdux.selectors; + if (!selectors) return []; + + return Object.entries( + fp.mapValues(selector => (state:any) => (selector as any)(state[slice]))(selectors) + ); +} + +export default function buildSelectors( + localSelectors: Dictionary = {}, + subduxes: Dictionary = {} +) { + return Object.fromEntries( + [ + Object.entries(subduxes).flatMap(subSelectors), + Object.entries(localSelectors), + ].flat() + ); +} diff --git a/src/selectors.test.ts b/src/selectors.test.ts index 524e801..7d6ce8c 100644 --- a/src/selectors.test.ts +++ b/src/selectors.test.ts @@ -21,7 +21,9 @@ test('basic selectors', () => { } }; + console.log(updux.selectors); + expect( updux.selectors.bogeys(state) ).toEqual( { foo:1, bar :2 } ); - expect( updux.selectors.bogey(state)('foo')).toEqual(1); + expect( (updux.selectors.bogey(state) as any)('foo')).toEqual(1); }); diff --git a/src/types.ts b/src/types.ts index ca0da78..891c705 100644 --- a/src/types.ts +++ b/src/types.ts @@ -202,3 +202,5 @@ export interface UpduxMiddlewareAPI { export type UpduxMiddleware = ( api: UpduxMiddlewareAPI ) => (next: UpduxDispatch) => (action: Action) => any; + +export type Selector = (state:S) => unknown; diff --git a/src/updux.ts b/src/updux.ts index 2df48c3..fa6fa73 100644 --- a/src/updux.ts +++ b/src/updux.ts @@ -18,10 +18,12 @@ import { UpduxDispatch, UpduxMiddleware, MutationEntry, - EffectEntry + EffectEntry, + Selector } from "./types"; import { Middleware, Store, PreloadedState } from "redux"; +import buildSelectors from "./buildSelectors"; export { actionCreator } from "./buildActions"; type StoreWithDispatchActions< @@ -46,10 +48,13 @@ export type Dux = Pick< export class Updux { subduxes: Dictionary; + private local_selectors: Dictionary> = {}; + initial: S; groomMutations: (mutation: Mutation) => Mutation; + private localEffects: EffectEntry[] = []; private localActions: Dictionary = {}; @@ -61,6 +66,9 @@ export class Updux { constructor(config: UpduxConfig = {}) { this.groomMutations = config.groomMutations || ((x: Mutation) => x); + const selectors = fp.getOr( {}, 'selectors', config ) as Dictionary; + Object.entries(selectors).forEach( ([name,sel]: [string,Function]) => this.addSelector(name,sel as Selector) ); + this.subduxes = fp.mapValues((value: UpduxConfig | Updux) => fp.isPlainObject(value) ? new Updux(value) : value )(fp.getOr({}, "subduxes", config)) as Dictionary; @@ -219,6 +227,14 @@ export class Updux { ].filter(x => x) ); } + + addSelector( name: string, selector: Selector) { + this.local_selectors[name] = selector; + } + + get selectors() { + return buildSelectors(this.local_selectors,this.subduxes); + } } export default Updux;