store icon indicating copy to clipboard operation
store copied to clipboard

Dispatch decorator should accept Observable

Open aminpaks opened this issue 7 years ago • 4 comments

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.

aminpaks avatar Oct 14 '17 20:10 aminpaks

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.

e-schultz avatar Oct 17 '17 15:10 e-schultz

I'll let you decide. I'll be glad to work on a prototype.

aminpaks avatar Oct 17 '17 18:10 aminpaks

@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

e-schultz avatar Nov 08 '17 18:11 e-schultz

to answer to your questions:

  1. We should not complete the observable until the component is destroyed.
  2. 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.
  3. 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.

aminpaks avatar Nov 08 '17 23:11 aminpaks