Esempio n. 1
0
        public static IEnumerableAsync <T> OnCompleteAsync <T>(this IEnumerableAsync <T> enumerableAsync,
                                                               Func <T[], Task> onComplete,
                                                               ILogger logger = default(ILogger))
        {
            var scopedLogger = logger.CreateScope("OnCompleteAsync");
            var enumerator   = enumerableAsync.GetEnumerator();
            var stack        = new Stack <T>();

            return(EnumerableAsync.Yield <T>(
                       async(next, last) =>
            {
                scopedLogger.Trace("MoveNextAsync.");
                if (await enumerator.MoveNextAsync())
                {
                    var current = enumerator.Current;
                    scopedLogger.Trace("Passthrough on value.");
                    stack.Push(current);
                    return next(current);
                }

                var allValues = stack.ToArray();
                scopedLogger.Trace($"Accumulated `{allValues.Length}` Values.");
                await onComplete(allValues);
                scopedLogger.Trace($"Complete.");

                return last;
            }));
        }
Esempio n. 2
0
        public static IEnumerableAsync <T> OnComplete <T>(this IEnumerableAsync <T> enumerableAsync,
                                                          Action <T[]> onComplete,
                                                          EastFive.Analytics.ILogger logger = default(ILogger))
        {
            var enumerator = enumerableAsync.GetEnumerator();
            var stack      = new Stack <T>();

            return(EnumerableAsync.Yield <T>(
                       async(next, last) =>
            {
                //if (!tag.IsNullOrWhiteSpace())
                //    Console.WriteLine($"Join[{tag}] MoveNextAsync.");
                if (await enumerator.MoveNextAsync())
                {
                    var current = enumerator.Current;
                    //if (!tag.IsNullOrWhiteSpace())
                    //    Console.WriteLine($"Join[{tag}] Passthrough on value.");
                    stack.Push(current);
                    return next(current);
                }

                var allValues = stack.ToArray();
                if (!logger.IsDefaultOrNull())
                {
                    Console.WriteLine($"OnComplete Accumulated `{allValues.Length}` Values.");
                }
                onComplete(allValues);
                if (!logger.IsDefaultOrNull())
                {
                    Console.WriteLine($"OnComplete Complete.");
                }

                return last;
            }));
        }
Esempio n. 3
0
        public static IEnumerableAsync <T> OnCompleteJoin <T>(this IEnumerableAsync <T> enumerableAsync,
                                                              Func <T[], IEnumerableAsync <T> > onComplete)
        {
            var enumerator = enumerableAsync.GetEnumerator();
            var stack      = new Stack <T>();
            var called     = false;

            return(EnumerableAsync.Yield <T>(
                       async(next, last) =>
            {
                if (!called)
                {
                    if (await enumerator.MoveNextAsync())
                    {
                        var current = enumerator.Current;
                        stack.Push(current);
                        return next(current);
                    }

                    var allValues = stack.ToArray();
                    var completeItems = onComplete(allValues);
                    called = true;
                    enumerator = completeItems.GetEnumerator();
                }

                if (await enumerator.MoveNextAsync())
                {
                    var current = enumerator.Current;
                    return next(current);
                }

                return last;
            }));
        }
Esempio n. 4
0
        public static IEnumerableAsync <T> JoinTask <T>(this IEnumerableAsync <T> enumerableAsync,
                                                        Task task,
                                                        EastFive.Analytics.ILogger logger = default(ILogger))
        {
            var scopedLogger = logger.CreateScope($"Join[{task.Id}]");
            var enumerator   = enumerableAsync.GetEnumerator();

            return(EnumerableAsync.Yield <T>(
                       async(next, last) =>
            {
                //if (!tag.IsNullOrWhiteSpace())
                //    Console.WriteLine($"Join[{tag}] MoveNextAsync.");
                if (await enumerator.MoveNextAsync())
                {
                    //if (!tag.IsNullOrWhiteSpace())
                    //    Console.WriteLine($"Join[{tag}] Passthrough on value.");
                    return next(enumerator.Current);
                }

                scopedLogger.Trace($"Joining Task.");
                await task;
                scopedLogger.Trace($"Complete.");
                return last;
            }));
        }
Esempio n. 5
0
        public static IEnumerableAsync <TSelect> SelectAsyncOptional <TItem, TSelect>(this IEnumerableAsync <TItem> enumerable,
                                                                                      Func <TItem, Func <TSelect, ISelectionResult <TSelect> >, Func <ISelectionResult <TSelect> >, Task <ISelectionResult <TSelect> > > selection)
        {
            var enumerator = enumerable.GetEnumerator();
            var selections = EnumerableAsync.Yield <TSelect>(
                async(yieldAsync, yieldBreak) =>
            {
                while (true)
                {
                    if (!await enumerator.MoveNextAsync())
                    {
                        return(yieldBreak);
                    }

                    var item            = enumerator.Current;
                    var selectionResult = await selection(item,
                                                          (selected) =>
                    {
                        return(new Selection <TSelect>(selected));
                    },
                                                          () =>
                    {
                        return(new SelectionSkipped <TSelect>());
                    });
                    if (selectionResult.HasValue)
                    {
                        return(yieldAsync(selectionResult.Value));
                    }
                }
            });

            return(selections);
        }
