store
store copied to clipboard
Dispatch decorator should accept Observable
This is a...
[x] feature request
What toolchain are you using for transpilation/bundling?
[x] @angular/cli
Environment
NodeJS Version: 8.7.0 Typescript Version: 2.3.4 Angular Version: 4.3.x @angular-redux/store version: 6.5.7 @angular/cli version: (if applicable) OS: macOS
Expected Behaviour:
The dispatch
decorator should accept more than just a simple object. Sometimes it would be very useful to return a promise or even better observable in the method of component. If dispatch
could accept this behaviour and after receiving the first value dispatch it to store would remove lots of boilerplate.
Example of use-case:
@dispatch()
selectionChange(target: HTMLSelectElement) {
const { value } = target;
return this.items$
.map(items => items.find(item => item.value === value))
.map(item => item === undefined ? false : item)
.map(item => { type: 'SELECTED_ITEM', payload: item });
Actual Behaviour:
The dispatch
decorator only accepts the method to return a plain object.
Additional Notes:
If you are on board with the idea I can work on it and create a PR.
Hrmm, this could be kind of interesting - but wondering if a middleware would be better suited - as results of dspatch will go through all middleware.
That said, I do like the idea - I'll think on this a bit more.
I'll let you decide. I'll be glad to work on a prototype.
@aminpaks If you want to work on a prototype, happy to review the PR.
Some other questions/thoughts I'd have though is:
- would the dispatch only fire when the observable that is returned completes?
- would the dispatch fire for every item emitted?
- how would we ensure that any subscriptions inside of the dispatch get cleaned up
to answer to your questions:
- We should not complete the observable until the component is destroyed.
- Yes. Developer is responsible for any value that is being emitted to this lift observable. But I don't believe this should be a concern cause if the value is not a plain object there is other layers that will point this out.
- Descriptor helps us to assign a function to component that follows Angular standard lifecycle and once component is about to be destroyed we release the lift observable with a simple
takeUntil
or simply an unsubscription call
If this is clear I can work on a prototype.