Skip to content
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

SubscribeToAction with NavigateTo throws an exception #81

Closed
stephens01 opened this issue Aug 18, 2020 · 4 comments
Closed

SubscribeToAction with NavigateTo throws an exception #81

stephens01 opened this issue Aug 18, 2020 · 4 comments

Comments

@stephens01
Copy link

In a Blazor WASM project, I am trying to handle the navigation to a new page once my HTTP method has completed successfully. My thinking is that I can then choose to remain on the page if I need to handle errors in the API call etc.

Using SubscribeToAction works unless I add a navigation call in the callback.: e.g.

         SubscribeToAction<AddItemResponseAction>(x =>
            {
                NavigationManager.NavigateTo("/");
            });

With the navigation call in place, I get Unhandled exception rendering component: Collection was modified; enumeration operation may not execute.

The exception is thrown by the line in the Notify method of the ActionSubscriber class (line #38):

foreach (Action<object> callback in callbacks)

If I change the previous Select query to persist (i.e. by adding a ToList() call), or change the foreach to foreach (Action<object> callback in callbacks.ToList()), the exception is not thrown.

It doesn't seem resolvable by unsubscribing from the actions. Is there a way I can resolve this in client code rather than modifying the ActionSubscriber class?

Thanks.

@mrpmorris
Copy link
Owner

This looks like the behaviour I would expect. Instead, add an effect that dispatches a GoAction.

@stephens01
Copy link
Author

That's very helpful; thanks. I see that this can be resolved by adding either an Effect class or an [EffectMethod] flagged method within my component (which is the option I'll use).

Do you think that the ActionAsubscriber Notify method should be protected from this reentrancy problem though?

(And thanks for the work on this library.)

@mrpmorris
Copy link
Owner

mrpmorris commented Aug 19, 2020

I'll have to think about whether or not to change the loop. The change should happen in an effect.

I would put the effect method in with my store, not in the component. They should be kept separate.

And if you don't take my advice, at least make it a static method so Fluxor doesn't create an instance of your component.

@stephens01
Copy link
Author

I appreciate your thoughts - thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants