Skip to content

Commit 116a3d8

Browse files
committed
Remove AwakeMonitor
Awake monitor is only necessary for UniTask due to how Unity Monobehaviours functioned. Specifically, MonoBehaviours would not call OnDestroy if they were not awaked first. Therefore UniTask had to rely on checking if the MonoBehaviour was set to null to detect if the MonoBehavior was destroyed. However in Godot, Predelete is always called when a node is freed, so there's no need to handle any edge cases. See Cysharp/UniTask#435 the use of AwakeMonitor in UniTask.
1 parent 8e15e47 commit 116a3d8

5 files changed

+43
-101
lines changed

addons/GDTask/Triggers/AsyncDestroyTrigger.cs

+4-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using System.Threading;
2-
using Godot;
1+
using Godot;
2+
using System.Threading;
33

44
namespace Fractural.Tasks.Triggers
55
{
@@ -13,7 +13,7 @@ public static AsyncDestroyTrigger GetAsyncDestroyTrigger(this Node node)
1313

1414
public sealed partial class AsyncDestroyTrigger : Node
1515
{
16-
bool awakeCalled = false;
16+
bool enterTreeCalled = false;
1717
bool called = false;
1818
CancellationTokenSource cancellationTokenSource;
1919

@@ -26,18 +26,13 @@ public CancellationToken CancellationToken
2626
cancellationTokenSource = new CancellationTokenSource();
2727
}
2828

29-
if (!awakeCalled)
30-
{
31-
GDTaskPlayerLoopAutoload.AddAction(PlayerLoopTiming.Process, new AwakeMonitor(this));
32-
}
33-
3429
return cancellationTokenSource.Token;
3530
}
3631
}
3732

3833
public override void _EnterTree()
3934
{
40-
awakeCalled = true;
35+
enterTreeCalled = true;
4136
}
4237

4338
public override void _Notification(int what)
@@ -69,27 +64,6 @@ public GDTask OnDestroyAsync()
6964

7065
return tcs.Task;
7166
}
72-
73-
class AwakeMonitor : IPlayerLoopItem
74-
{
75-
readonly AsyncDestroyTrigger trigger;
76-
77-
public AwakeMonitor(AsyncDestroyTrigger trigger)
78-
{
79-
this.trigger = trigger;
80-
}
81-
82-
public bool MoveNext()
83-
{
84-
if (trigger.called) return false;
85-
if (trigger == null)
86-
{
87-
trigger.OnDestroy();
88-
return false;
89-
}
90-
return true;
91-
}
92-
}
9367
}
9468
}
9569

addons/GDTask/Triggers/AsyncEnterTreeTrigger.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using System.Threading;
2-
using Godot;
1+
using Godot;
32

