Last active
April 11, 2020 20:02
-
-
Save TheFo2sh/e91ca3c048c6a76a797321112277d6a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TaskAsyncEnumerable<T> : IAsyncEnumerable<T> | |
| { | |
| private Func<Task<Option<T>>> _asyncTask; | |
| private TimeSpan timeSpan; | |
| public TaskAsyncEnumerable(Func<Task<Option<T>>> asyncTask) | |
| { | |
| _asyncTask = asyncTask; | |
| } | |
| public TaskAsyncEnumerable(Func<Task<Option<T>>> taskOption, TimeSpan timeSpan) | |
| { | |
| this._asyncTask = taskOption; | |
| this.timeSpan = timeSpan; | |
| } | |
| public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = new CancellationToken()) | |
| { | |
| return new TaskAsyncEnumerator<T>(_asyncTask,timeSpan,cancellationToken); | |
| } | |
| } | |
| public class TaskAsyncEnumerator<T> : IAsyncEnumerator<T> | |
| { | |
| private Func<Task<Option<T>>> _asyncTask; | |
| private readonly CancellationToken _cancellationToken; | |
| private TimeSpan _timeSpan; | |
| public TaskAsyncEnumerator(Func<Task<Option<T>>> asyncTask, CancellationToken cancellationToken) | |
| { | |
| _asyncTask = asyncTask; | |
| _cancellationToken = cancellationToken; | |
| } | |
| public TaskAsyncEnumerator(Func<Task<Option<T>>> asyncTask,TimeSpan timeSpan, CancellationToken cancellationToken) | |
| { | |
| _timeSpan = timeSpan; | |
| _asyncTask = asyncTask; | |
| _cancellationToken = cancellationToken; | |
| } | |
| public ValueTask DisposeAsync() | |
| { | |
| return new ValueTask(Task.CompletedTask); | |
| } | |
| public async ValueTask<bool> MoveNextAsync() | |
| { | |
| if (_timeSpan != TimeSpan.Zero) | |
| await Task.Delay(_timeSpan,_cancellationToken); | |
| var result=await _asyncTask.Invoke(); | |
| result.MatchSome(obj =>Current= obj); | |
| return result.HasValue; | |
| } | |
| public T Current { get; private set; } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment