Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- // This initially was my first Chat GPT experiment... I should have done that myself and saved a couple of hours :)
- /// <summary>
- /// An event that supposed to be fired once, and after that any subscription will be fired immediately. <code>
- /// private static OneTimeEvent onHadInitialized = new OneTimeEvent();
- /// public static event Action OnHadInitialized { add => onHadInitialized += value; remove => onHadInitialized -= value; }
- /// </code>
- /// </summary>
- public class OneTimeEvent
- {
- private event Action _event;
- private bool _wasInvoked;
- private static object _staticlock = new object();
- private object _lock = new object();
- public bool WasInvoked
- {
- get
- {
- lock (_lock)
- {
- return _wasInvoked;
- }
- }
- }
- public static OneTimeEvent operator +(OneTimeEvent oneTimeEvent, Action handler)
- {
- lock (_staticlock)
- {
- if (oneTimeEvent == null)
- {
- oneTimeEvent = new OneTimeEvent();
- return Subscribe(oneTimeEvent, handler);
- }
- }
- lock (oneTimeEvent._lock)
- {
- return Subscribe(oneTimeEvent, handler);
- }
- static OneTimeEvent Subscribe(OneTimeEvent oneTimeEvent, Action handler)
- {
- if (oneTimeEvent._wasInvoked)
- {
- handler?.Invoke();
- }
- else
- {
- oneTimeEvent._event += handler;
- }
- return oneTimeEvent;
- }
- }
- public static OneTimeEvent operator -(OneTimeEvent oneTimeEvent, Action handler)
- {
- lock (_staticlock)
- {
- if (oneTimeEvent == null)
- {
- oneTimeEvent = new OneTimeEvent();
- return Unsubscribe(oneTimeEvent, handler);
- }
- }
- lock (oneTimeEvent._lock)
- {
- return Unsubscribe(oneTimeEvent, handler);
- }
- static OneTimeEvent Unsubscribe(OneTimeEvent oneTimeEvent, Action handler)
- {
- if (oneTimeEvent._wasInvoked)
- {
- handler?.Invoke();
- }
- else
- {
- oneTimeEvent._event -= handler;
- }
- return oneTimeEvent;
- }
- }
- public void Invoke()
- {
- Action temp;
- lock (_lock)
- {
- if (_wasInvoked)
- {
- return;
- }
- _wasInvoked = true;
- temp = _event;
- _event = null;
- }
- temp?.Invoke();
- }
- public IAsyncResult BeginInvoking()
- {
- Action temp;
- lock (_lock)
- {
- if (_wasInvoked)
- {
- return null;
- }
- _wasInvoked = true;
- temp = _event;
- _event = null;
- }
- return temp?.BeginInvoke(temp.EndInvoke, null);
- }
- }
- /// <summary>
- /// An event that supposed to be fired once, and after that any subscription will be fired immediately.<para/>
- /// Thanks Chat GPT to helping improving it... Although, I should have done that myself and saved a couple of hours
- /// </summary>
- public class OneTimeEvent<T>
- {
- private Action<T> _event;
- private T _arg;
- private bool _wasInvoked;
- private static object _staticlock = new object();
- private object _lock = new object();
- public bool WasInvoked
- {
- get
- {
- lock (_lock)
- {
- return _wasInvoked;
- }
- }
- }
- public static OneTimeEvent<T> operator +(OneTimeEvent<T> oneTimeEvent, Action<T> handler)
- {
- lock (_staticlock)
- {
- if (oneTimeEvent == null)
- {
- oneTimeEvent = new OneTimeEvent<T>();
- return Subscribe(oneTimeEvent, handler);
- }
- }
- lock (oneTimeEvent._lock)
- {
- return Subscribe(oneTimeEvent, handler);
- }
- static OneTimeEvent<T> Subscribe(OneTimeEvent<T> oneTimeEvent, Action<T> handler)
- {
- if (oneTimeEvent._wasInvoked)
- {
- handler?.Invoke(oneTimeEvent._arg);
- }
- else
- {
- oneTimeEvent._event += handler;
- }
- return oneTimeEvent;
- }
- }
- public static OneTimeEvent<T> operator -(OneTimeEvent<T> oneTimeEvent, Action<T> handler)
- {
- lock (_staticlock)
- {
- if (oneTimeEvent == null)
- {
- oneTimeEvent = new OneTimeEvent<T>();
- return Unsubscribe(oneTimeEvent, handler);
- }
- }
- lock (oneTimeEvent._lock)
- {
- return Unsubscribe(oneTimeEvent, handler);
- }
- static OneTimeEvent<T> Unsubscribe(OneTimeEvent<T> oneTimeEvent, Action<T> handler)
- {
- if (oneTimeEvent._wasInvoked)
- {
- handler?.Invoke(oneTimeEvent._arg);
- }
- else
- {
- oneTimeEvent._event -= handler;
- }
- return oneTimeEvent;
- }
- }
- public void Invoke(T arg)
- {
- Action<T> temp;
- lock (_lock)
- {
- if (_wasInvoked)
- {
- return;
- }
- _wasInvoked = true;
- temp = _event;
- _event = null;
- }
- temp?.Invoke(arg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement