/*=============================================================================== Copyright (C) 2020 PhantomsXR Ltd. All Rights Reserved. This file is part of the XR-MOD SDK. The XR-MOD SDK cannot be copied, distributed, or made available to third-parties for commercial purposes without written permission of PhantomsXR Ltd. Contact nswell@phantomsxr.com for licensing requests. ===============================================================================*/ using System; using UnityEngine; using System.Collections.Generic; using System.Threading.Tasks; namespace Phantom.XRMOD.ActionNotification.Runtime { /// /// The central hub for posting and receiving notifications within the XRMOD system. /// It supports synchronous, asynchronous, and result-returning notifications. /// public partial class ActionNotificationCenter : IActionNotificationCenter> { private static readonly ActionNotificationCenter _DEFAULT_CENTER = new(); /// /// Releases all registered handlers. Use this to clean up the notification center. /// public void Release() { handlers.Clear(); } /// /// Gets the default instance of the notification center. /// public static ActionNotificationCenter DefaultCenter => _DEFAULT_CENTER; /// /// Adds a synchronous observer that performs an action when a specific notification is posted. /// /// The action to execute (delegate). /// The name of the notification to observe. /// /// /// ActionNotificationCenter.DefaultCenter.AddObserver(data => { /// Debug.Log($"Notification received: {data}"); /// }, "MyEventName"); /// /// public void AddObserver(Action _action, string _name) { if (string.IsNullOrEmpty(_name)) { Debug.LogError("Null name specified for notification in AddObserver."); return; } AddHandler(new SyncNotificationHandler(_name, _action)); } /// /// Adds an asynchronous observer that can be awaited when a notification is posted via PostNotificationAsync. /// /// The async function to execute. It should return a Task of object. /// The name of the notification to observe. /// /// This is useful for operations that involve networking, file I/O, or other long-running tasks. /// public void AddAsyncObserver(Func> _action, string _name) { if (string.IsNullOrEmpty(_name)) { Debug.LogError("Null name specified for notification in AddObserver."); return; } AddHandler(new AsyncNotificationHandler(_name, _action)); } /// /// Adds a synchronous observer that returns a result object. /// /// The function to execute that returns a result. /// The name of the notification to observe. public void AddObserver(Func _action, string _name) { if (string.IsNullOrEmpty(_name)) { Debug.LogError("Null name specified for notification in AddObserver."); return; } AddHandler(new SyncNotificationHandler(_name, _action)); } /// /// Removes a specific synchronous action observer from a notification. /// /// The name of the notification. /// The action to remove. public void RemoveObserver(string _name, Action _action) { RemoveHandler(_name); } /// /// Removes a specific synchronous function observer from a notification. /// /// The name of the notification. /// The function to remove. public void RemoveObserver(string _name, Func _action) { RemoveHandler(_name, new SyncNotificationHandler(_name, _action)); } /// /// Removes all observers associated with a specific notification name. /// /// The name of the notification. public void RemoveObserver(string _name) { RemoveHandler(_name); } /// /// Posts a notification to all registered observers synchronously. /// /// The name of the notification to post. /// The data to pass to observers. public void PostNotification(string _name, BaseNotificationData _object) { if (string.IsNullOrEmpty(_name)) { return; } Post(_name, _object); } /// /// Posts a notification synchronously and collects results from all observers that return a value. /// /// The name of the notification. /// The data to pass to observers. /// A list of results from observers. Returns null if name is empty. public List PostNotificationWithResult(string _name, BaseNotificationData _object) { if (string.IsNullOrEmpty(_name)) { #if DEBUG Debug.LogError("The method name is empty!."); #endif return null; } return Post(_name, _object); } /// /// Posts a notification asynchronously and awaits all async observers. /// /// The name of the notification. /// The data to pass to observers. /// A task returning a list of result objects from async observers. public async Task> PostNotificationAsync(string _name, BaseNotificationData _data) { if (string.IsNullOrEmpty(_name)) return null; return await PostAsync(_name, _data); } } }