store icon indicating copy to clipboard operation
store copied to clipboard

Epics subscription

Open furyscript opened this issue 6 years ago • 4 comments

This is a...

  • [ ] feature request
  • [ ] bug report
  • [x] usage question

What toolchain are you using for transpilation/bundling?

  • [x] angular/cli
  • [ ] Custom @ngTools/webpack
  • [ ] Raw ngc
  • [ ] SystemJS
  • [ ] Rollup
  • [ ] Other

I want to know if it's possible to subscribe ActionsObservable created by an Epics in a Component. With module @ngrx it's possibile to subscribe Effects like this:

       this.login_subscription = this._userEffect.login$
            .filter(action => action.type === UserAction.LOGIN)
            .subscribe(res => {
                this.router.navigate([this.returnUrl]);
                console.log("FINISH WITHOUT ERROR", res);
            });

With this I'm sure that I'm catch the action LOGIN... Now I'm checking a property of store like this:

       this.login_subscription = ngRedux.select().subscribe((value: UserState) => {
            if (value.is_logged_in) {
                this.router.navigate([this.returnUrl]);
            }
        });

But it's unsecure for me...

furyscript avatar Oct 20 '17 13:10 furyscript

Why is that unsecure? After an epic has dispatched its last action, the state has already changed. In that moment, that subscription to the state must give you the last changes ocurred on it.

lvidal1 avatar Oct 27 '17 06:10 lvidal1

Because it forces you to use state change in order to execute your logic, hence having to do 'if()' checks. Sometimes you only need the success/error response from the server. I am currently facing the same issue and I saw that with ngrx you can do something like:

    constructor(actions$: Actions) {
        actions$
            .ofType(PostActions.SAVE_POST_SUCCESS)
            .do(() => /* hooray, success, show notification alert etc.. */)
            .subscribe();
    }

I'm also interested if something like this is possible.

Shiroh1ge avatar Oct 30 '17 20:10 Shiroh1ge

@lvidal1

Why is that unsecure? After an epic has dispatched its last action, the state has already changed. In that moment, that subscription to the state must give you the last changes ocurred on it.

I think that it isn't the best way to check integrity of my state, because we have to do multiple if to check and determinate last actions called.

furyscript avatar Oct 31 '17 10:10 furyscript

Things you could try:

  • Implement a simple middleware that writes the actions to an observable. Inject that middleware as a service and subscribe to the observable.
  • Add a "last action" state to your redux store.

Note though that listening to actions inside a component opens up possibilites to change your UI based on actions, which is against the redux paradigm of the state defining the UI rendering.

In your case, I would suggest to rather write a normal Redux-Observable Epic which handles your navigation and not do that in the component at all.

dummdidumm avatar Jan 25 '18 21:01 dummdidumm