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

Effects class instantiated twice? #33

Closed
pm64 opened this issue May 9, 2020 · 6 comments
Closed

Effects class instantiated twice? #33

pm64 opened this issue May 9, 2020 · 6 comments

Comments

@pm64
Copy link

pm64 commented May 9, 2020

In debugging an issue where state didn't appear to be affected by a dispatched action as expected following a page navigation, I found that an effects class (class with one or more methods marked [EffectMethod]) is instantiated twice. I'm new to Fluxor am not sure if this is expected, but it seems wrong. Is there any common mistake I could be making that would explain this behavior?

Most likely I don't completely understand how to share state between pages/components. I assumed that each time IState is injected into a component, it's always a reference to the same state object, so the state is already implicitly shared between pages. But maybe some special handling is required to accomplish this?

@mrpmorris
Copy link
Owner

Hi

I looked at the source code to refresh my memory. Classes with [EffectMethod] methods in them are registered as Scoped, so you should get the same instance for every method in the same class. If you are seeing something different then I'll need to see a project that reproduces the problem.

You are correct about sharing state. If you inject IState<MyState> into more than one component then you should get the same state object. Again, if this is not what you are seeing then I'll need a project that reproduces the problem.

@pm64
Copy link
Author

pm64 commented May 9, 2020

Thank you @mrpmorris for the kind reply. Before I get too deep into creating an example project (which I'm more than happy to do), there's one part of my code that doesn't smell right, which might explain the issue.

In the aforementioned effects class, some of my EffectMethod handlers require access to the state, outside of what I supply in the action object. To make this possible, I inject IState into the effects class using a parameter on the effect class constructor. I then store a private reference to the state, which my methods access. In my mind this should work, but maybe it is the wrong approach?

I have traced the issue to this state reference in my effects class reflecting stale state data.

Example:

    private readonly IState<MyState> _myState;

    public Effects(IState<MyState> myState)
    {
        _myState = myState;
    }

    [EffectMethod]
    public async Task HandleMyAction(MyAction action, IDispatcher dispatcher)
    {
        var foo = _myState.Value.Blerg;
        ...
        dispatcher.Dispatch(new MyResultAction(...));
    }

The reason I don't simply pass the necessary parts of the state in the action object is that the effect requires access to tons of state properties, so instantiating the action object would be very cumbersome.

@pm64
Copy link
Author

pm64 commented May 10, 2020

Sorry to have wasted your time. This behavior was caused by an error in my state class design, not related to Fluxor.

@pm64 pm64 closed this as completed May 10, 2020
@mrpmorris
Copy link
Owner

Can you share any information so others can benefit?

@pm64
Copy link
Author

pm64 commented May 10, 2020

Normally I would have, but in this case the error was pure stupidity on my part. My state class constructor basically had an error like this:

 class MyState {
      public int Foo { get; }
      public MyState(int foo)
      {
           Foo = foo;
           Foo = 0;
      }
 }

So the passed value of foo was always ignored.

@mrpmorris
Copy link
Owner

Thanks :)

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