fix(core): update
This commit is contained in:
@ -6,9 +6,9 @@ import { startWith, takeUntil, map } from 'rxjs/operators';
|
||||
import { StateAction, IActionDef } from './smartstate.classes.stateaction';
|
||||
|
||||
export class StatePart<TStatePartName, TStatePayload> {
|
||||
name: TStatePartName;
|
||||
state = new Subject<TStatePayload>();
|
||||
stateStore: TStatePayload;
|
||||
public name: TStatePartName;
|
||||
public state = new Subject<TStatePayload>();
|
||||
public stateStore: TStatePayload;
|
||||
|
||||
constructor(nameArg: TStatePartName) {
|
||||
this.name = nameArg;
|
||||
@ -17,7 +17,7 @@ export class StatePart<TStatePartName, TStatePayload> {
|
||||
/**
|
||||
* gets the state from the state store
|
||||
*/
|
||||
getState(): TStatePayload {
|
||||
public getState(): TStatePayload {
|
||||
return this.stateStore;
|
||||
}
|
||||
|
||||
@ -25,7 +25,7 @@ export class StatePart<TStatePartName, TStatePayload> {
|
||||
* sets the stateStore to the new state
|
||||
* @param newStateArg
|
||||
*/
|
||||
setState(newStateArg: TStatePayload) {
|
||||
public setState(newStateArg: TStatePayload) {
|
||||
this.stateStore = newStateArg;
|
||||
this.notifyChange();
|
||||
}
|
||||
@ -33,21 +33,27 @@ export class StatePart<TStatePartName, TStatePayload> {
|
||||
/**
|
||||
* notifies of a change on the state
|
||||
*/
|
||||
notifyChange() {
|
||||
public notifyChange() {
|
||||
this.state.next(this.stateStore);
|
||||
}
|
||||
|
||||
/**
|
||||
* selects a state or a substate
|
||||
*/
|
||||
select<T = TStatePayload>(selectorFn?: (state: TStatePayload) => T): Observable<T> {
|
||||
public select<T = TStatePayload>(selectorFn?: (state: TStatePayload) => T): Observable<T> {
|
||||
if (!selectorFn) {
|
||||
selectorFn = (state: TStatePayload) => <T>(<any>state);
|
||||
}
|
||||
|
||||
const mapped = this.state.pipe(
|
||||
startWith(this.getState()),
|
||||
map(selectorFn)
|
||||
map((stateArg) => {
|
||||
try {
|
||||
return selectorFn(stateArg);
|
||||
} catch (e) {
|
||||
// Nothing here
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
return mapped;
|
||||
@ -56,17 +62,33 @@ export class StatePart<TStatePartName, TStatePayload> {
|
||||
/**
|
||||
* creates an action capable of modifying the state
|
||||
*/
|
||||
createAction<TActionPayload>(
|
||||
public createAction<TActionPayload>(
|
||||
actionDef: IActionDef<TStatePayload, TActionPayload>
|
||||
): StateAction<TStatePayload, TActionPayload> {
|
||||
return new StateAction(actionDef);
|
||||
return new StateAction(this, actionDef);
|
||||
}
|
||||
|
||||
/**
|
||||
* dispatches an action on the statepart level
|
||||
*/
|
||||
async dispatchAction<T>(stateAction: StateAction<TStatePayload, T>, actionPayload: T) {
|
||||
public async dispatchAction<T>(stateAction: StateAction<TStatePayload, T>, actionPayload: T) {
|
||||
const newState = await stateAction.actionDef(this, actionPayload);
|
||||
this.setState(newState);
|
||||
}
|
||||
|
||||
/**
|
||||
* waits until a certain part of the state becomes available
|
||||
* @param selectorFn
|
||||
*/
|
||||
public async waitUntilPresent<T = TStatePayload>(selectorFn?: (state: TStatePayload) => T): Promise<T> {
|
||||
const done = plugins.smartpromise.defer<T>();
|
||||
const selectedObservable = this.select(selectorFn);
|
||||
const subscription = selectedObservable.subscribe(value => {
|
||||
if (value) {
|
||||
subscription.unsubscribe();
|
||||
done.resolve(value);
|
||||
}
|
||||
});
|
||||
return await done.promise;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user