Last active
October 28, 2025 15:52
-
-
Save mrkybe/dc80e39a7a70bbb47abf773264b9a6c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class SubscriptionTracker | |
| { | |
| public string Name => _owner.name; | |
| private readonly MonoBehaviour _owner; | |
| private readonly Dictionary<(object source, Delegate handler), Action> _subscriptions = new(); | |
| public SubscriptionTracker(MonoBehaviour owner) | |
| { | |
| _owner = owner; | |
| } | |
| public (object source, EventHandler handler) TrySubscribe( | |
| object source, | |
| Action<EventHandler> subscribeAction, | |
| Action<EventHandler> unsubscribeAction, | |
| EventHandler handler) | |
| { | |
| var key = (source, handler); | |
| if (_subscriptions.ContainsKey(key)) | |
| return default; | |
| subscribeAction(handler); | |
| _subscriptions[key] = () => unsubscribeAction(handler); | |
| return key; | |
| } | |
| public (object source, EventHandler<TEventArgs> handler) TrySubscribe<TEventArgs>( | |
| object source, | |
| Action<EventHandler<TEventArgs>> subscribeAction, | |
| Action<EventHandler<TEventArgs>> unsubscribeAction, | |
| EventHandler<TEventArgs> handler) where TEventArgs : EventArgs | |
| { | |
| var key = (source, handler); | |
| if (_subscriptions.ContainsKey(key)) | |
| return default; | |
| subscribeAction(handler); | |
| _subscriptions[key] = () => unsubscribeAction(handler); | |
| return key; | |
| } | |
| public bool Unsubscribe(object source, EventHandler handler) | |
| { | |
| var key = (source, handler); | |
| bool result = false; | |
| if (_subscriptions.ContainsKey(key)) | |
| { | |
| result = true; | |
| _subscriptions[key](); | |
| } | |
| _subscriptions.Remove(key); | |
| return result; | |
| } | |
| public bool Unsubscribe<TEventArgs>(object source, EventHandler<TEventArgs> handler) where TEventArgs : EventArgs | |
| { | |
| var key = (source, handler); | |
| bool result = false; | |
| if (_subscriptions.ContainsKey(key)) | |
| { | |
| result = true; | |
| _subscriptions[key](); | |
| } | |
| _subscriptions.Remove(key); | |
| return result; | |
| } | |
| public bool Unsubscribe<TEventArgs>((object source, EventHandler<TEventArgs> handler) key) where TEventArgs : EventArgs | |
| { | |
| bool result = false; | |
| if (_subscriptions.ContainsKey(key)) | |
| { | |
| result = true; | |
| _subscriptions[key](); | |
| } | |
| _subscriptions.Remove(key); | |
| return result; | |
| } | |
| public void UnsubscribeAll() | |
| { | |
| foreach (var unsubscribe in _subscriptions.Values) | |
| { | |
| unsubscribe(); | |
| } | |
| _subscriptions.Clear(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment