Skip to content

Define an IUnityMainThreadDispatcher interface #32

@Rabadash8820

Description

@Rabadash8820

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions