using System.Collections; using System.Collections.Generic; using System.Text; using System; using System.Linq; namespace Rokojori { public class EventSlot { protected List> _actions = new List>(); List> _once = new List>(); protected bool _canModify = true; List> _additions = new List>(); List> _onceAdditions = new List>(); List> _removals = new List>(); public bool hasListeners => _once.Count > 0 || _actions.Count > 0; public void AddAction( Action action ) { if ( _canModify ) { _actions.Add( action ); } else { _additions.Add( action ); } } public void RemoveAction( Action action ) { if ( _canModify ) { _actions.Remove( action ); } else { _removals.Add( action ); } } public void Once( Action action ) { if ( _canModify ) { _actions.Add( action ); _once.Add( action ); } else { _onceAdditions.Add( action ); } } protected void _UpdateLists() { _once.ForEach( a => { _actions.Remove( a ); } ); _once.Clear(); if ( _removals.Count > 0 ) { Lists.RemoveRange( _actions, _removals ); _removals.Clear(); } if ( _additions.Count > 0 ) { _actions.AddRange( _additions ); _additions.Clear(); } if ( _onceAdditions.Count > 0 ) { _once.AddRange( _onceAdditions ); _actions.AddRange( _onceAdditions ); } } public void DispatchEvent( T t ) { _canModify = false; _actions.ForEach( a => { if ( a != null ){ a( t ); } } ); _UpdateLists(); _canModify = true; } } }