From 499e987219101a6e0d0c4a8e064eb2fbde5c92c3 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 3 Jan 2020 10:37:21 -0500 Subject: [PATCH 1/8] add the test --- src/selectors.test.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/selectors.test.ts diff --git a/src/selectors.test.ts b/src/selectors.test.ts new file mode 100644 index 0000000..524e801 --- /dev/null +++ b/src/selectors.test.ts @@ -0,0 +1,27 @@ +import Updux from '.'; + +test('basic selectors', () => { + const updux = new Updux({ + subduxes: { + bogeys: { + selectors: { + bogey: (bogeys: any) => (id: string) => bogeys[id], + }, + }, + }, + selectors: { + bogeys: ({ bogeys }: any) => bogeys, + }, + } as any); + + const state = { + bogeys: { + foo: 1, + bar: 2 + } + }; + + expect( updux.selectors.bogeys(state) ).toEqual( { foo:1, bar :2 } ); + expect( updux.selectors.bogey(state)('foo')).toEqual(1); + +}); From 1825e8d2690bab00a9fed45d4bfd0d18786e6b83 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 3 Jan 2020 12:00:24 -0500 Subject: [PATCH 2/8] 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; From f744616cb26ae14a7ded4887d9d1a8bc1b67f86f Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 3 Jan 2020 17:27:11 -0500 Subject: [PATCH 3/8] wip --- src/buildSelectors/index.ts | 6 ++++-- src/selectors.test.ts | 4 +--- src/types.ts | 2 ++ src/updux.ts | 25 +++++++++++++------------ 4 files changed, 20 insertions(+), 17 deletions(-) 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); } } From 931377b5840cf7d595611dbbfaee4d40ed784139 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 3 Jan 2020 19:40:20 -0500 Subject: [PATCH 4/8] tests are passing --- src/buildMiddleware/index.ts | 14 +++++++------ src/selectors.test.ts | 38 +++++++++++++++++++++++++++++++----- src/types.ts | 3 ++- src/updux.ts | 9 ++++----- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/buildMiddleware/index.ts b/src/buildMiddleware/index.ts index 0b1d198..8421869 100644 --- a/src/buildMiddleware/index.ts +++ b/src/buildMiddleware/index.ts @@ -10,6 +10,7 @@ import { UpduxMiddlewareAPI, EffectEntry } from "../types"; +import Updux from ".."; const MiddlewareFor = ( type: any, @@ -23,12 +24,13 @@ const MiddlewareFor = ( type Next = (action: Action) => any; -function sliceMw(slice: string, mw: Middleware): Middleware { +function sliceMw(slice: string, mw: Middleware, updux: Updux): Middleware { return api => { const getSliceState = slice.length > 0 ? () => fp.get(slice, api.getState()) : api.getState; const getRootState = (api as any).getRootState || api.getState; - return mw({ ...api, getState: getSliceState, getRootState } as any); + return mw({ ...api, getState: getSliceState, getRootState, + selectors: updux.selectors } as any); }; } @@ -37,11 +39,11 @@ function buildMiddleware( actions: Dictionary = {} ): UpduxMiddleware { let mws = middlewareEntries - .map(([slice, actionType, mw, isGen]: any) => - isGen ? [slice, actionType, mw()] : [slice, actionType, mw] + .map(([updux, slice, actionType, mw, isGen]: any) => + isGen ? [updux, slice, actionType, mw()] : [updux, slice, actionType, mw] ) - .map(([slice, actionType, mw]) => - MiddlewareFor(actionType, sliceMw(slice, mw)) + .map(([updux, slice, actionType, mw]) => + MiddlewareFor(actionType, sliceMw(slice, mw, updux)) ); return (api: UpduxMiddlewareAPI) => { diff --git a/src/selectors.test.ts b/src/selectors.test.ts index 0a689ee..29feabe 100644 --- a/src/selectors.test.ts +++ b/src/selectors.test.ts @@ -17,11 +17,39 @@ test('basic selectors', () => { const state = { bogeys: { foo: 1, - bar: 2 - } + 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.bogeys(state)).toEqual({ foo: 1, bar: 2 }); + expect((updux.selectors.bogey(state) as any)('foo')).toEqual(1); +}); + +test('available in the middleware', () => { + const updux = new Updux({ + subduxes: { + bogeys: { + initial: { enkidu: 'foo' }, + selectors: { + bogey: (bogeys: any) => (id: string) => bogeys[id], + }, + }, + }, + effects: { + doIt: ({ selectors: { bogey }, getState }) => next => action => { + next({ + ...action, + payload: bogey(getState())('enkidu'), + }); + }, + }, + mutations: { + doIt: payload => state => ({ ...state, payload }), + }, + }); + + const store = updux.createStore(); + store.dispatch.doIt(); + + expect(store.getState()).toMatchObject({ payload: 'foo' }); }); diff --git a/src/types.ts b/src/types.ts index ce75ac4..98e2bcf 100644 --- a/src/types.ts +++ b/src/types.ts @@ -200,9 +200,10 @@ export interface UpduxMiddlewareAPI { dispatch: UpduxDispatch; getState(): any; getRootState(): S; + selectors: Dictionary; } export type UpduxMiddleware = ( api: UpduxMiddlewareAPI ) => (next: UpduxDispatch) => (action: Action) => any; -export type Selector = (state:S) => unknown; +export type Selector = (state:S) => any; diff --git a/src/updux.ts b/src/updux.ts index 20f5180..9f46c59 100644 --- a/src/updux.ts +++ b/src/updux.ts @@ -200,22 +200,21 @@ export class Updux { get _middlewareEntries() { const groupByOrder = (mws: any) => fp.groupBy( - ([_, actionType]: any) => + ([a,b, actionType]: any) => ["^", "$"].includes(actionType) ? actionType : "middle", mws ); let subs = fp.flow([ - fp.mapValues("_middlewareEntries"), fp.toPairs, - fp.map(([slice, entries]) => - entries.map(([ps, ...args]: any) => [[slice, ...ps], ...args]) + fp.map(([slice, updux]) => + updux._middlewareEntries.map(([u, ps, ...args]: any) => [u,[slice, ...ps], ...args]) ), fp.flatten, groupByOrder ])(this.subduxes); - let local = groupByOrder(this.localEffects.map(x => [[], ...x])); + let local = groupByOrder(this.localEffects.map(x => [this,[], ...x])); return fp.flatten( [ From 4c0d7b366fa24e7780131bfa263c7e524889d5d6 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Mon, 27 Jan 2020 23:35:01 -0500 Subject: [PATCH 5/8] wip --- jest.config.js | 2 +- src/middleware.test.ts | 491 ++++++++++++++++++++++------------------- src/middleware_aux.ts | 13 ++ 3 files changed, 284 insertions(+), 222 deletions(-) create mode 100644 src/middleware_aux.ts diff --git a/jest.config.js b/jest.config.js index 2d94fac..096fb97 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,6 +1,6 @@ module.exports = { "roots": [ - "./src" + "./dist" ], "transform": { "^.+\\.ts$": "ts-jest", diff --git a/src/middleware.test.ts b/src/middleware.test.ts index 54d02a9..e3a0a1c 100644 --- a/src/middleware.test.ts +++ b/src/middleware.test.ts @@ -1,228 +1,277 @@ -import Updux, { actionCreator } from "."; -import u from "updeep"; +import Updux, { actionCreator } from '.'; +import u from 'updeep'; +import mwUpdux from './middleware_aux'; -test("simple effect", () => { - const tracer = jest.fn(); +test('simple effect', () => { + const tracer = jest.fn(); - const store = new Updux({ - effects: { - foo: (api: any) => (next: any) => (action: any) => { - tracer(); - next(action); - } - } - }).createStore(); - - expect(tracer).not.toHaveBeenCalled(); - - store.dispatch({ type: "bar" }); - - expect(tracer).not.toHaveBeenCalled(); - - store.dispatch.foo(); - - expect(tracer).toHaveBeenCalled(); -}); - -test("effect and sub-effect", () => { - const tracer = jest.fn(); - - const tracerEffect = (signature: string) => (api: any) => (next: any) => ( - action: any - ) => { - tracer(signature); - next(action); - }; - - const store = new Updux({ - effects: { - foo: tracerEffect("root") - }, - subduxes: { - zzz: { + const store = new Updux({ effects: { - foo: tracerEffect("child") - } - } - } - }).createStore(); - - expect(tracer).not.toHaveBeenCalled(); - - store.dispatch({ type: "bar" }); - - expect(tracer).not.toHaveBeenCalled(); - - store.dispatch.foo(); - - expect(tracer).toHaveBeenNthCalledWith(1, "root"); - expect(tracer).toHaveBeenNthCalledWith(2, "child"); -}); - -test('"*" effect', () => { - const tracer = jest.fn(); - - const store = new Updux({ - effects: { - "*": api => next => action => { - tracer(); - next(action); - } - } - }).createStore(); - - expect(tracer).not.toHaveBeenCalled(); - - store.dispatch({ type: "bar" }); - - expect(tracer).toHaveBeenCalled(); -}); - -test("async effect", async () => { - function timeout(ms: number) { - return new Promise(resolve => setTimeout(resolve, ms)); - } - - const tracer = jest.fn(); - - const store = new Updux({ - effects: { - foo: api => next => async action => { - next(action); - await timeout(1000); - tracer(); - } - } - }).createStore(); - - expect(tracer).not.toHaveBeenCalled(); - - store.dispatch.foo(); - - expect(tracer).not.toHaveBeenCalled(); - - await timeout(1000); - - expect(tracer).toHaveBeenCalled(); -}); - -test("getState is local", () => { - let childState; - let rootState; - let rootFromChild; - - const child = new Updux({ - initial: { alpha: 12 }, - effects: { - doIt: ({ getState, getRootState }) => next => action => { - childState = getState(); - rootFromChild = getRootState(); - next(action); - } - } - }); - - const root = new Updux({ - initial: { beta: 24 }, - subduxes: { child }, - effects: { - doIt: ({ getState }) => next => action => { - rootState = getState(); - next(action); - } - } - }); - - const store = root.createStore(); - store.dispatch.doIt(); - - expect(rootState).toEqual({ beta: 24, child: { alpha: 12 } }); - expect(rootFromChild).toEqual({ beta: 24, child: { alpha: 12 } }); - expect(childState).toEqual({ alpha: 12 }); -}); - -test("middleware as map", () => { - let childState; - let rootState; - let rootFromChild; - - const doIt = actionCreator("doIt"); - - const child = new Updux({ - initial: "", - effects: [ - [ - doIt, - () => next => action => { - next(u({ payload: (p: string) => p + "Child" }, action) as any); - } - ] - ] - }); - - const root = new Updux({ - initial: { message: "" }, - subduxes: { child }, - effects: [ - [ - "^", - () => next => action => { - next(u({ payload: (p: string) => p + "Pre" }, action) as any); - } - ], - [ - doIt, - () => next => action => { - next(u({ payload: (p: string) => p + "Root" }, action) as any); - } - ], - [ - "*", - () => next => action => { - next(u({ payload: (p: string) => p + "After" }, action) as any); - } - ], - [ - "$", - () => next => action => { - next(u({ payload: (p: string) => p + "End" }, action) as any); - } - ] - ], - mutations: [[doIt, (message: any) => () => ({ message })]] - }); - - const store = root.createStore(); - store.dispatch.doIt(""); - - expect(store.getState()).toEqual({ message: "PreRootAfterChildEnd" }); -}); - -test("generator", () => { - const updux = new Updux({ - initial: 0, - mutations: [["doIt", payload => () => payload]], - effects: [ - [ - "doIt", - () => { - let i = 0; - return () => (next: any) => (action: any) => - next({ ...action, payload: ++i }); + foo: (api: any) => (next: any) => (action: any) => { + tracer(); + next(action); + }, }, - true - ] - ] - }); + }).createStore(); - const store1 = updux.createStore(); - store1.dispatch.doIt(); - expect(store1.getState()).toEqual(1); - store1.dispatch.doIt(); - expect(store1.getState()).toEqual(2); - updux.actions; + expect(tracer).not.toHaveBeenCalled(); - const store2 = updux.createStore(); - store2.dispatch.doIt(); - expect(store2.getState()).toEqual(1); + store.dispatch({ type: 'bar' }); + + expect(tracer).not.toHaveBeenCalled(); + + store.dispatch.foo(); + + expect(tracer).toHaveBeenCalled(); +}); + +test('effect and sub-effect', () => { + const tracer = jest.fn(); + + const tracerEffect = (signature: string) => (api: any) => (next: any) => ( + action: any + ) => { + tracer(signature); + next(action); + }; + + const store = new Updux({ + effects: { + foo: tracerEffect('root'), + }, + subduxes: { + zzz: { + effects: { + foo: tracerEffect('child'), + }, + }, + }, + }).createStore(); + + expect(tracer).not.toHaveBeenCalled(); + + store.dispatch({ type: 'bar' }); + + expect(tracer).not.toHaveBeenCalled(); + + store.dispatch.foo(); + + expect(tracer).toHaveBeenNthCalledWith(1, 'root'); + expect(tracer).toHaveBeenNthCalledWith(2, 'child'); +}); + +describe('"*" effect', () => { + test('from the constructor', () => { + const tracer = jest.fn(); + + const store = new Updux({ + effects: { + '*': api => next => action => { + tracer(); + next(action); + }, + }, + }).createStore(); + + expect(tracer).not.toHaveBeenCalled(); + + store.dispatch({ type: 'bar' }); + + expect(tracer).toHaveBeenCalled(); + }); + + test('from addEffect', () => { + const tracer = jest.fn(); + + const updux = new Updux({}); + + updux.addEffect('*', api => next => action => { + tracer(); + next(action); + }); + + expect(tracer).not.toHaveBeenCalled(); + + updux.createStore().dispatch({ type: 'bar' }); + + expect(tracer).toHaveBeenCalled(); + }); + + test('action can be modified', () => { + + const mw = mwUpdux.middleware; + + const next = jest.fn(); + + mw({dispatch:{}} as any)(next as any)({type: 'bar'}); + + expect(next).toHaveBeenCalled(); + + expect(next.mock.calls[0][0]).toMatchObject({meta: 'gotcha'}); + }); +}); + +test('async effect', async () => { + function timeout(ms: number) { + return new Promise(resolve => setTimeout(resolve, ms)); + } + + const tracer = jest.fn(); + + const store = new Updux({ + effects: { + foo: api => next => async action => { + next(action); + await timeout(1000); + tracer(); + }, + }, + }).createStore(); + + expect(tracer).not.toHaveBeenCalled(); + + store.dispatch.foo(); + + expect(tracer).not.toHaveBeenCalled(); + + await timeout(1000); + + expect(tracer).toHaveBeenCalled(); +}); + +test('getState is local', () => { + let childState; + let rootState; + let rootFromChild; + + const child = new Updux({ + initial: { alpha: 12 }, + effects: { + doIt: ({ getState, getRootState }) => next => action => { + childState = getState(); + rootFromChild = getRootState(); + next(action); + }, + }, + }); + + const root = new Updux({ + initial: { beta: 24 }, + subduxes: { child }, + effects: { + doIt: ({ getState }) => next => action => { + rootState = getState(); + next(action); + }, + }, + }); + + const store = root.createStore(); + store.dispatch.doIt(); + + expect(rootState).toEqual({ beta: 24, child: { alpha: 12 } }); + expect(rootFromChild).toEqual({ beta: 24, child: { alpha: 12 } }); + expect(childState).toEqual({ alpha: 12 }); +}); + +test('middleware as map', () => { + let childState; + let rootState; + let rootFromChild; + + const doIt = actionCreator('doIt'); + + const child = new Updux({ + initial: '', + effects: [ + [ + doIt, + () => next => action => { + next( + u( + { payload: (p: string) => p + 'Child' }, + action + ) as any + ); + }, + ], + ], + }); + + const root = new Updux({ + initial: { message: '' }, + subduxes: { child }, + effects: [ + [ + '^', + () => next => action => { + next( + u({ payload: (p: string) => p + 'Pre' }, action) as any + ); + }, + ], + [ + doIt, + () => next => action => { + next( + u({ payload: (p: string) => p + 'Root' }, action) as any + ); + }, + ], + [ + '*', + () => next => action => { + next( + u( + { payload: (p: string) => p + 'After' }, + action + ) as any + ); + }, + ], + [ + '$', + () => next => action => { + next( + u({ payload: (p: string) => p + 'End' }, action) as any + ); + }, + ], + ], + mutations: [[doIt, (message: any) => () => ({ message })]], + }); + + const store = root.createStore(); + store.dispatch.doIt(''); + + expect(store.getState()).toEqual({ message: 'PreRootAfterChildEnd' }); +}); + +test('generator', () => { + const updux = new Updux({ + initial: 0, + mutations: [['doIt', payload => () => payload]], + effects: [ + [ + 'doIt', + () => { + let i = 0; + return () => (next: any) => (action: any) => + next({ ...action, payload: ++i }); + }, + true, + ], + ], + }); + + const store1 = updux.createStore(); + store1.dispatch.doIt(); + expect(store1.getState()).toEqual(1); + store1.dispatch.doIt(); + expect(store1.getState()).toEqual(2); + updux.actions; + + const store2 = updux.createStore(); + store2.dispatch.doIt(); + expect(store2.getState()).toEqual(1); }); diff --git a/src/middleware_aux.ts b/src/middleware_aux.ts new file mode 100644 index 0000000..47ab313 --- /dev/null +++ b/src/middleware_aux.ts @@ -0,0 +1,13 @@ +import Updux from '.'; + +const updux = new Updux({ + subduxes: { + foo: { initial: "banana" } + } +}); + +updux.addEffect('*', api => next => action => { + next({...action, meta: "gotcha" }); +}); + +export default updux; From e7fbd582fcf467d1bab3894739ae542a302dc337 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 2 Feb 2020 14:53:13 -0500 Subject: [PATCH 6/8] test src --- jest.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jest.config.js b/jest.config.js index 096fb97..2d94fac 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,6 +1,6 @@ module.exports = { "roots": [ - "./dist" + "./src" ], "transform": { "^.+\\.ts$": "ts-jest", From 64a36c40883fad62f53f2e55577f734d13ab2f53 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 2 Feb 2020 14:53:33 -0500 Subject: [PATCH 7/8] ignore orig files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index c05203a..03995cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ tsconfig.tsbuildinfo +**/*.orig From 91923bfd49e0ab26bc9aaac46e61d4a18882a501 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 2 Feb 2020 23:45:16 -0500 Subject: [PATCH 8/8] add documentation --- docs/_sidebar.md | 1 + docs/concepts.md | 30 ++++++++++++++++++++++++++++++ docs/updux.md | 25 +++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 docs/concepts.md diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 67ee16e..dfdd5c5 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -1,5 +1,6 @@ * [Home](/) +* [Concepts](concepts.md) * API Reference * [Updux](updux.md) diff --git a/docs/concepts.md b/docs/concepts.md new file mode 100644 index 0000000..65cbbc6 --- /dev/null +++ b/docs/concepts.md @@ -0,0 +1,30 @@ + # Updux concepts + +## effects + +Updux effects are a superset of redux middleware. I kept that format, and the +use of `next` mostly because I wanted to give myself a way to alter +actions before they hit the reducer, something that `redux-saga` and +`rematch` don't allow. + +An effect has the signature + +```js +const effect = ({ getState, dispatch, getRootState, selectors}) + => next => action => { ... } +``` + +The first argument is like the usual redux middlewareApi, except +for the availability of selectors and of the root updux's state. + +Also, the function `dispatch` is augmented to be able to be called +with the allowed actions as props. For example, assuming that the action +`complete_todo` has been declared somewhere, then it's possible to do: + +```js +updux.addEffect( 'todo_bankrupcy', + ({ getState, dispatch }) => next => action => { + getState.forEach( todo => dispatch.complete_todo( todo.id ) ); + } +) +``` diff --git a/docs/updux.md b/docs/updux.md index d95ba2c..debb225 100644 --- a/docs/updux.md +++ b/docs/updux.md @@ -36,6 +36,23 @@ const { actions } = updux({ actions.foo({ x: 1, y: 2 }); // => { type: foo, payload: { x:1, y:2 } } actions.bar(1,2); // => { type: bar, payload: { x:1, y:2 } } + + +#### selectors + +Dictionary of selectors for the current updux. The updux also +inherit its dubduxes' selectors. + +The selectors are available via the class' getter and, for +middlewares, the middlewareApi. + +```js +const todoUpdux = new Updux({ + selectors: { + done: state => state.filter( ({done}) => done ), + byId: state => targetId => state.find( ({id}) => id === targetId ), + } +} ``` #### mutations @@ -332,3 +349,11 @@ baz(); // => { type: 'baz', payload: undefined } ``` +### selectors + +Returns a dictionary of the +updux's selectors. Subduxes' selectors +are included as well (with the mapping to the sub-state already +taken care of you). + +