This repository has been archived by the owner on Mar 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTaskChain.cs
91 lines (80 loc) · 2.48 KB
/
TaskChain.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Linq;
using System.Collections.Generic;
namespace DepthFirstScheduler
{
public enum ChainStatus
{
Unknown,
Continue,
Done,
Error,
}
public class TaskChain
{
public IEnumerator<ISchedulable> Enumerator;
public Action<Exception> OnError;
public ChainStatus ChainStatus;
public static TaskChain Schedule(ISchedulable schedulable, Action<Exception> onError)
{
var item = new TaskChain
{
Enumerator = schedulable.Traverse().GetEnumerator(),
OnError = onError
};
if (item.Enumerator.MoveNext())
{
if (item.Enumerator.Current.Schedulder == null)
{
// default
Scheduler.MainThread.Enqueue(item);
}
else
{
item.Enumerator.Current.Schedulder.Enqueue(item);
}
}
return item;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public ExecutionStatus Next()
{
if (this.ChainStatus == ChainStatus.Done
|| this.ChainStatus== ChainStatus.Error)
{
return ExecutionStatus.Done;
}
{
var status = Enumerator.Current.Execute();
if (status == ExecutionStatus.Error)
{
ChainStatus = ChainStatus.Error;
OnError(Enumerator.Current.GetError());
}
if (status == ExecutionStatus.Continue)
{
// 中断(coroutine)
ChainStatus = ChainStatus.Continue;
return ExecutionStatus.Continue;
}
}
if (!Enumerator.MoveNext())
{
// 終了
ChainStatus = ChainStatus.Done;
return ExecutionStatus.Done;
}
if (Enumerator.Current.Schedulder != null)
{
// Scheduleして中断
ChainStatus = ChainStatus.Continue;
Enumerator.Current.Schedulder.Enqueue(this);
return ExecutionStatus.Done;
}
return Next();
}
}
}