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 ResetAndClearAll() { _actions.Clear(); _once.Clear(); _additions.Clear(); _onceAdditions.Clear(); _removals.Clear(); _canModify = true; } public void AddAction( Action action ) { if ( HasAction( action ) ) { RJLog.Error( this, "This action already exists", action ); return; } if ( _canModify ) { _actions.Add( action ); } else { _additions.Add( action ); } } public void RemoveAction( Action action ) { if ( _canModify ) { var removed = _actions.Remove( action ); // RJLog.Log( "Removed:", action, removed ); } else { _removals.Add( action ); } } public bool HasAction( Action action ) { return _actions.Has( action ) || _additions.Has( action ) || _onceAdditions.Has( action ); } public void Once( Action action ) { if ( HasAction( action ) ) { RJLog.Error( this, "This action already exists", action ); return; } 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.ForEach( // r => // { // var removed = _actions.Remove( r ); // RJLog.Log( "Removed:", r, removed ); // } // ); _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; } } }