示例#1
0
文件: Buffers.cs 项目: areilly711/psi
        private static IProducer <U> BufferSelectInternal <T, U>(this IProducer <T> source, int size, Func <IEnumerable <Message <T> >, ValueTuple <U, DateTime> > selector, DeliveryPolicy policy)
        {
            if (size < 0)
            {
                throw new ArgumentOutOfRangeException("size", size, "The size should be positive (and non-zero).");
            }

            var buffer = new BufferProcess <T, U>(
                source.Out.Pipeline,
                (m, f, e) =>
            {
                if (!f)
                {
                    BufferSelectProcessor(selector, m, e);
                }
            },
                size);

            return(PipeTo(source, buffer, policy));
        }
示例#2
0
文件: Buffers.cs 项目: areilly711/psi
        /// <summary>
        /// Historical messages by time span.
        /// </summary>
        /// <typeparam name="T">Type of source messages.</typeparam>
        /// <typeparam name="U">Type of output messages.</typeparam>
        /// <param name="source">Source stream.</param>
        /// <param name="timeSpan">Time span over which to gather historical messages.</param>
        /// <param name="selector">Selector function.</param>
        /// <param name="policy">Delivery policy.</param>
        /// <returns>Output stream.</returns>
        public static IProducer <U> History <T, U>(this IProducer <T> source, TimeSpan timeSpan, Func <IEnumerable <Message <T> >, ValueTuple <U, DateTime> > selector, DeliveryPolicy policy = null)
        {
            if (timeSpan.Ticks < 0)
            {
                throw new ArgumentOutOfRangeException("timeSpan", timeSpan, "The timeSpan should be positive (and non-zero).");
            }

            Func <Message <T>, DateTime, bool> bufferRemoveCondition =
                (b, ct) => b.OriginatingTime < ct - timeSpan;

            var buffer = new BufferProcess <T, U>(
                source.Out.Pipeline,
                bufferRemoveCondition,
                (m, f, e) =>
            {
                if (!f)
                {
                    BufferSelectProcessor(selector, m, e);
                }
            });

            source.PipeTo(buffer.In, policy ?? DeliveryPolicy.Immediate);
            return(buffer.Out);
        }