angular-redux / store

Angular 2+ bindings for Redux
MIT License
1.34k stars 202 forks source link

Dispatch decorator should accept Observable #471

Open aminpaks opened 6 years ago

aminpaks commented 6 years ago

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.

e-schultz commented 6 years ago

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.

aminpaks commented 6 years ago

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

e-schultz commented 6 years ago

@aminpaks If you want to work on a prototype, happy to review the PR.

Some other questions/thoughts I'd have though is:

aminpaks commented 6 years ago

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.