diff --git a/src/buildSelectors/index.ts b/src/buildSelectors/index.ts index 45d4cd6..217202b 100644 --- a/src/buildSelectors/index.ts +++ b/src/buildSelectors/index.ts @@ -2,12 +2,14 @@ import fp from 'lodash/fp'; import Updux from '..'; import { Dictionary, Selector } from '../types'; -function subSelectors([slice, subdux]: [string, Updux]): [ string, Selector ][] { +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) + fp.mapValues(selector => (state: any) => + (selector as any)(state[slice]) + )(selectors) ); } diff --git a/src/selectors.test.ts b/src/selectors.test.ts index 7d6ce8c..0a689ee 100644 --- a/src/selectors.test.ts +++ b/src/selectors.test.ts @@ -12,7 +12,7 @@ test('basic selectors', () => { selectors: { bogeys: ({ bogeys }: any) => bogeys, }, - } as any); + }); const state = { bogeys: { @@ -21,8 +21,6 @@ test('basic selectors', () => { } }; - console.log(updux.selectors); - expect( updux.selectors.bogeys(state) ).toEqual( { foo:1, bar :2 } ); expect( (updux.selectors.bogey(state) as any)('foo')).toEqual(1); diff --git a/src/types.ts b/src/types.ts index 891c705..ce75ac4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -98,6 +98,8 @@ export type UpduxConfig = { [type: string]: ActionCreator; }; + selectors?: Dictionary; + /** * Object mapping actions to the associated state mutation. * diff --git a/src/updux.ts b/src/updux.ts index fa6fa73..20f5180 100644 --- a/src/updux.ts +++ b/src/updux.ts @@ -46,20 +46,20 @@ export type Dux = Pick< >; export class Updux { - subduxes: Dictionary; + subduxes: Dictionary = {}; - private local_selectors: Dictionary> = {}; + private local_selectors: Dictionary> = {}; initial: S; groomMutations: (mutation: Mutation) => Mutation; - private localEffects: EffectEntry[] = []; + private localEffects: EffectEntry[] = []; - private localActions: Dictionary = {}; + private localActions: Dictionary = {}; - private localMutations: Dictionary< + private localMutations: Dictionary< Mutation | [Mutation, boolean | undefined] > = {}; @@ -69,9 +69,11 @@ export class Updux { 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; + Object.entries( fp.mapValues((value: UpduxConfig | Updux) => + fp.isPlainObject(value) ? new Updux(value as any) : value + )(fp.getOr({}, "subduxes", config))).forEach( + ([slice,sub]) => this.subduxes[slice] = sub as any + ); const actions = fp.getOr({}, "actions", config); Object.entries(actions).forEach(([type, payload]: [string, any]): any => @@ -104,7 +106,6 @@ export class Updux { } get actions(): Dictionary { - return buildActions([ ...(Object.entries(this.localActions) as any), ...(fp.flatten( @@ -116,7 +117,7 @@ export class Updux { ]); } - get upreducer(): Upreducer { + get upreducer(): Upreducer { return buildUpreducer(this.initial, this.mutations); } @@ -232,8 +233,8 @@ export class Updux { this.local_selectors[name] = selector; } - get selectors() { - return buildSelectors(this.local_selectors,this.subduxes); + get selectors() { + return buildSelectors(this.local_selectors, this.subduxes); } }