selectors added to class
This commit is contained in:
parent
499e987219
commit
1825e8d269
24
src/buildSelectors/index.ts
Normal file
24
src/buildSelectors/index.ts
Normal file
@ -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<Selector> = {},
|
||||
subduxes: Dictionary<Updux> = {}
|
||||
) {
|
||||
return Object.fromEntries(
|
||||
[
|
||||
Object.entries(subduxes).flatMap(subSelectors),
|
||||
Object.entries(localSelectors),
|
||||
].flat()
|
||||
);
|
||||
}
|
@ -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);
|
||||
|
||||
});
|
||||
|
@ -202,3 +202,5 @@ export interface UpduxMiddlewareAPI<S> {
|
||||
export type UpduxMiddleware<S = any> = (
|
||||
api: UpduxMiddlewareAPI<S>
|
||||
) => (next: UpduxDispatch) => (action: Action) => any;
|
||||
|
||||
export type Selector<S = any> = (state:S) => unknown;
|
||||
|
18
src/updux.ts
18
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<S> = Pick<
|
||||
export class Updux<S = any> {
|
||||
subduxes: Dictionary<Updux>;
|
||||
|
||||
private local_selectors: Dictionary<Selector<S>> = {};
|
||||
|
||||
initial: S;
|
||||
|
||||
groomMutations: (mutation: Mutation<S>) => Mutation<S>;
|
||||
|
||||
|
||||
private localEffects: EffectEntry<S>[] = [];
|
||||
|
||||
private localActions: Dictionary<ActionCreator> = {};
|
||||
@ -61,6 +66,9 @@ export class Updux<S = any> {
|
||||
constructor(config: UpduxConfig = {}) {
|
||||
this.groomMutations = config.groomMutations || ((x: Mutation<S>) => x);
|
||||
|
||||
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>;
|
||||
@ -219,6 +227,14 @@ export class Updux<S = any> {
|
||||
].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;
|
||||
|
Loading…
Reference in New Issue
Block a user