// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. namespace Microsoft.ReactNative.Managed { public struct ReactNotificationService { public IReactNotificationService Handle { get; } public bool IsValid => Handle != null; public static explicit operator bool(ReactNotificationService service) => service.IsValid; public ReactNotificationService(IReactNotificationService handle = null) { Handle = handle; } public static ReactNotificationSubscriptionRevoker SubscribeWithAutoRevoke( IReactNotificationService handle, ReactNotificationId notificationId, ReactDispatcher dispatcher, ReactNotificationHandler handler) { return new ReactNotificationSubscriptionRevoker(Subscribe(handle, notificationId, dispatcher, handler)); } public static ReactNotificationSubscriptionRevoker SubscribeWithAutoRevoke( IReactNotificationService handle, ReactNotificationId notificationId, ReactNotificationHandler handler) { return new ReactNotificationSubscriptionRevoker(Subscribe(handle, notificationId, new ReactDispatcher(null), handler)); } public ReactNotificationSubscriptionRevoker SubscribeWithAutoRevoke( ReactNotificationId notificationId, ReactDispatcher dispatcher, ReactNotificationHandler handler) { return SubscribeWithAutoRevoke(Handle, notificationId, dispatcher, handler); } public ReactNotificationSubscriptionRevoker SubscribeWithAutoRevoke( ReactNotificationId notificationId, ReactNotificationHandler handler) { return SubscribeWithAutoRevoke(Handle, notificationId, handler); } public static ReactNotificationSubscription Subscribe( IReactNotificationService handle, ReactNotificationId notificationId, ReactDispatcher dispatcher, ReactNotificationHandler handler) { IReactNotificationSubscription subscriptionHandle = handle?.Subscribe( notificationId.Handle, dispatcher.Handle, (sender, args) => handler(sender, new ReactNotificationArgs(args))); return new ReactNotificationSubscription(subscriptionHandle); } public static ReactNotificationSubscription Subscribe( IReactNotificationService handle, ReactNotificationId notificationId, ReactNotificationHandler handler) { return Subscribe(handle, notificationId, new ReactDispatcher(null), handler); } public ReactNotificationSubscription Subscribe( ReactNotificationId notificationId, ReactDispatcher dispatcher, ReactNotificationHandler handler) { return Subscribe(Handle, notificationId, dispatcher, handler); } public ReactNotificationSubscription Subscribe( ReactNotificationId notificationId, ReactNotificationHandler handler) { return Subscribe(Handle, notificationId, handler); } public static void SendNotification( IReactNotificationService handle, ReactNotificationId notificationId, object sender, T value) { handle?.SendNotification(notificationId.Handle, sender, value); } public static void SendNotification( IReactNotificationService handle, ReactNotificationId notificationId, object sender) { handle?.SendNotification(notificationId.Handle, sender, null); } public static void SendNotification( IReactNotificationService handle, ReactNotificationId notificationId, T value) { handle?.SendNotification(notificationId.Handle, null, value); } public static void SendNotification( IReactNotificationService handle, ReactNotificationId notificationId) { handle?.SendNotification(notificationId.Handle, null, null); } public void SendNotification( ReactNotificationId notificationId, object sender, T value) { SendNotification(Handle, notificationId, sender, value); } public void SendNotification( ReactNotificationId notificationId, object sender) { SendNotification(Handle, notificationId, sender); } public void SendNotification( ReactNotificationId notificationId, T value) { SendNotification(Handle, notificationId, value); } public void SendNotification(ReactNotificationId notificationId) { SendNotification(Handle, notificationId); } } }