diff --git a/src/Uno.UI.RemoteControl/RemoteControlClient.cs b/src/Uno.UI.RemoteControl/RemoteControlClient.cs index c05abcabf61a..de8d05b302b3 100644 --- a/src/Uno.UI.RemoteControl/RemoteControlClient.cs +++ b/src/Uno.UI.RemoteControl/RemoteControlClient.cs @@ -33,7 +33,63 @@ public partial class RemoteControlClient : IRemoteControlClient public delegate void RemoteControlClientEventEventHandler(object sender, ClientEventEventArgs args); public delegate void SendMessageFailedEventHandler(object sender, SendMessageFailedEventArgs args); - public static RemoteControlClient? Instance { get; private set; } + public static RemoteControlClient? Instance + { + get => _instance; + private set + { + _instance = value; + + if (value is { }) + { + while (Interlocked.Exchange(ref _waitingList, null) is { } waitingList) + { + foreach (var action in waitingList) + { + action(value); + } + } + } + } + } + + private static IReadOnlyCollection>? _waitingList; + + /// + /// Add a callback to be called when the Instance is available. + /// + /// + /// Will be called synchronously if the instance is already available, no need to check for it before. + /// + public static void OnRemoteControlClientAvailable(Action action) + { + if(Instance is not null) + { + action(Instance); + } + else + { + // Thread-safe way to add the action to a waiting list for the client to be available + while (true) + { + var waitingList = _waitingList; + IReadOnlyCollection> newList = waitingList is null + ? [action] + : [.. waitingList, action]; + + if(Instance is { } i) // Last chance to avoid the waiting list + { + action(i); + break; + } + + if (ReferenceEquals(Interlocked.CompareExchange(ref _waitingList, newList, waitingList), waitingList)) + { + break; + } + } + } + } public static RemoteControlClient Initialize(Type appType) => Instance = new RemoteControlClient(appType); @@ -59,6 +115,7 @@ internal static RemoteControlClient Initialize(Type appType, ServerEndpointAttri private readonly StatusSink _status; private static readonly TimeSpan _keepAliveInterval = TimeSpan.FromSeconds(30); + private static RemoteControlClient? _instance; private readonly (string endpoint, int port)[]? _serverAddresses; private readonly Dictionary _processors = new(); private readonly List _preprocessors = new();