Esempio n. 6
0
        public static async Task <TResult> SelectOptionalAsync <TItem, TSelect, TCarry, TResult>(this IEnumerableAsync <TItem> enumerable,
                                                                                                 TCarry carry,
                                                                                                 Func <TItem, TCarry, Func <TSelect, TCarry, ISelectionResult <TSelect> >, Func <TCarry, ISelectionResult <TSelect> >, Task <ISelectionResult <TSelect> > > selectionAsync,
                                                                                                 Func <TSelect[], TCarry, TResult> aggregation)
        {
            var enumerator = enumerable.GetEnumerator();
            var selecteds  = default(IEnumerableAsync <TSelect>);

            // var completedEvent = new System.Threading.ManualResetEvent(false);
            selecteds = EnumerableAsync.Yield <TSelect>(
                async(yieldReturn, yieldBreak) =>
            {
                while (true)
                {
                    if (!await enumerator.MoveNextAsync())
                    {
                        return(yieldBreak);
                    }

                    var yieldResult = await selectionAsync(enumerator.Current, carry,
                                                           (selected, carryUpdated) =>
                    {
                        carry = carryUpdated;
                        return(new Selection <TSelect>(selected));
                    },
                                                           (carryUpdated) =>
                    {
                        carry = carryUpdated;
                        return(new SelectionSkipped <TSelect>());
                    });
                    if (yieldResult.HasValue)
                    {
                        return(yieldReturn(yieldResult.Value));
                    }
                }
            });
            var selectedResults = (await selecteds.Async()).ToArray();
            var aggregated      = aggregation(selectedResults, carry);

            return(aggregated);
        }
            public IEnumeratorAsync <KeyValuePair <TKey, TValue> > GetEnumerator()
            {
                KeyValuePair <TKey, TValue>[] dictionaryKvps;
                lock (dictionary)
                {
                    dictionaryKvps = dictionary.ToArray();
                }
                var dictionaryKvpsIndex = 0;
                var kvps = EnumerableAsync.Yield <KeyValuePair <TKey, TValue> >(
                    async(yieldReturn, yieldBreak) =>
                {
                    if (dictionaryKvpsIndex < dictionaryKvps.Length)
                    {
                        dictionaryKvpsIndex = dictionaryKvpsIndex + 1;
                        var kvp             = dictionaryKvps[dictionaryKvpsIndex];
                        return(yieldReturn(kvp));
                    }

                    return(await yieldBreak.AsTask());
                });

                return(kvps.GetEnumerator());
            }
Esempio n. 8
0
        public static TResult SelectOptional <TItem, TSelect, TCarry, TResult>(this IEnumerableAsync <TItem> enumerable,
                                                                               TCarry carry,
                                                                               Func <TItem, TCarry, Func <TSelect, TCarry, TResult>, Func <TCarry, TResult>, TResult> selection,
                                                                               Func <IEnumerableAsync <TSelect>, TCarry, TResult> aggregation)
        {
            var     enumerator     = enumerable.GetEnumerator();
            var     selecteds      = default(IEnumerableAsync <TSelect>);
            TResult aggregated     = default(TResult);
            var     completedEvent = new System.Threading.AutoResetEvent(false);

            selecteds = EnumerableAsync.Yield <TSelect>(
                async(yieldReturn, yieldBreak) =>
            {
                if (!await enumerator.MoveNextAsync())
                {
                    return(yieldBreak);
                }

                var yieldResult = default(IYieldResult <TSelect>);
                var discard     = selection(enumerator.Current, carry,
                                            (selected, carryUpdated) =>
                {
                    yieldResult = yieldReturn(selected);
                    completedEvent.WaitOne();
                    return(aggregated);
                },
                                            (carryUpdated) =>
                {
                    completedEvent.WaitOne();
                    return(aggregated);
                });
                return(yieldResult);
            });
            aggregated = aggregation(selecteds, carry);
            completedEvent.Set();
            return(aggregated);
        }
Esempio n. 9
0
        public static IEnumerableAsync <T> OnCompleteAsyncAppend <T>(this IEnumerableAsync <T> enumerableAsync,
                                                                     Func <T[], Task <T[]> > onComplete)
        {
            var enumerator         = enumerableAsync.GetEnumerator();
            var stack              = new Stack <T>();
            var called             = false;
            var completeItemsIndex = 0;
            var completeItems      = default(T[]);

            return(EnumerableAsync.Yield <T>(
                       async(next, last) =>
            {
                if (!called)
                {
                    if (await enumerator.MoveNextAsync())
                    {
                        var current = enumerator.Current;
                        stack.Push(current);
                        return next(current);
                    }

                    var allValues = stack.ToArray();
                    completeItems = await onComplete(allValues);
                    called = true;
                }

                if (completeItemsIndex < completeItems.Length)
                {
                    var current = completeItems[completeItemsIndex];
                    completeItemsIndex++;
                    return next(current);
                }

                return last;
            }));
        }