namespace Caliburn.Micro.Core {
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
///
/// Manages coroutine execution.
///
public static class Coroutine {
static readonly ILog Log = LogManager.GetLog(typeof(Coroutine));
///
/// Creates the parent enumerator.
///
public static Func, IResult> CreateParentEnumerator = inner => new SequentialResult(inner);
///
/// Executes a coroutine.
///
/// The coroutine to execute.
/// The context to execute the coroutine within.
/// /// The completion callback for the coroutine.
public static void BeginExecute(IEnumerator coroutine, CoroutineExecutionContext context = null, EventHandler callback = null) {
Log.Info("Executing coroutine.");
var enumerator = CreateParentEnumerator(coroutine);
IoC.BuildUp(enumerator);
if (callback != null) {
ExecuteOnCompleted(enumerator, callback);
}
ExecuteOnCompleted(enumerator, Completed);
enumerator.Execute(context ?? new CoroutineExecutionContext());
}
///
/// Executes a coroutine asynchronous.
///
/// The coroutine to execute.
/// The context to execute the coroutine within.
/// A task that represents the asynchronous coroutine.
public static Task ExecuteAsync(IEnumerator coroutine, CoroutineExecutionContext context = null) {
var taskSource = new TaskCompletionSource