using System;
using System.Collections.Generic;
using NBC;
namespace NBF
{
///
/// 默认任务运行器
///
public class DefRunner : Runner
{
private static DefRunner mUpdateRunner;
public static DefRunner Scheduler => mUpdateRunner ??= new DefRunner();
private readonly List _updateRoutines = new List();
private static bool _pause;
public event Action OnUpdate;
///
/// 暂停运行器
///
public static bool Pause
{
get => _pause;
set => _pause = value;
}
public DefRunner()
{
App.OnUpdate += Update;
StartCoroutine(new RunnerProcess("DefRunner", Coroutines, ReadyTask, FlushingOperation));
}
private void StartCoroutine(IProcess process)
{
var routines = _updateRoutines;
if (!routines.Contains(process))
{
routines.Add(process);
}
}
private void Update()
{
if (Pause) return;
ExecuteRoutines(_updateRoutines);
OnUpdate?.Invoke();
}
private void ExecuteRoutines(List arr)
{
if (arr != null && arr.Count > 0)
{
for (var index = 0; index < arr.Count; index++)
{
var task = arr[index];
var st = task.Process();
if (st == NTaskStatus.Success)
{
arr.RemoveAt(index);
index--;
}
}
}
}
}
}