angular-redux / store

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

Epics subscription #472

Open furyscript opened 6 years ago

furyscript commented 6 years ago

This is a...

What toolchain are you using for transpilation/bundling?

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...

lvidal1 commented 6 years ago

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.

Shiroh1ge commented 6 years ago

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.

furyscript commented 6 years ago

@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.

dummdidumm commented 6 years ago

Things you could try:

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.