aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Plugins.Extensions.Loading/src/IAsyncLazy.cs
blob: 98e0ebea341ae257768e3a3b927f84d8cc7decc6 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright (c) 2023 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Extensions.Loading
* File: IAsyncLazy.cs 
*
* IAsyncLazy.cs is part of VNLib.Plugins.Extensions.Loading which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Extensions.Loading is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Affero General Public License as 
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Plugins.Extensions.Loading is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see https://www.gnu.org/licenses/.
*/


using System;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;

namespace VNLib.Plugins.Extensions.Loading
{
    /// <summary>
    /// Represents an asynchronous lazy operation. with non-blocking access to the target value.
    /// </summary>
    /// <typeparam name="T">The result type</typeparam>
    public interface IAsyncLazy<T>
    {
        /// <summary>
        /// Gets a value indicating whether the asynchronous operation has completed.
        /// </summary>
        bool Completed { get; }

        /// <summary>
        /// Gets a task that represents the asynchronous operation.
        /// </summary>
        /// <returns></returns>
        TaskAwaiter<T> GetAwaiter();

        /// <summary>
        /// Gets the target value of the asynchronous operation without blocking. 
        /// If the operation failed, throws an exception that caused the failure. 
        /// If the operation has not completed, throws an exception.
        /// </summary>
        T Value { get; }
    }

    /// <summary>
    /// Extension methods for <see cref="IAsyncLazy{T}"/>
    /// </summary>
    public static class AsyncLazyExtensions
    {
        /// <summary>
        /// Gets an <see cref="IAsyncLazy{T}"/> wrapper for the specified <see cref="Task{T}"/>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="task"></param>
        /// <returns>The async operation task wrapper</returns>
        public static IAsyncLazy<T> AsLazy<T>(this Task<T> task) => new AsyncLazy<T>(task);

        /// <summary>
        /// Tranforms one lazy operation into another using the specified handler
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TResult">The resultant type</typeparam>
        /// <param name="lazy"></param>
        /// <param name="handler">The function that will peform the transformation of the lazy result</param>
        /// <returns>A new <see cref="IAsyncLazy{T}"/> that returns the transformed type</returns>
        public static IAsyncLazy<TResult> Transform<T, TResult>(this IAsyncLazy<T> lazy, Func<T, TResult> handler)
        {
            _ = lazy ?? throw new ArgumentNullException(nameof(lazy));
            _ = handler ?? throw new ArgumentNullException(nameof(handler));

            //Await the lazy task, then pass the result to the handler
            static async Task<TResult> OnResult(IAsyncLazy<T> lazy, Func<T, TResult> cb)
            {
                T result = await lazy;
                return cb(result);
            }

            return OnResult(lazy, handler).AsLazy();
        }

#nullable disable

        private sealed class AsyncLazy<T> : IAsyncLazy<T>
        {
            private readonly Task<T> _task;

            private T _result;

            public AsyncLazy(Task<T> task)
            {
                _task = task ?? throw new ArgumentNullException(nameof(task));
                _ = task.ContinueWith(SetResult, TaskScheduler.Default);
            }

            ///<inheritdoc/>
            public bool Completed => _task.IsCompleted;

            ///<inheritdoc/>
            public T Value
            {
                get
                {
                    if (_task.IsCompletedSuccessfully)
                    {
                        return _result;
                    }
                    else if(_task.IsFaulted)
                    {
                        //Compress and raise exception from result
                        return _task.GetAwaiter().GetResult();
                    }
                    else
                    {
                        throw new InvalidOperationException("The asynchronous operation has not completed.");
                    }
                }
            }

            /*
             * Only set the result if the task completed successfully.
             */
            private void SetResult(Task<T> task)
            {
                if (task.IsCompletedSuccessfully)
                {
                    _result = task.Result;
                }
            }

            ///<inheritdoc/>
            public TaskAwaiter<T> GetAwaiter() => _task.GetAwaiter();
        }
#nullable enable

    }
}