From a4d43456d7ce52b131858fc72e77155cfe461bae Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Tue, 26 Feb 2019 18:09:38 +0100 Subject: [PATCH] update action generation --- test/test.ts | 14 ++++++++++--- ts/smartstate.classes.stateaction.ts | 9 ++++++-- ts/smartstate.classes.statepart.ts | 31 +++++++++++++++++----------- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/test/test.ts b/test/test.ts index 801f317..a89c9b0 100644 --- a/test/test.ts +++ b/test/test.ts @@ -30,12 +30,20 @@ tap.test('should create a new StatePart', async () => { tap.test('should select something', async () => { testStatePart - .select(state => state.deep) + .select(state => state.deep.hi) .subscribe(substate => { - console.log(substate); + expect(substate).to.equal(2); }); }); -tap.test('should dispatch a state action', async () => {}); +tap.test('should dispatch a state action', async () => { + const addFavourite = testStatePart.createAction(async (statePart, payload) => { + const currentState = statePart.getState(); + currentState.currentFavorites.push(payload) + return currentState; + }); + await testStatePart.dispatchAction(addFavourite, 'my favourite things') + expect(testStatePart.getState().currentFavorites).to.include('my favourite things') +}); tap.start(); diff --git a/ts/smartstate.classes.stateaction.ts b/ts/smartstate.classes.stateaction.ts index 201c667..2dadc65 100644 --- a/ts/smartstate.classes.stateaction.ts +++ b/ts/smartstate.classes.stateaction.ts @@ -1,8 +1,13 @@ import * as plugins from './smartstate.plugins'; +import { StatePart } from './smartstate.classes.statepart'; + +export interface IActionDef { + (stateArg: StatePart, actionPayload: TActionPayloadType): Promise; +} /** * an actionmodifier for the state */ -export class StateAction { - constructor(public actionDef: (stateArg: StatePayload) => StatePayload) {} +export class StateAction { + constructor(public actionDef: IActionDef) {} } diff --git a/ts/smartstate.classes.statepart.ts b/ts/smartstate.classes.statepart.ts index 5b7b6b5..b2c8d9f 100644 --- a/ts/smartstate.classes.statepart.ts +++ b/ts/smartstate.classes.statepart.ts @@ -3,21 +3,21 @@ import * as plugins from './smartstate.plugins'; import { Observable, Subject } from 'rxjs'; import { startWith, takeUntil, map } from 'rxjs/operators'; -import { StateAction } from './smartstate.classes.stateaction'; +import { StateAction, IActionDef } from './smartstate.classes.stateaction'; -export class StatePart { - name: StatePartNameType; - state = new Subject(); - stateStore: PayloadType; +export class StatePart { + name: TStatePartName; + state = new Subject(); + stateStore: TStatePayload; - constructor(nameArg: StatePartNameType) { + constructor(nameArg: TStatePartName) { this.name = nameArg; } /** * gets the state from the state store */ - getState(): PayloadType { + getState(): TStatePayload { return this.stateStore; } @@ -25,7 +25,7 @@ export class StatePart { * sets the stateStore to the new state * @param newStateArg */ - setState(newStateArg: PayloadType) { + setState(newStateArg: TStatePayload) { this.stateStore = newStateArg; this.notifyChange(); } @@ -40,9 +40,9 @@ export class StatePart { /** * selects a state or a substate */ - select(selectorFn?: (state: PayloadType) => T): Observable { + select(selectorFn?: (state: TStatePayload) => T): Observable { if (!selectorFn) { - selectorFn = (state: PayloadType) => (state); + selectorFn = (state: TStatePayload) => (state); } const mapped = this.state.pipe( @@ -53,11 +53,18 @@ export class StatePart { return mapped; } + /** + * creates an action capable of modifying the state + */ + createAction (actionDef: IActionDef): StateAction { + return new StateAction(actionDef); + } + /** * dispatches an action on the statepart level */ - async dispatch(stateAction: StateAction) { - const newState = stateAction.actionDef(this.getState()); + async dispatchAction(stateAction: StateAction, actionPayload: T) { + const newState = await stateAction.actionDef(this, actionPayload); this.setState(newState); } }