-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeout.cs
More file actions
30 lines (28 loc) · 986 Bytes
/
Timeout.cs
File metadata and controls
30 lines (28 loc) · 986 Bytes
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
using System;
using System.Threading;
using UnityEngine;
public readonly struct Timeout {
public Timeout(Action callback, float seconds, MonoBehaviour invoker) {
ImplWToken(callback, seconds, invoker.destroyCancellationToken);
}
async readonly void ImplWToken(Action callback, float seconds, CancellationToken token) {
try {
if (token.IsCancellationRequested) return;
await Awaitable.WaitForSecondsAsync(seconds, token);
if (token.IsCancellationRequested) return;
callback?.Invoke();
} catch {}
}
/// <summary>
/// USE CONTSTRUCTOR WITH TOKEN PARAMETER IF POSSIBLE
/// </summary>
public Timeout(Action callback, float seconds) {
ImplNoToken(callback, seconds);
}
async readonly void ImplNoToken(Action callback, float seconds) {
try {
await Awaitable.WaitForSecondsAsync(seconds);
callback?.Invoke();
} catch {}
}
}