This commit is contained in:
Yanick Champoux 2020-01-03 17:27:11 -05:00
parent 1825e8d269
commit f744616cb2
4 changed files with 20 additions and 17 deletions

View File

@ -2,12 +2,14 @@ import fp from 'lodash/fp';
import Updux from '..'; import Updux from '..';
import { Dictionary, Selector } from '../types'; 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; const selectors = subdux.selectors;
if (!selectors) return []; if (!selectors) return [];
return Object.entries( 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)
); );
} }

View File

@ -12,7 +12,7 @@ test('basic selectors', () => {
selectors: { selectors: {
bogeys: ({ bogeys }: any) => bogeys, bogeys: ({ bogeys }: any) => bogeys,
}, },
} as any); });
const state = { const state = {
bogeys: { 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.bogeys(state) ).toEqual( { foo:1, bar :2 } );
expect( (updux.selectors.bogey(state) as any)('foo')).toEqual(1); expect( (updux.selectors.bogey(state) as any)('foo')).toEqual(1);

View File

@ -98,6 +98,8 @@ export type UpduxConfig<S = any> = {
[type: string]: ActionCreator; [type: string]: ActionCreator;
}; };
selectors?: Dictionary<Selector>;
/** /**
* Object mapping actions to the associated state mutation. * Object mapping actions to the associated state mutation.
* *

View File

@ -46,7 +46,7 @@ export type Dux<S> = Pick<
>; >;
export class Updux<S = any> { export class Updux<S = any> {
subduxes: Dictionary<Updux>; subduxes: Dictionary<Updux> = {};
private local_selectors: Dictionary<Selector<S>> = {}; private local_selectors: Dictionary<Selector<S>> = {};
@ -69,9 +69,11 @@ export class Updux<S = any> {
const selectors = fp.getOr( {}, 'selectors', config ) as Dictionary<Selector>; const selectors = fp.getOr( {}, 'selectors', config ) as Dictionary<Selector>;
Object.entries(selectors).forEach( ([name,sel]: [string,Function]) => this.addSelector(name,sel as Selector) ); Object.entries(selectors).forEach( ([name,sel]: [string,Function]) => this.addSelector(name,sel as Selector) );
this.subduxes = fp.mapValues((value: UpduxConfig | Updux) => Object.entries( fp.mapValues((value: UpduxConfig | Updux) =>
fp.isPlainObject(value) ? new Updux(value) : value fp.isPlainObject(value) ? new Updux(value as any) : value
)(fp.getOr({}, "subduxes", config)) as Dictionary<Updux>; )(fp.getOr({}, "subduxes", config))).forEach(
([slice,sub]) => this.subduxes[slice] = sub as any
);
const actions = fp.getOr({}, "actions", config); const actions = fp.getOr({}, "actions", config);
Object.entries(actions).forEach(([type, payload]: [string, any]): any => Object.entries(actions).forEach(([type, payload]: [string, any]): any =>
@ -104,7 +106,6 @@ export class Updux<S = any> {
} }
get actions(): Dictionary<ActionCreator> { get actions(): Dictionary<ActionCreator> {
return buildActions([ return buildActions([
...(Object.entries(this.localActions) as any), ...(Object.entries(this.localActions) as any),
...(fp.flatten( ...(fp.flatten(
@ -233,7 +234,7 @@ export class Updux<S = any> {
} }
get selectors() { get selectors() {
return buildSelectors(this.local_selectors,this.subduxes); return buildSelectors(this.local_selectors, this.subduxes);
} }
} }