rj-action-library/Runtime/Events/EventSlot.cs

49 lines
939 B
C#

using System.Collections;
using System.Collections.Generic;
using System.Text;
using System;
using System.Linq;
namespace Rokojori
{
public class EventSlot<T>
{
protected List<Action<T>> _actions = new List<Action<T>>();
List<Action<T>> _once = new List<Action<T>>();
public bool hasListeners => _once.Count > 0 || _actions.Count > 0;
public void AddAction( Action<T> action )
{
_actions.Add( action );
}
public void RemoveAction( Action<T> action )
{
_actions.Remove( action );
}
public void Once( Action<T> action )
{
_actions.Add( action );
_once.Add( action );
}
protected void _ClearOnceActions()
{
_once.ForEach( a => { _actions.Remove( a ); } );
_once.Clear();
}
public void DispatchEvent( T t )
{
_actions.ForEach( a => { if ( a != null ){ a( t ); } } );
_ClearOnceActions();
}
}
}