-
Notifications
You must be signed in to change notification settings - Fork 401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐞[BUG]: NgxsReduxDevtoolsPluginModule logs actions in wrong order when dispatching actions from async actions #139
Comments
I am seeing that too. I have a different pattern for my calls:
I was using observables, but when I upgraded from ngsx 1.x to 2.0 RC20, the observables stopped working. I debugged and saw that the underlying line that calls HttpClient get was being hit, but the network call was never attempted. After hours, I tried the async/await pattern and the app works correctly (and I really like how the code looks now!), but paired actions in the store show up reversed in the Dev Tools. |
If you wrap |
Yes, I just confirmed that. I think the entries into DevTools are being completed when the @action function completes and not when patchState or setState are called. The sequence seems to be this:
|
@RobCannon can you try with the latest version and confirm if you are still seeing the issue? |
Ok, reading through this and I can confirm this is working as designed right now (now im not saying thats right but its how it is). So heres the gist:
We need to wait for completion before we send the action to the devtools. We also don't want to block any other actions from dispatching since one could be long running. It would also be incorrect to show them in sequential order since they actually didn't run that way. They might have been dispatched that way but not actually executed in that flow. |
@deebloo I am still seeing states in the "reverse" order after upgrading to @ngxs/store@2.0.0. I think the issue is with the semantics of dispatch(). It looks like dispatch immediately executes the supplied Action. I would expect that dispatch would add the Action to a queue and once the current Action completes, ngxs would see if there is anything left in the queue. I tried to look at the source code to see if that is true, but I got lost trying to track the Observables. I can use Observables, but I don't totally grok them enough where I can figure out what is going on in someone else code. I was having troubles with Observable based actions, but now that the code is out of RC, I will give that a try again and see if that changes anything. |
I tried Observables again and that is working now, but the DevTool is still showing actions in reverse order. Here are the two version of code that I am trying: Observable version:
Async version:
|
This is similar to #213 |
Should be fixed in 3.0 |
Hi, I have the version 3.0.1 and also I have the problem. Have You fix? |
@markwhitfeld - shouldn't the ordered actions fixed this? |
@amcdnl In theory it could have fixed it but I think we made an assumption here about the cause. |
I had a look at the code here and it sends the info to the redux dev tools when the action has completed. This makes sense because the dev tools requires the new state as part of an action's payload. By design in NGXS the parent action only completes when the child action completes, so because the child action is completing before the parent it is posting its completed state to the dev tools before the parent. @amcdnl We will need to figure out how to handle this correctly...
|
considering this is clearly still happening - can we reopen the issue? |
Re-opening because we need to figure out an approach that makes sense for the redux dev tools. |
Thanks for reopening the issue. While this is not a crucial topic, it is kind of irritating to have the wrong order. This misses an important point about the Action -> ActionSuccess || ActionError pattern, which most of us love using. May I ask what are your motives behind the decision to create a new devtools plugin? I myself would feel more comfortable to use the same plugin for all state instances I have open, whether ngrx, NGXS or something else. I find Redux DevTools pretty good and I don’t really see a significant ROI of a new plugin (please enlighten me!). P.S. Please also update the issue tags. |
@filipjnc There is much richer information that we are able to gather about the actions that we would want to display in the dev tools. Something that the standard redux dev tools won't support. When you see it you will know why ;-) It will be a while before this is ready though, so we do need to look at better options of displaying info in the redux dev tools. Ngxs's approach allows for a bit more flexibility than the typical redux style frameworks and as a result some of the paradigms don't fit nicely into what the redux dev tools expect. Actions dispatched as a part of a parent action are considered to be children and as a result the parent action only completes once the children complete. The completion states are what you are seeing in the dev tools, hence the out of order listing. |
Logging the dispatch only will also give an unexpected result because the state is not changed at dispatch. |
Any news on this Topic? I want to use ngxs instead of ngrx but this is a major drawback |
Really hard to track app state when this is happening ;/ Any new on this? |
Another issue with the DevTool showing actions in reverse order, is that the state changes are not displayed properly on the parent Action. If the parent action changes a property and the same property is then changed on the child, the DevTool shows no change on the parent. In this example if the service errors out the DevTools shows:
|
This issue was solved? I am still getting this behavior. |
@guilhermechiara sorry, but the problem is still not solved |
The devtools of ngxs seem to be abandoned. At least from what I could gather from ngxs/devtools#118 and the repo. So it comes down to fixing it in the plugin I guess? Are people using other ways to visualize their state, that I missed? |
Is there a workaround ? I tried this:
But it doesn't seems to work anymore. |
Checking for an update on this issue. As mentioned above it seems as though the ngxs/devtools project is abandoned. Would be great to see some kind of resolution or potential workaround on this issue. |
Starting a pretty big project and trying out NGXS as my state management. I'm running into this and wondering if it'll have implications down the road. Thinking of switching to Akita but it seems convoluted... Just want something as simple as this but maintained. |
Hi @DavidTheProgrammer. I can assure you that this project is maintained ;-) We had a contributor that started writing a dev tool for us (that would recognise the action lifecycle in ngxs), but they couldn't continue due to changes in their personal life, so it hasn't received much attention since then. |
Hi @markwhitfeld Thanks a lot for taking the time to respond to my comment with such level of detail, I really appreciate it. I now have a better understanding of the scope of the issue and the limitations of the dev-tools. It's unfortunate the contributor could not continue working on the custom plugin, seeing as that's the best solution for us currently, but I guess life happens and I hope he/she/they is/are alright.. Please keep us updated after discussing with the team; I'd be more than happy to help in any way I can. |
Hi @markwhitfeld. I think the whole action chaining mechanism is flawed. Treating dispatched actions from the action handler as child actions might not be the best approach. |
This is still a an outstanding issue in 3.7.2 |
This approach fixes the issue: When wrapping the new dispatch inside asyncScheduler the dispatch waits for the action to complete and then is executed: Works like a charm:
|
@Hypnoticbg Thx for the workaround. Here is my example. Maybe will be helpful @Action(GetUserList, {cancelUncompleted: true})
getUserList({patchState, dispatch}: StateContext<UserListStateModel>, action: GetUserList) {
patchState({loading: LoadingState.LOADING});
return this.userHttpService.getAll(action.params, action.pageable)
.pipe(
tap(items => asapScheduler.schedule(() => dispatch(new GetUserListSuccess(items)))),
catchError(error => {
asapScheduler.schedule(() => dispatch(new GetUserListFail(error)))
return throwError(error);
})
)
} |
We used to use asapScheduler until RxJS 7.x With the latest upgrade with Angular 14+ and RxJS 7.x this stopped working and to be able to work around it we had to change to asyncScheduler wherever we used it. Regarding the "weird when you need to modify your code to see the correct behavior", I believe this is happening intentionally because the body of the action decorator is just attached to the operator chain in the observable tree of the state. Adding it directly to the chain you don't have a way to delay it until the action is completed and here the scheduler takes place. If you want to have the content executed after the action it must be provided to the state as a callback function that will be executed on completion of the subscriber (The design of NGXS works differently). The author of the library seems to be avoiding this solution for a reason that I am not aware of, which probably causes other issues down the road. |
@Hypnoticbg Thx for the explanation. Yea I understand the nature of the issue on the same level as you. Just for me, it's a dramatic change of code just for the dev tool. It has 0 business value. And maybe even can produce some additional performance expenses or even make some troubles in SSR considering that Scheduler most likely uses some underhood timeout. |
@Halynsky It depends on what you do with the action stack. It doesn't affect the Developer tool directly but affects the action stack as a whole and the Developer tool just read it in the wrong way. In our particular case, we track these actions in the order they come so we can track the user journey and this is a problem if the successful actions come before the loading ones. In this case, it makes sense to make small changes to keep the order correct. Regarding the performance, we have tested both ways with the profiler and haven't noticed any peaks or degradation at all. |
Versions
Repro steps
When dispatching
RequestSearchOptions
stillRequestSearchOptionsSuccess
logs beforeRequestSearchOptions
in ReduxDevTools.Observed behavior
NgxsReduxDevtoolsPluginModule logs actions in wrong order when dispatching actions from async actions.
The text was updated successfully, but these errors were encountered: