public static IDisposableItem AsDisposable(this RoutineHandle handler)
        {
            var disposable = ClassPool.Spawn <DisposableAction>();

            disposable.Initialize(() => UniRoutineManager.TryToStopRoutine(handler));
            return(disposable);
        }
        public static IDisposableItem AddTo(this RoutineHandle handler, ICollection <IDisposable> collection)
        {
            var disposable = handler.AsDisposable();

            collection.Add(disposable);
            return(disposable);
        }
        /// <summary>
        /// start uniroutine interator
        /// </summary>
        /// <param name="enumerator">target enumerator</param>
        /// <param name="routineType">routine type</param>
        /// <param name="scope">routine execution scope</param>
        /// <param name="moveNextImmediately"></param>
        /// <returns>cancelation handle</returns>
        public static RoutineHandle RunUniRoutine(
            IEnumerator enumerator,
            RoutineType routineType,
            RoutineScope scope,
            bool moveNextImmediately = true)
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                return(new RoutineHandle());
            }
#endif

            var routineObject = GetRoutineObject(scope);
            //get routine
            var routine = routineObject.GetRoutine(routineType);
            //add enumerator to routines
            var routineTask = routine.AddRoutine(enumerator, moveNextImmediately);
            if (routineTask == null)
            {
                return(new RoutineHandle(0, routineType, scope));
            }

            var routineValue = new RoutineHandle(routineTask.Id, routineType, scope);
            return(routineValue);
        }
        public static RoutineHandle WithFinally(this RoutineHandle handle, Action action)
        {
            if (action == null)
            {
                return(handle);
            }

            UniRoutineManager.AddFinally(handle, action);
            return(handle);
        }
        public static bool AddFinally(RoutineHandle handler, Action action)
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                return(false);
            }
#endif
            //get routine
            var scope   = GetRoutineObject(handler.Scope);
            var routine = scope.GetRoutine(handler.Type);
            return(routine.AddFinally(handler.Id, action));
        }
        public static bool IsRoutineActive(RoutineHandle handler)
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                return(false);
            }
#endif
            //get routine
            var scope   = GetRoutineObject(handler.Scope);
            var routine = scope.GetRoutine(handler.Type);
            return(routine.IsActive(handler.Id));
        }
        public static bool TryToStopRoutine(RoutineHandle handler)
        {
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                return(false);
            }
#endif
            //get routine
            var scope   = GetRoutineObject(handler.Scope);
            var routine = scope.GetRoutine(handler.Type);
            //add enumerator to routines
            return(routine.CancelRoutine(handler.Id));
        }
 public static RoutineHandle AddTo(this RoutineHandle handler, ILifeTime lifeTime)
 {
     lifeTime.AddCleanUpAction(() => handler.Cancel());
     return(handler);
 }
 public static ILifeTime AddTo(this RoutineHandle handle, Component component)
 {
     return(component.AddTo(() => handle.Cancel()));
 }
 public static bool IsActive(this RoutineHandle handler)
 {
     return(UniRoutineManager.IsRoutineActive(handler));
 }
 public static bool Cancel(this RoutineHandle handler)
 {
     return(UniRoutineManager.TryToStopRoutine(handler));
 }