wip
This commit is contained in:
parent
1825e8d269
commit
f744616cb2
@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -98,6 +98,8 @@ export type UpduxConfig<S = any> = {
|
||||
[type: string]: ActionCreator;
|
||||
};
|
||||
|
||||
selectors?: Dictionary<Selector>;
|
||||
|
||||
/**
|
||||
* Object mapping actions to the associated state mutation.
|
||||
*
|
||||
|
25
src/updux.ts
25
src/updux.ts
@ -46,20 +46,20 @@ export type Dux<S> = Pick<
|
||||
>;
|
||||
|
||||
export class Updux<S = any> {
|
||||
subduxes: Dictionary<Updux>;
|
||||
subduxes: Dictionary<Updux> = {};
|
||||
|
||||
private local_selectors: Dictionary<Selector<S>> = {};
|
||||
private local_selectors: Dictionary<Selector<S>> = {};
|
||||
|
||||
initial: S;
|
||||
|
||||
groomMutations: (mutation: Mutation<S>) => Mutation<S>;
|
||||
|
||||
|
||||
private localEffects: EffectEntry<S>[] = [];
|
||||
private localEffects: EffectEntry<S>[] = [];
|
||||
|
||||
private localActions: Dictionary<ActionCreator> = {};
|
||||
private localActions: Dictionary<ActionCreator> = {};
|
||||
|
||||
private localMutations: Dictionary<
|
||||
private localMutations: Dictionary<
|
||||
Mutation<S> | [Mutation<S>, boolean | undefined]
|
||||
> = {};
|
||||
|
||||
@ -69,9 +69,11 @@ export class Updux<S = any> {
|
||||
const selectors = fp.getOr( {}, 'selectors', config ) as Dictionary<Selector>;
|
||||
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<Updux>;
|
||||
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<S = any> {
|
||||
}
|
||||
|
||||
get actions(): Dictionary<ActionCreator> {
|
||||
|
||||
return buildActions([
|
||||
...(Object.entries(this.localActions) as any),
|
||||
...(fp.flatten(
|
||||
@ -116,7 +117,7 @@ export class Updux<S = any> {
|
||||
]);
|
||||
}
|
||||
|
||||
get upreducer(): Upreducer<S> {
|
||||
get upreducer(): Upreducer<S> {
|
||||
return buildUpreducer(this.initial, this.mutations);
|
||||
}
|
||||
|
||||
@ -232,8 +233,8 @@ export class Updux<S = any> {
|
||||
this.local_selectors[name] = selector;
|
||||
}
|
||||
|
||||
get selectors() {
|
||||
return buildSelectors(this.local_selectors,this.subduxes);
|
||||
get selectors() {
|
||||
return buildSelectors(this.local_selectors, this.subduxes);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user