Exemple #1
0
 /// <summary>
 /// Applies specified action to each collection element asynchronously.
 /// </summary>
 /// <typeparam name="T">Type of elements in the collection.</typeparam>
 /// <param name="collection">A collection to enumerate. Cannot be <see langword="null"/>.</param>
 /// <param name="action">An action to applied for each element.</param>
 /// <param name="token">The token that can be used to cancel the enumeration.</param>
 /// <returns>The task representing asynchronous execution of this method.</returns>
 /// <exception cref="OperationCanceledException">The enumeration has been canceled.</exception>
 public static async ValueTask ForEachAsync <T>(this IAsyncEnumerable <T> collection, ValueFunc <T, CancellationToken, ValueTask> action, CancellationToken token = default)
 {
     await foreach (var item in collection.WithCancellation(token))
     {
         await action.Invoke(item, token).ConfigureAwait(false);
     }
 }
            internal async void Execute(TimeSpan dueTime, TimeSpan period, ValueFunc <CancellationToken, Task <bool> > callback)
            {
                try
                {
                    await System.Threading.Tasks.Task.Delay(dueTime, cancellation.Token).ConfigureAwait(false);

                    while (await callback.Invoke(cancellation.Token).ConfigureAwait(false))
                    {
                        await System.Threading.Tasks.Task.Delay(period, cancellation.Token).ConfigureAwait(false);
                    }
                    TrySetResult(true);
                }
                catch (OperationCanceledException)
                {
                    TrySetResult(false);
                }
                catch (Exception e)
                {
                    TrySetException(e);
                }
                finally
                {
                    cancellation.Dispose();
                }
            }
Exemple #3
0
        /// <summary>
        /// Returns the first element in a sequence that satisfies a specified condition.
        /// </summary>
        /// <typeparam name="T">The type of the elements of source.</typeparam>
        /// <param name="seq">A collection to return an element from.</param>
        /// <param name="filter">A function to test each element for a condition.</param>
        /// <param name="token">The token that can be used to cancel enumeration.</param>
        /// <returns>The first element in the sequence that matches to the specified filter; or empty value.</returns>
        /// <exception cref="OperationCanceledException">The operation has been canceled.</exception>
        public static async ValueTask <Optional <T> > FirstOrEmptyAsync <T>(this IAsyncEnumerable <T> seq, ValueFunc <T, bool> filter, CancellationToken token = default)
            where T : notnull
        {
            await foreach (var item in seq.WithCancellation(token))
            {
                if (filter.Invoke(item))
                {
                    return(item);
                }
            }

            return(Optional <T> .None);
        }
Exemple #4
0
            private void Execute()
            {
                try
                {
                    switch (state)
                    {
                    case BeginInitState:
                        voidAwaiter = System.Threading.Tasks.Task.Delay(dueTime, cancellation.Token).ConfigureAwait(false).GetAwaiter();
                        if (voidAwaiter.IsCompleted)
                        {
                            goto case EndInitState;
                        }
                        state = EndInitState;
                        voidAwaiter.OnCompleted(continuation);
                        break;

                    case EndInitState:
                        voidAwaiter.GetResult();
                        voidAwaiter = default;
                        goto case BeginLoopState;

                    case BeginLoopState:
                        boolAwaiter = callback.Invoke(cancellation.Token).ConfigureAwait(false).GetAwaiter();
                        if (boolAwaiter.IsCompleted)
                        {
                            goto case EndLoopState;
                        }
                        state = EndLoopState;
                        boolAwaiter.OnCompleted(continuation);
                        break;

                    case EndLoopState:
                        var success = boolAwaiter.GetResult();
                        boolAwaiter = default;
                        if (success)
                        {
                            goto case BeginDelayState;
                        }
                        TrySetResult(true);
                        break;

                    case BeginDelayState:
                        voidAwaiter = System.Threading.Tasks.Task.Delay(period, cancellation.Token).ConfigureAwait(false).GetAwaiter();
                        if (voidAwaiter.IsCompleted)
                        {
                            goto case EndDelayState;
                        }
                        state = EndDelayState;
                        voidAwaiter.OnCompleted(continuation);
                        break;

                    case EndDelayState:
                        voidAwaiter.GetResult();
                        voidAwaiter = default;
                        goto case BeginLoopState;
                    }
                }
                catch (OperationCanceledException)
                {
                    TrySetResult(false);
                }
                catch (Exception e)
                {
                    TrySetException(e);
                }

                if (Task.IsCompleted)
                {
                    cancellation.Dispose();
                }
            }
Exemple #5
0
 public TResult Invoke(T item, int index) => selector.Invoke(item);
Exemple #6
0
 internal TWrapper WrapBuffer <TWrapper>(ValueFunc <T[], int, TWrapper> factory)
 => factory.Invoke(buffer, position);
Exemple #7
0
 public void Execute()
 {
     Output[0] = Func.Invoke(Input[0]);
 }