| | 1 | | /* |
| | 2 | | * Copyright 2017 Stanislav Muhametsin. All rights Reserved. |
| | 3 | | * |
| | 4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
| | 5 | | * you may not use this file except in compliance with the License. |
| | 6 | | * You may obtain a copy of the License at |
| | 7 | | * |
| | 8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
| | 9 | | * |
| | 10 | | * Unless required by applicable law or agreed to in writing, software |
| | 11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | 12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| | 13 | | * implied. |
| | 14 | | * |
| | 15 | | * See the License for the specific language governing permissions and |
| | 16 | | * limitations under the License. |
| | 17 | | */ |
| | 18 | | using AsyncEnumeration.Abstractions; |
| | 19 | | using AsyncEnumeration.Implementation.Enumerable; |
| | 20 | | using System; |
| | 21 | | using System.Collections.Generic; |
| | 22 | | using System.Text; |
| | 23 | | using System.Threading; |
| | 24 | | using System.Threading.Tasks; |
| | 25 | | using UtilPack; |
| | 26 | |
|
| | 27 | | namespace AsyncEnumeration |
| | 28 | | { |
| | 29 | |
|
| | 30 | | namespace Implementation.Enumerable |
| | 31 | | { |
| | 32 | | //internal sealed class SingletonEnumerator<T> : IAsyncEnumerator<T> |
| | 33 | | //{ |
| | 34 | | // private const Int32 STATE_INITIAL = 0; |
| | 35 | | // private const Int32 STATE_WAIT_CALLED = 1; |
| | 36 | | // private const Int32 STATE_GET_CALLED = 2; |
| | 37 | |
|
| | 38 | | // private readonly T _value; |
| | 39 | | // private Int32 _state; |
| | 40 | |
|
| | 41 | | // public SingletonEnumerator( T value ) |
| | 42 | | // => this._value = value; |
| | 43 | |
|
| | 44 | | // public Task<Boolean> WaitForNextAsync() |
| | 45 | | // => TaskUtils.TaskFromBoolean( Interlocked.CompareExchange( ref this._state, STATE_WAIT_CALLED, STATE_INITI |
| | 46 | |
|
| | 47 | | // public T TryGetNext( out Boolean success ) |
| | 48 | | // { |
| | 49 | | // success = Interlocked.CompareExchange( ref this._state, STATE_GET_CALLED, STATE_WAIT_CALLED ) == STATE_WAI |
| | 50 | | // return success ? this._value : default; |
| | 51 | | // } |
| | 52 | |
|
| | 53 | | // public Task DisposeAsync() |
| | 54 | | // => TaskUtils.CompletedTask; |
| | 55 | | //} |
| | 56 | |
|
| | 57 | | internal sealed class AsyncSingletonEnumerator<T> : IAsyncEnumerator<T> |
| | 58 | | { |
| | 59 | | private const Int32 STATE_INITIAL = 0; |
| | 60 | | private const Int32 STATE_WAIT_CALLED = 1; |
| | 61 | | private const Int32 STATE_GET_CALLED = 2; |
| | 62 | |
|
| | 63 | | private readonly Task<T> _asyncValue; |
| | 64 | | private Int32 _state; |
| | 65 | |
|
| | 66 | | public AsyncSingletonEnumerator( Task<T> asyncValue ) |
| | 67 | | => this._asyncValue = ArgumentValidator.ValidateNotNull( nameof( asyncValue ), asyncValue ); |
| | 68 | |
|
| | 69 | | public Task<Boolean> WaitForNextAsync() |
| | 70 | | { |
| | 71 | | Task<Boolean> retVal; |
| | 72 | | if ( Interlocked.CompareExchange( ref this._state, STATE_WAIT_CALLED, STATE_INITIAL ) == STATE_INITIAL ) |
| | 73 | | { |
| | 74 | | if ( this._asyncValue.IsCompleted ) |
| | 75 | | { |
| | 76 | | retVal = TaskUtils.True; |
| | 77 | | } |
| | 78 | | else |
| | 79 | | { |
| | 80 | | retVal = this.ReallyWaitForNextAsync(); |
| | 81 | | } |
| | 82 | | } |
| | 83 | | else |
| | 84 | | { |
| | 85 | | retVal = TaskUtils.False; |
| | 86 | | } |
| | 87 | | return retVal; |
| | 88 | | } |
| | 89 | |
|
| | 90 | | public T TryGetNext( out Boolean success ) |
| | 91 | | { |
| | 92 | | success = Interlocked.CompareExchange( ref this._state, STATE_GET_CALLED, STATE_WAIT_CALLED ) == STATE_WAIT_ |
| | 93 | | return success ? this._asyncValue.Result : default; |
| | 94 | | } |
| | 95 | |
|
| | 96 | | public Task DisposeAsync() |
| | 97 | | => TaskUtils.CompletedTask; |
| | 98 | |
|
| | 99 | | private async Task<Boolean> ReallyWaitForNextAsync() |
| | 100 | | { |
| | 101 | | await this._asyncValue; |
| | 102 | | return true; |
| | 103 | | } |
| | 104 | | } |
| | 105 | |
|
| | 106 | |
|
| | 107 | | internal sealed class ValueTaskAsyncSingletonEnumerator<T> : IAsyncEnumerator<T> |
| | 108 | | { |
| | 109 | | private const Int32 STATE_INITIAL = 0; |
| | 110 | | private const Int32 STATE_WAIT_CALLED = 1; |
| | 111 | | private const Int32 STATE_GET_CALLED = 2; |
| | 112 | |
|
| | 113 | | private readonly ValueTask<T> _asyncValue; |
| | 114 | | private Int32 _state; |
| | 115 | |
|
| 1 | 116 | | public ValueTaskAsyncSingletonEnumerator( ValueTask<T> asyncValue ) |
| | 117 | | { |
| 1 | 118 | | this._asyncValue = asyncValue; |
| 1 | 119 | | } |
| | 120 | |
|
| | 121 | | public Task<Boolean> WaitForNextAsync() |
| | 122 | | { |
| | 123 | | Task<Boolean> retVal; |
| 2 | 124 | | if ( Interlocked.CompareExchange( ref this._state, STATE_WAIT_CALLED, STATE_INITIAL ) == STATE_INITIAL ) |
| | 125 | | { |
| 1 | 126 | | if ( this._asyncValue.IsCompleted ) |
| | 127 | | { |
| 0 | 128 | | retVal = TaskUtils.True; |
| 0 | 129 | | } |
| | 130 | | else |
| | 131 | | { |
| 1 | 132 | | retVal = this.ReallyWaitForNextAsync(); |
| | 133 | | } |
| 1 | 134 | | } |
| | 135 | | else |
| | 136 | | { |
| 1 | 137 | | retVal = TaskUtils.False; |
| | 138 | | } |
| 2 | 139 | | return retVal; |
| | 140 | | } |
| | 141 | |
|
| | 142 | | public T TryGetNext( out Boolean success ) |
| | 143 | | { |
| 2 | 144 | | success = Interlocked.CompareExchange( ref this._state, STATE_GET_CALLED, STATE_WAIT_CALLED ) == STATE_WAIT_ |
| 2 | 145 | | return success ? this._asyncValue.Result : default; |
| | 146 | | } |
| | 147 | |
|
| | 148 | | public Task DisposeAsync() => |
| 1 | 149 | | TaskUtils.CompletedTask; |
| | 150 | |
|
| | 151 | | private async Task<Boolean> ReallyWaitForNextAsync() |
| | 152 | | { |
| 1 | 153 | | await this._asyncValue; |
| 1 | 154 | | return true; |
| 1 | 155 | | } |
| | 156 | | } |
| | 157 | | } |
| | 158 | |
|
| | 159 | |
|
| | 160 | | public static partial class AsyncEnumerationExtensions |
| | 161 | | { |
| | 162 | | /// <summary> |
| | 163 | | /// Encapsulates this single value as <see cref="IAsyncEnumerable{T}"/> containing only this value. |
| | 164 | | /// </summary> |
| | 165 | | /// <typeparam name="T">The type of this value.</typeparam> |
| | 166 | | /// <param name="value">This value</param> |
| | 167 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | 168 | | /// <returns><see cref="IAsyncEnumerable{T}"/> containing only this value.</returns> |
| | 169 | | public static IAsyncEnumerable<T> AsSingletonAsync<T>( |
| | 170 | | this T value, |
| | 171 | | IAsyncProvider asyncProvider |
| | 172 | | ) |
| | 173 | | { |
| | 174 | | return AsyncEnumerable.Repeat( value, 1, asyncProvider ); |
| | 175 | | } |
| | 176 | |
|
| | 177 | | /// <summary> |
| | 178 | | /// Encapsulates this asynchronous value as <see cref="IAsyncEnumerable{T}"/> containing only this value. |
| | 179 | | /// </summary> |
| | 180 | | /// <typeparam name="T">The type of this value.</typeparam> |
| | 181 | | /// <param name="task">The task acquiring this value.</param> |
| | 182 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | 183 | | /// <returns><see cref="IAsyncEnumerable{T}"/> containing only this value.</returns> |
| | 184 | | /// <exception cref="NullReferenceException">If this <see cref="Task{TResult}"/> is <c>null</c>.</exception> |
| | 185 | | public static IAsyncEnumerable<T> AsSingletonAsync<T>( |
| | 186 | | this Task<T> task, |
| | 187 | | IAsyncProvider asyncProvider |
| | 188 | | ) => AsyncEnumerationFactory.FromGeneratorCallback( ArgumentValidator.ValidateNotNullReference( task ), t => ne |
| | 189 | |
|
| | 190 | | /// <summary> |
| | 191 | | /// Encapsulates this asynchronous value as <see cref="IAsyncEnumerable{T}"/> containing only this value. |
| | 192 | | /// </summary> |
| | 193 | | /// <typeparam name="T">The type of this value.</typeparam> |
| | 194 | | /// <param name="task">The task acquiring this value.</param> |
| | 195 | | /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/ |
| | 196 | | /// <returns><see cref="IAsyncEnumerable{T}"/> containing only this value.</returns> |
| | 197 | | public static IAsyncEnumerable<T> AsSingletonAsync<T>( |
| | 198 | | this ValueTask<T> task, |
| | 199 | | IAsyncProvider asyncProvider |
| | 200 | | ) => AsyncEnumerationFactory.FromGeneratorCallback( task, t => new ValueTaskAsyncSingletonEnumerator<T>( t ), a |
| | 201 | |
|
| | 202 | | } |
| | 203 | | } |