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

Subscription is called multiple times if you resubscribe at the same frame in async task #134

Open
nicloay opened this issue Oct 8, 2024 · 1 comment

Comments

@nicloay
Copy link

nicloay commented Oct 8, 2024

Message brocker use the following Publish method and here is how it take subscriber list
handlers.GetValues()
What happens is that I trigger the publisher (in my case from input) and then subscribe to the same event again

private ISubscriber<ButtonClickMsg> _testSubscriber; 
private async UniTask RunLoop()
{
    while (true)
    {
        await _testSubscriber.FirstAsync(default);
        Debug.Log(">> called 4 times instead of 1 <<");
    }
}

And never the less _testPublisher will be called once, you will see debug.log 4 times,

For the temporary solution I copy the subscriber array to another array, so new value will not be considered, but there are probably better ways of handling this

So in MessageBrocker.cs line 48 I have this

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Publish(TMessage message)
        {
            var array =  handlers.GetValues().ToArray(); // copy array here
            
            for (int i = 0; i < array.Length; i++)
            {
                array[i]?.Handle(message);
            }
        }
@huylqnothing
Copy link

same issue here, they did not fix it. I guess would have to delay a frame until they handle this.

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