-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDispatcher.cs
112 lines (101 loc) · 3.09 KB
/
Dispatcher.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Linq;
using System.Management.Instrumentation;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
namespace UEx
{
public class Dispatcher : MonoBehaviour
{
readonly ConcurrentQueue<Action> _actions = new ConcurrentQueue<Action>();
private static Dispatcher _current;
public static Dispatcher Current { get { return _current; } }
public static async Task InvokeAsync(Action action)
{
if (_current == null)
throw new Exception("please run Dispatcher.Initialize() or attach the component to a gameobject in the starting scene");
var tks = new TaskCompletionSource<bool>();
_current._actions.Enqueue(() =>
{
try
{
action();
}
catch (Exception e)
{
Debug.LogError(e);
}
tks.SetResult(true);
});
await tks.Task;
}
public static void Initialize()
{
if (!Application.isPlaying)
return;
if (_current != null) return;
var g = new GameObject("Dispatcher");
var d = g.AddComponent<Dispatcher>();
d.InitializeAsThis();
}
void InitializeAsThis()
{
_current = this;
DontDestroyOnLoad(this);
}
void Awake()
{
if (_current == null)
InitializeAsThis();
else
Destroy(this);
}
void Update()
{
Action action;
while (_actions.TryDequeue(out action))
{
if (action == null)
break;
action();
}
}
void OnDestroy()
{
if (_current == this)
{
_current = null;
}
}
}
public static class TaskExtensions
{
/// <summary>
/// start a couroutine that waits on the specified task in a global component
/// </summary>
/// <param name="task"></param>
/// <returns></returns>
public static Coroutine Yield(this Task task)
{
return task.Yield(Dispatcher.Current);
}
/// <summary>
/// start a coroutine on the specified container that waits on the specified task
/// </summary>
/// <param name="task"></param>
/// <param name="container"></param>
/// <returns></returns>
public static Coroutine Yield(this Task task, MonoBehaviour container)
{
return container.StartCoroutine(CompletionEnumerator(task));
}
private static IEnumerator CompletionEnumerator(Task task)
{
while (!task.IsCompleted)
yield return null;
}
}
}