Beispiel #1
0
        private static async Task RunSetSamplesAsync(ISet <Data> set)
        {
            var data = new Data()
            {
                Name = "A name", Value = 5
            };

            // A set is a collection of unique items
            await set.AddAsync(data);

            // Adding the same item again overwrites the existing.
            // Usually this doesn't makes any difference, but depend of how the implementation compares the values.
            // For instance, the memory set uses an equality comparer, and the SQL uses the item's primary key values.
            await set.AddAsync(data);

            if (await set.ContainsAsync(data))
            {
                Console.WriteLine($"The value '{data}' exists in the set");
            }

            // The set also supports the IAsyncEnumerable interface, that allows async enumeration of the items.
            IAsyncEnumerable <Data> enumerable = await set.AsEnumerableAsync();

            // Some async extensions are available
            Console.WriteLine($"There are '{await enumerable.CountAsync()}' items in the set, which are:");
            await enumerable.ForEachAsync(i => Console.WriteLine($"- {i}"), CancellationToken.None);
        }
        public virtual async Task SynchronizeAsync(ISet <T> source, ISet <T> target)
        {
            using (var cts = new CancellationTokenSource(_synchronizationTimeout))
            {
                var targetEnumerable = await target.AsEnumerableAsync().ConfigureAwait(false);

                // Copy the keys to be removed to the memory, to avoid problems with changing the collection while enumerating.
                var targetToRemove = new List <T>();
                await targetEnumerable.ForEachAsync(async v =>
                {
                    if (!await source.ContainsAsync(v).ConfigureAwait(false))
                    {
                        targetToRemove.Add(v);
                    }
                }, cts.Token);

                foreach (var key in targetToRemove)
                {
                    await target.TryRemoveAsync(key).ConfigureAwait(false);
                }

                var sourceEnumerable = await source.AsEnumerableAsync().ConfigureAwait(false);

                await sourceEnumerable.ForEachAsync(async v =>
                {
                    await target.AddAsync(v).ConfigureAwait(false);
                }, cts.Token);
            }
        }
Beispiel #3
0
 /// <summary>
 /// Adds multiple instances of <see cref="T"/> to the set.
 /// </summary>
 /// <param name="set"></param>
 /// <param name="items"></param>
 /// <param name="cancellationToken"></param>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static async Task AddRangeAsync <T>(this ISet <T> set, IAsyncEnumerable <T> items, CancellationToken cancellationToken = default)
 {
     await foreach (var item in items.WithCancellation(cancellationToken).ConfigureAwait(false))
     {
         await set.AddAsync(item, cancellationToken).ConfigureAwait(false);
     }
 }
Beispiel #4
0
        public async Task AddAsync(T value, CancellationToken cancellationToken = default)
        {
            await _set.AddAsync(value, cancellationToken).ConfigureAwait(false);

            await _writeHandler(value, cancellationToken).ConfigureAwait(false);
        }
Beispiel #5
0
 public virtual Task AddAsync(TItem value, CancellationToken cancellationToken = default)
 {
     return(_set.AddAsync(value));
 }
        public async Task AddAsync(T value, CancellationToken cancellationToken = default)
        {
            await Task.Delay(_delay, cancellationToken);

            await _set.AddAsync(value, cancellationToken);
        }
Beispiel #7
0
            public async Task AddAsync(T value)
            {
                await _set.AddAsync(value).ConfigureAwait(false);

                await _addTrigger(value).ConfigureAwait(false);
            }
Beispiel #8
0
 public virtual Task AddAsync(TItem value)
 {
     return(_set.AddAsync(value));
 }
Beispiel #9
0
 public virtual Task AddAsync(TItem value, CancellationToken cancellationToken = default) =>
 _set.AddAsync(value, cancellationToken);