-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncProperty.cs
More file actions
42 lines (35 loc) · 929 Bytes
/
AsyncProperty.cs
File metadata and controls
42 lines (35 loc) · 929 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
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace SpatialDotNet
{
// Async property wrapper
public sealed class AsyncProperty<T>
{
private readonly Func<Task<T>> _getter;
public AsyncProperty(Func<T> factory)
{
_getter = () => Task.Run(factory);
}
public AsyncProperty(Func<Task<T>> factory)
{
_getter = () => Task.Run(factory);
}
public TaskAwaiter<T> GetAwaiter()
{
return _getter().GetAwaiter();
}
public void Start()
{
_getter().Start();
}
public static implicit operator AsyncProperty<T>(T obj)
{
return new AsyncProperty<T>(() => obj);
}
public static implicit operator T(AsyncProperty<T> obj)
{
return obj._getter().Result;
}
}
}