private void DealWithEnable(SPComponent component, IList<RadicalCoroutine> lst)
 {
     if (lst.Count > 0)
     {
         RadicalCoroutine routine;
         for (int i = 0; i < lst.Count; i++)
         {
             routine = lst[i];
             switch (routine.OperatingState)
             {
                 case RadicalCoroutineOperatingState.Active:
                     //if the routine is currently active, that means the routine was already running. This could be because it
                     //was started in Awake, in an override of OnEnable, or the routine is in a mode that does not pause it OnDisable.
                     continue;
                 case RadicalCoroutineOperatingState.Inactive:
                     if (routine.DisableMode.HasFlag(RadicalCoroutineDisableMode.ResumeOnEnable))
                     {
                         routine.Resume(component);
                     }
                     else
                     {
                         routine.OnFinished -= this.OnRoutineFinished;
                         lst.RemoveAt(i);
                         i--;
                         Debug.LogWarning("A leaked RadicalCoroutine was found and cleaned up.", component);
                     }
                     break;
                 default:
                     //somehow a finished routine made its way into the collection... remove it
                     routine.OnFinished -= this.OnRoutineFinished;
                     lst.RemoveAt(i);
                     i--;
                     Debug.LogWarning("A leaked RadicalCoroutine was found and cleaned up.", component);
                     break;
             }
         }
     }
 }
        internal void Resume(SPComponent behaviour)
        {
            if (_state != RadicalCoroutineOperatingState.Inactive) throw new System.InvalidOperationException("Failed to start RadicalCoroutine. The Coroutine is already being processed.");
            if (behaviour == null) throw new System.ArgumentNullException("behaviour");

            _state = RadicalCoroutineOperatingState.Active;
            _owner = behaviour;
            _token = behaviour.StartCoroutine(this);

            if (_stack.Count > 0 && _stack.Peek() is IPausibleYieldInstruction) (_stack.Peek() as IPausibleYieldInstruction).OnResume();
        }
        private void DealWithDisable(SPComponent component, IList<RadicalCoroutine> lst)
        {
            if (lst.Count > 0)
            {
                var arr = lst.ToArray();
                var stoppableMode = (this.gameObject.activeInHierarchy) ? RadicalCoroutineDisableMode.StopOnDisable : RadicalCoroutineDisableMode.StopOnDeactivate;
                RadicalCoroutine routine;
                for (int i = 0; i < arr.Length; i++)
                {
                    routine = arr[i];
                    if (routine.DisableMode == RadicalCoroutineDisableMode.CancelOnDeactivate || routine.DisableMode.HasFlag(RadicalCoroutineDisableMode.CancelOnDisable))
                    {
                        routine.Cancel();
                        routine.OnFinished -= this.OnRoutineFinished;
                        lst.Remove(routine);
                    }
                    else
                    {
                        if (routine.DisableMode.HasFlag(stoppableMode))
                        {
                            routine.Stop();
                            if (routine.Finished)
                            {
                                routine.OnFinished -= this.OnRoutineFinished;
                                lst.Remove(routine);
                            }
                        }
                        if (!routine.DisableMode.HasFlag(RadicalCoroutineDisableMode.ResumeOnEnable))
                        {
                            routine.OnFinished -= this.OnRoutineFinished;
                            lst.Remove(routine);
                        }
                    }
                }
            }

            if(lst.Count == 0)
            {
                _routines.Remove(component);
                component.OnEnabled -= this.OnComponentEnabled;
                component.OnDisabled -= this.OnComponentDisabled;
                component.ComponentDestroyed -= this.OnComponentDestroyed;
            }
        }
 private void DealWithDestroy(SPComponent component, IList<RadicalCoroutine> lst)
 {
     if (lst.Count > 0)
     {
         foreach (var routine in lst)
         {
             routine.Cancel();
             routine.OnFinished -= this.OnRoutineFinished;
         }
     }
     lst.Clear();
     _routines.Remove(component);
     component.OnEnabled -= this.OnComponentEnabled;
     component.OnDisabled -= this.OnComponentDisabled;
     component.ComponentDestroyed -= this.OnComponentDestroyed;
 }
        internal void RegisterCoroutine(SPComponent component, RadicalCoroutine routine)
        {
            if (_routines.Contains(routine)) return;

            if(!_routines.ContainsKey(component))
            {
                component.OnEnabled -= this.OnComponentEnabled;
                component.OnDisabled -= this.OnComponentDisabled;
                component.ComponentDestroyed -= this.OnComponentDestroyed;
                component.OnEnabled += this.OnComponentEnabled;
                component.OnDisabled += this.OnComponentDisabled;
                component.ComponentDestroyed += this.OnComponentDestroyed;
            }

            routine.OnFinished -= this.OnRoutineFinished;
            routine.OnFinished += this.OnRoutineFinished;
            _routines.Add(component, routine);
        }
 internal void PurgeCoroutines(SPComponent behaviour)
 {
     IList<RadicalCoroutine> lst;
     if (_routines.Lists.TryGetList(behaviour, out lst))
     {
         var arr = lst.ToArray();
         foreach (var r in arr)
         {
             r.Cancel();
         }
         _routines.Remove(behaviour);
     }
 }
 public IEnumerable<ManagedCoroutineInfo> GetCoroutineInfo(SPComponent behaviour)
 {
     IList<RadicalCoroutine> lst;
     if(_routines.Lists.TryGetList(behaviour, out lst))
     {
         var arr = lst.ToArray();
         foreach(var r in arr)
         {
             yield return new ManagedCoroutineInfo(behaviour, r);
         }
     }
 }