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

Define an IUnityMainThreadDispatcher interface #32

Open
Rabadash8820 opened this issue Apr 4, 2024 · 0 comments
Open

Define an IUnityMainThreadDispatcher interface #32

Rabadash8820 opened this issue Apr 4, 2024 · 0 comments

Comments

@Rabadash8820
Copy link

Rabadash8820 commented Apr 4, 2024

I love UnityMainThreadDispatcher; it solves a very simple problem well. However, I strongly dislike the static singleton anti-pattern used throughout the Unity ecosystem, and unfortunately this library is one of the culprits. I suggest adding the following interface:

public interface IUnityMainThreadDispatcher
{
    void Enqueue(IEnumerator action);
    void Enqueue(Action action);
    Task EnqueueAsync(Action action);
}

which the UnityMainThreadDispatcher class would implement. That way, developers can inject IUnityMainThreadDispatcher implementations (possibly their own) via dependency injection, mock it in unit tests, and generally avoid static cling in their code. This would not be a breaking change, as the static Instance method would still be present on the implementing class.

You can see my own version of this for reference in my personal UnityUtil library.

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

1 participant