43
namespace Fractural.Tasks.Triggers
54
{
65
public static partial class AsyncTriggerExtensions
76
{
8-
public static AsyncEnterTreeTrigger GetAsyncAwakeTrigger(this Node node)
7+
public static AsyncEnterTreeTrigger GetAsyncEnterTreeTrigger(this Node node)
98
{
109
return node.GetOrAddImmediateChild<AsyncEnterTreeTrigger>();
1110
}
@@ -19,7 +18,7 @@ public override void _EnterTree()
1918
RaiseEvent(AsyncUnit.Default);
2019
}
2120

22-
public GDTask AwakeAsync()
21+
public GDTask EnterTreeAsync()
2322
{
2423
if (calledEnterTree) return GDTask.CompletedTask;
2524

addons/GDTask/Triggers/AsyncReadyTrigger.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Fractural.Tasks.Triggers
44
{
55
public static partial class AsyncTriggerExtensions
66
{
7-
public static AsyncReadyTrigger GetAsyncStartTrigger(this Node node)
7+
public static AsyncReadyTrigger GetAsyncReadyTrigger(this Node node)
88
{
99
return node.GetOrAddImmediateChild<AsyncReadyTrigger>();
1010
}
@@ -21,7 +21,7 @@ public override void _Ready()
2121
RaiseEvent(AsyncUnit.Default);
2222
}
2323

24-
public GDTask StartAsync()
24+
public GDTask ReadyAsync()
2525
{
2626
if (called) return GDTask.CompletedTask;
2727

addons/GDTask/Triggers/AsyncTriggerBase.cs

+2-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System;
1+
using Godot;
2+
using System;
23
using System.Threading;
3-
using Godot;
44

55
namespace Fractural.Tasks.Triggers
66
{
@@ -32,49 +32,18 @@ void OnDestroy()
3232

3333
internal void AddHandler(ITriggerHandler<T> handler)
3434
{
35-
if (!calledEnterTree)
36-
{
37-
GDTaskPlayerLoopAutoload.AddAction(PlayerLoopTiming.Process, new AwakeMonitor(this));
38-
}
39-
4035
triggerEvent.Add(handler);
4136
}
4237

4338
internal void RemoveHandler(ITriggerHandler<T> handler)
4439
{
45-
if (!calledEnterTree)
46-
{
47-
GDTaskPlayerLoopAutoload.AddAction(PlayerLoopTiming.Process, new AwakeMonitor(this));
48-
}
49-
5040
triggerEvent.Remove(handler);
5141
}
5242

5343
protected void RaiseEvent(T value)
5444
{
5545
triggerEvent.SetResult(value);
5646
}
57-
58-
class AwakeMonitor : IPlayerLoopItem
59-
{
60-
readonly AsyncTriggerBase<T> trigger;
61-
62-
public AwakeMonitor(AsyncTriggerBase<T> trigger)
63-
{
64-
this.trigger = trigger;
65-
}
66-
67-
public bool MoveNext()
68-
{
69-
if (trigger.calledEnterTree) return false;
70-
if (trigger == null)
71-
{
72-
trigger.OnDestroy();
73-
return false;
74-
}
75-
return true;
76-
}
77-
}
7847
}
7948

8049
public interface IAsyncOneShotTrigger

addons/GDTask/Triggers/AsyncTriggerExtensions.cs

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System;
2-
using System.Threading;
3-
using Fractural.Tasks.Triggers;
1+
using Fractural.Tasks.Triggers;
42
using Godot;
3+
using System;
4+
using System.Threading;
55

66
namespace Fractural.Tasks
77
{
@@ -20,48 +20,48 @@ namespace Fractural.Tasks.Triggers
2020
public static partial class AsyncTriggerExtensions
2121
{
2222
// Special for single operation.
23-
public static T GetImmediateChild<T>(this Node node, bool includeRoot = true)
24-
{
25-
if (node == null) throw new ArgumentNullException(nameof(node));
26-
if (includeRoot && node is T castedRoot)
27-
return castedRoot;
28-
else
29-
{
30-
foreach (Node child in node.GetChildren())
31-
if (child is T castedChild) return castedChild;
32-
}
33-
return default(T);
34-
}
23+
public static T GetImmediateChild<T>(this Node node, bool includeRoot = true)
24+
{
25+
if (node == null) throw new ArgumentNullException(nameof(node));
26+
if (includeRoot && node is T castedRoot)
27+
return castedRoot;
28+
else
29+
{
30+
foreach (Node child in node.GetChildren())
31+
if (child is T castedChild) return castedChild;
32+
}
33+
return default(T);
34+
}
3535

36-
public static T AddImmediateChild<T>(this Node node) where T : Node, new()
37-
{
38-
T child = new T();
39-
node.AddChild(child);
40-
return child;
41-
}
36+
public static T AddImmediateChild<T>(this Node node) where T : Node, new()
37+
{
38+
T child = new T();
39+
node.AddChild(child);
40+
return child;
41+
}
4242

43-
public static T GetOrAddImmediateChild<T>(this Node node) where T : Node, new()
44-
{
45-
T child = GetImmediateChild<T>(node);
46-
if (child == null)
47-
child = AddImmediateChild<T>(node);
48-
return child;
49-
}
43+
public static T GetOrAddImmediateChild<T>(this Node node) where T : Node, new()
44+
{
45+
T child = GetImmediateChild<T>(node);
46+
if (child == null)
47+
child = AddImmediateChild<T>(node);
48+
return child;
49+
}
5050

5151
/// <summary>This function is called when the Node will be destroyed.</summary>
5252
public static GDTask OnDestroyAsync(this Node node)
5353
{
5454
return node.GetAsyncDestroyTrigger().OnDestroyAsync();
5555
}
5656

57-
public static GDTask StartAsync(this Node node)
57+
public static GDTask ReadyAsync(this Node node)
5858
{
59-
return node.GetAsyncStartTrigger().StartAsync();
59+
return node.GetAsyncReadyTrigger().ReadyAsync();
6060
}
6161

62-
public static GDTask AwakeAsync(this Node node)
62+
public static GDTask EnterTreeAsync(this Node node)
6363
{
64-
return node.GetAsyncAwakeTrigger().AwakeAsync();
64+
return node.GetAsyncEnterTreeTrigger().EnterTreeAsync();
6565
}
6666
}
6767
}

0 commit comments

Comments
 (0)