Last active
January 28, 2019 12:19
-
-
Save deebloo/a2650cc636b1979b04fe6f4f9f903bf1 to your computer and use it in GitHub Desktop.
Wrapping a state manager to allow for async actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type StateChange<A> = A | Observable<A> | Promise<A>; | |
const stateChangeToObservable = <A>(result: StateChange<A>): Observable<A> => { | |
if (isObservable(result)) { | |
return result; | |
} else if (result instanceof Promise) { | |
return from(result); | |
} | |
return of(result); | |
}; | |
export class StateContainer<A, T> { | |
constructor( | |
private readonly dispatch: (action: A) => any, | |
public readonly value: Observable<T>\ | |
) {} | |
update(src: StateChange<A>): Observable<T> { | |
const res = stateChangeToObservable(src).pipe(shareReplay(1)); | |
res.subscribe(action => { | |
this.dispatch(action) | |
}); | |
return res.pipe( | |
switchMapTo(this.value), | |
take(1) | |
); | |
} | |
} |
Yeah was supposed to be returned. I've been writing to much rust
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nothing can subscribe to this, so what is the reason for this code?
Unless it was supposed to be returned?