/// <summary>
        /// Context-preserving variant of <see cref="FlowOperations.Select{T,TIn,TOut,TMat}"/>
        /// </summary>
        public static FlowWithContext <TCtx, TIn, TCtx, TOut2, TMat> Select <TCtx, TIn, TOut, TOut2, TMat>(
            this FlowWithContext <TCtx, TIn, TCtx, TOut, TMat> flow, Func <TOut, TOut2> fn)
        {
            var stage = new Select <(TOut, TCtx), (TOut2, TCtx)>(x => (fn(x.Item1), x.Item2));

            return(flow.Via(Flow.FromGraph(stage)));
        }
        /// <summary>
        /// Context-preserving variant of <see cref="FlowOperations.Grouped{TIn,TOut,TMat}"/>
        /// </summary>
        public static FlowWithContext <TCtx, TIn, TCtx, TOut, TMat> WhereNot <TCtx, TIn, TOut, TMat>(
            this FlowWithContext <TCtx, TIn, TCtx, TOut, TMat> flow, Func <TOut, bool> predicate)
        {
            var stage = new Where <(TOut, TCtx)>(x => !predicate(x.Item1));

            return(flow.Via(Flow.FromGraph(stage)));
        }
        /// <summary>
        /// Context-preserving variant of <see cref="FlowOperations.SelectAsync{T,TIn,TOut,TMat}"/>
        /// </summary>
        public static FlowWithContext <TCtx, TIn, TCtx, TOut2, TMat> SelectAsync <TCtx, TIn, TOut, TOut2, TMat>(
            this FlowWithContext <TCtx, TIn, TCtx, TOut, TMat> flow, int parallelism, Func <TOut, Task <TOut2> > fn)
        {
            var stage = new SelectAsync <(TOut, TCtx), (TOut2, TCtx)>(parallelism,
                                                                      async x => (await fn(x.Item1), x.Item2));

            return(flow.Via(Flow.FromGraph(stage)));
        }
        /// <summary>
        /// Apply the given function to each context element (leaving the data elements unchanged).
        /// </summary>
        public static FlowWithContext <TCtx, TIn, TCtx2, TOut, TMat> SelectContext <TCtx, TIn, TCtx2, TOut, TMat>(
            this FlowWithContext <TCtx, TIn, TCtx, TOut, TMat> flow, Func <TCtx, TCtx2> mapContext)
        {
            var stage = new Select <(TOut, TCtx), (TOut, TCtx2)>(x =>
                                                                 (x.Item1, mapContext(x.Item2)));

            return(flow.Via(Flow.FromGraph(stage)));
        }
Пример #5
0
        public static FlowWithContext <ICommittableOffset, E, ICommittableOffsetBatch, NotUsed, NotUsed> FlowWithOffsetContext <E>(CommitterSettings settings)
        {
            var value = Akka.Streams.Dsl.Flow.Create <(E, ICommittableOffset)>()
                        .Select(m => m.Item2 as ICommittable)
                        .Via(BatchFlow(settings))
                        .Select(b => (NotUsed.Instance, b));

            return(FlowWithContext.From(value));
        }
        /// <summary>
        /// Context-preserving variant of <see cref="FlowOperations.Collect{T,TIn,TOut,TMat}"/>
        /// </summary>
        public static FlowWithContext <TCtx, TIn, TCtx, TOut2, TMat> Collect <TCtx, TIn, TOut, TOut2, TMat>(
            this FlowWithContext <TCtx, TIn, TCtx, TOut, TMat> flow, Func <TOut, TOut2> fn) where TOut2 : class
        {
            var stage = new Collect <(TOut, TCtx), (TOut2, TCtx)>(func: x =>
            {
                var result = fn(x.Item1);
                return(ReferenceEquals(result, null) ? default((TOut2, TCtx)) : (result, x.Item2));
            });

            return(flow.Via(Flow.FromGraph(stage)));
        }
        /// <summary>
        /// Context-preserving variant of <see cref="FlowOperations.StatefulSelectMany{T,TIn,TOut,TMat}"/>.
        /// </summary>
        public static FlowWithContext <TCtx, TIn, TCtx, TOut2, TMat> StatefulSelectConcat <TCtx, TIn, TOut, TOut2, TMat>(
            this FlowWithContext <TCtx, TIn, TCtx, TOut, TMat> flow, Func <Func <TOut, IEnumerable <TOut2> > > fn)
        {
            var stage = new StatefulSelectMany <(TOut, TCtx), (TOut2, TCtx)>(() =>
            {
                var fun = fn();
                return(itemWithContext =>
                {
                    var items = fun(itemWithContext.Item1);
                    return items.Select(i => (i, itemWithContext.Item2));
                });
            });

            return(flow.Via(Flow.FromGraph(stage)));
        }
        /// <summary>
        /// Context-preserving variant of <see cref="FlowOperations.Grouped{TIn,TOut,TMat}"/>
        /// Each output group will be associated with a `Seq` of corresponding context elements.
        /// </summary>
        public static FlowWithContext <TCtx, TIn, IReadOnlyList <TCtx>, IReadOnlyList <TOut>, TMat> Grouped <TCtx, TIn,
                                                                                                             TOut, TMat>(
            this FlowWithContext <TCtx, TIn, TCtx, TOut, TMat> flow, int n)
        {
            var stage = new Grouped <(TOut, TCtx)>(n);

            return(flow.Via(Flow.FromGraph(stage).Select(itemsWithContexts =>
            {
                var items = new List <TOut>(n);
                var ctxs = new List <TCtx>(n);

                foreach (var tuple in itemsWithContexts)
                {
                    items.Add(tuple.Item1);
                    ctxs.Add(tuple.Item2);
                }

                return ((IReadOnlyList <TOut>)items, (IReadOnlyList <TCtx>)ctxs);
            })));
        }
Пример #9
0
        public void SourceWithContext_must_pass_through_context_using_FlowWithContext()
        {
            var flowWithContext = FlowWithContext.Create <long, string>();

            var msg = new Message("a", 1);

            var sink = this.CreateSubscriberProbe <(string, long)>();

            Source.From(new[] { msg })
            .AsSourceWithContext(x => x.Offset)
            .Select(x => x.Data)
            .Via(flowWithContext.Select(s => s + "b"))
            .AsSource()
            .RunWith(Sink.FromSubscriber(sink), Materializer);

            var sub = sink.ExpectSubscription();

            sub.Request(1);
            sink.ExpectNext(("ab", 1L));
            sink.ExpectComplete();
        }
 /// <summary>
 /// Context-preserving variant of <see cref="FlowOperations.SelectMany{T,TIn,TOut,TMat}"/>.
 /// The context of the input element will be associated with each of the output elements calculated from
 /// this input element.
 /// </summary>
 public static FlowWithContext <TCtx, TIn, TCtx, TOut2, TMat> SelectConcat <TCtx, TIn, TOut, TOut2, TMat>(
     this FlowWithContext <TCtx, TIn, TCtx, TOut, TMat> flow, Func <TOut, IEnumerable <TOut2> > fn) =>
 StatefulSelectConcat(flow, () => fn);