Example #1
0
        /// <summary>
        /// Standardized implementation to recover single item from buffer.
        /// Calls <see cref="Adapt"/> to perform the desired transformed.
        /// <para>Returns true when a consumable instance can be created from
        /// <paramref name="producerDataFeed"/> else returns false.</para>
        /// </summary>
        /// <param name="producerDataFeed">Feed that is getting populated by producers.</param>
        /// <param name="token">Cancellation token</param>
        /// <param name="consumable">transfomed item that can be passed to consumer</param>
        public bool TryGet(IConsumerBuffer <TP> producerDataFeed, CancellationToken token, out TC consumable)
        {
            if (producerDataFeed.TryGet(Timeout.Infinite, token, out var produced))
            {
                consumable = Adapt(produced, token);
                return(true);
            }

            consumable = default;
            return(false);
        }
 /// <summary>
 /// Return true when a consumable instance can be created from
 /// <paramref name="producerDataFeed"/> else returns false.
 /// <para>NOTE:</para>
 /// <para>1. It does NOT observes <seealso cref="IConsumerBuffer{T}.Finished"/> status</para>
 /// <para>2. The first element is ALWAYS (irrespective of provided timeout value) waited for
 /// <seealso cref="Timeout.Infinite"/>. Then items are added to list as long as they can be recovered
 /// before provided millisecond timeout is reached.</para>
 /// <para>3. Further, items are recovered and populated as long as they are available without
 /// any wait. Then the list if finalized and returned. Step 2 and 3 are same when inifinite timeout
 /// is suplied.</para>
 /// <para>4. If method returns true, then the list would contains at least 1 item.</para>
 /// <para>5. List max size would be respected as provided in Ctor.</para>
 /// </summary>
 /// <param name="producerDataFeed">Data feed</param>
 /// <param name="token">token to observe</param>
 /// <param name="consumable">consumable data instance</param>
 public bool TryGet(IConsumerBuffer <TP> producerDataFeed, CancellationToken token, out List <TC> consumable)
 {
     consumable = default(List <TC>);
     if (!producerDataFeed.TryGet(Timeout.Infinite, token, out var value))
     {
         return(false);
     }
     consumable = new List <TC>(_maxListSize)
     {
         Adapt(value, token)
     };
     return(_millisecTimeout == Timeout.Infinite
         ? TryGetWithInfiniteTo(producerDataFeed, token, consumable)
         : TryGetWithFiniteTo(producerDataFeed, token, consumable));
 }
        private bool TryGetWithInfiniteTo(IConsumerBuffer <TP> producerDataFeed, CancellationToken token,
                                          ICollection <TC> consumable)
        {
            while (consumable.Count < _maxListSize)
            {
                if (producerDataFeed.TryGet(Timeout.Infinite, token, out var value))
                {
                    consumable.Add(Adapt(value, token));
                }
                else
                {
                    return(true);
                }
            }

            return(true);
        }
        private bool TryGetWithFiniteTo(IConsumerBuffer <TP> producerDataFeed, CancellationToken token,
                                        ICollection <TC> consumable)
        {
            var timeRemains = _millisecTimeout;
            var sw          = Stopwatch.StartNew();

            while (consumable.Count < _maxListSize)
            {
                if (producerDataFeed.TryGet(timeRemains, token, out var value))
                {
                    consumable.Add(Adapt(value, token));
                    if (timeRemains != 0)
                    {
                        timeRemains = (int)Math.Max(0, _millisecTimeout - sw.ElapsedMilliseconds);
                    }
                }
                else
                {
                    return(true);
                }
            }

            return(true);
        }