| | | 1 | | using System.Collections; |
| | | 2 | | using Elsa.Workflows.Models; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Workflows; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides extension methods for the ActivityExecutionContext class. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class ItemSourceActivityExecutionContextExtensions |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Retrieves the item source and returns it as an asynchronous enumerable. |
| | | 13 | | /// Supported types are <see cref="IEnumerable{T}"/>, <see cref="IAsyncEnumerable{T}"/> and <see> <cref>IAsyncEnumer |
| | | 14 | | /// </summary> |
| | | 15 | | /// <typeparam name="T">The type of the items in the source collection.</typeparam> |
| | | 16 | | /// <param name="context">The activity execution context.</param> |
| | | 17 | | /// <param name="input">The input object.</param> |
| | | 18 | | /// <returns>An asynchronous enumerable of items from the source collection.</returns> |
| | | 19 | | public static async IAsyncEnumerable<T> GetItemSource<T>(this ActivityExecutionContext context, Input<object> input) |
| | | 20 | | { |
| | 21 | 21 | | var items = context.Get(input); |
| | | 22 | | |
| | 21 | 23 | | if (items == null) |
| | 1 | 24 | | yield break; |
| | | 25 | | |
| | 20 | 26 | | var itemsType = items.GetType(); |
| | 20 | 27 | | if (itemsType.Name == "AsyncEnumerableAdapter`1") |
| | | 28 | | { |
| | 0 | 29 | | var isBatch = itemsType.GenericTypeArguments.Length == 1 && typeof(IEnumerable).IsAssignableFrom(itemsType.G |
| | | 30 | | |
| | 0 | 31 | | if (isBatch) |
| | | 32 | | { |
| | 0 | 33 | | if(items is IAsyncEnumerable<IEnumerable<T>> typedItems) |
| | 0 | 34 | | await foreach (var typedItem in typedItems) |
| | 0 | 35 | | foreach (T item in typedItem) |
| | 0 | 36 | | yield return item; |
| | | 37 | | } |
| | | 38 | | } |
| | 20 | 39 | | else if (items is IAsyncEnumerable<T> asyncEnumerable) |
| | | 40 | | { |
| | 0 | 41 | | await foreach (T item in asyncEnumerable) |
| | 0 | 42 | | yield return item; |
| | | 43 | | } |
| | 20 | 44 | | else if (items is IEnumerable<T> enumerable) |
| | | 45 | | { |
| | 128 | 46 | | foreach (T item in enumerable) |
| | 44 | 47 | | yield return item; |
| | | 48 | | } |
| | 21 | 49 | | } |
| | | 50 | | } |