Exemple #1
0
        public override void Subscribe(ISubscriber <R>[] subscribers)
        {
            if (!this.Validate(subscribers))
            {
                return;
            }

            int n       = subscribers.Length;
            var parents = new ISubscriber <T> [n];

            for (int i = 0; i < n; i++)
            {
                R accumulator;

                try
                {
                    accumulator = initialFactory();
                }
                catch (Exception ex)
                {
                    ExceptionHelper.ThrowIfFatal(ex);
                    foreach (var s in subscribers)
                    {
                        EmptySubscription <R> .Error(s, ex);
                    }
                    return;
                }

                parents[i] = new PublisherReduceWith <T, R> .ReduceWithSubscriber(subscribers[i], accumulator, reducer);
            }

            source.Subscribe(parents);
        }
        public void Subscribe(ISubscriber <R> s)
        {
            IEnumerator <U> enumerator;

            bool hasValue;

            try
            {
                enumerator = other.GetEnumerator();

                hasValue = enumerator.MoveNext();
            }
            catch (Exception ex)
            {
                ExceptionHelper.ThrowIfFatal(ex);
                EmptySubscription <R> .Error(s, ex);

                return;
            }

            if (!hasValue)
            {
                EmptySubscription <R> .Complete(s);

                return;
            }

            var parent = new ZipEnumerableSubscriber(s, enumerator, zipper);

            source.Subscribe(parent);
        }
        public void Subscribe(ISubscriber <T> s)
        {
            S state;

            try
            {
                state = stateFactory();
            }
            catch (Exception ex)
            {
                ExceptionHelper.ThrowIfFatal(ex);
                EmptySubscription <T> .Error(s, ex);

                return;
            }

            if (s is IConditionalSubscriber <T> )
            {
                s.OnSubscribe(new GenerateConditionalSubscriber((IConditionalSubscriber <T>)s, state, generator, stateDisposer));
            }
            else
            {
                s.OnSubscribe(new GenerateSubscriber(s, state, generator, stateDisposer));
            }
        }
Exemple #4
0
        public void Subscribe(ISubscriber <T> s)
        {
            IPublisher <T> p;

            try
            {
                p = supplier();
            }
            catch (Exception ex)
            {
                ExceptionHelper.ThrowIfFatal(ex);
                EmptySubscription <T> .Error(s, ex);

                return;
            }

            if (p == null)
            {
                EmptySubscription <T> .Error(s, new NullReferenceException("The supplier returned a null IPublisher"));

                return;
            }

            p.Subscribe(s);
        }
        public void Subscribe(ISubscriber <R> s)
        {
            var pp = new PublishProcessor <T>(prefetch);

            IPublisher <R> o;

            try
            {
                o = transformer(pp);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ThrowIfFatal(ex);
                EmptySubscription <R> .Error(s, ex);

                return;
            }

            if (s is IConditionalSubscriber <R> )
            {
                o.Subscribe(new PublishSelectorConditionalSubscriber(pp, (IConditionalSubscriber <R>)s));
            }
            else
            {
                o.Subscribe(new PublishSelectorSubscriber(pp, s));
            }

            source.Subscribe(pp);
        }
Exemple #6
0
        /// <inheritDoc/>
        public void Subscribe(ISubscriber <T> s)
        {
            PublishSubscription ps = new PublishSubscription(s, this);

            s.OnSubscribe(ps);

            if (subscribers.Add(ps))
            {
                if (ps.IsCancelled())
                {
                    subscribers.Remove(ps);
                }
                else
                {
                    Drain();
                }
            }
            else
            {
                var ex = error;
                if (ex != null)
                {
                    EmptySubscription <T> .Error(s, ex);
                }
                else
                {
                    EmptySubscription <T> .Complete(s);
                }
            }
        }
Exemple #7
0
 public void Subscribe(ISubscriber <V> s)
 {
     if (Interlocked.CompareExchange(ref once, 1, 0) == 0)
     {
         s.OnSubscribe(this);
         if (s is IConditionalSubscriber <V> )
         {
             Volatile.Write(ref conditional, (IConditionalSubscriber <V>)s);
             if (Volatile.Read(ref cancelled) != 0)
             {
                 conditional = null;
                 return;
             }
         }
         else
         {
             Volatile.Write(ref regular, s);
             if (Volatile.Read(ref cancelled) != 0)
             {
                 regular = null;
                 return;
             }
         }
         Drain();
     }
     else
     {
         EmptySubscription <V> .Error(s, new InvalidOperationException("The IGroupedFlux allows only a single ISubscriber!"));
     }
 }
 public void Subscribe(ISubscriber <T> s)
 {
     if (!whenRequested)
     {
         EmptySubscription <T> .Error(s, error);
     }
     else
     {
         s.OnSubscribe(new ErrorSubscription(s, error));
     }
 }
Exemple #9
0
 /// <summary>
 /// Validate that the parallelism of the IParallelFlux equals to the number of elements in
 /// the ISubscriber array. If not, each ISubscriber is notified with an ArgumentException.
 /// </summary>
 /// <typeparam name="T">The element type of the parallel flux</typeparam>
 /// <typeparam name="U">The element type of the ISubscriber array (could be T or IOrderedItem{T})</typeparam>
 /// <param name="pf">The parent IParallelFlux instance</param>
 /// <param name="subscribers">The ISubscriber array</param>
 /// <returns>True if the subscribers were valid</returns>
 internal static bool Validate <T, U>(this IParallelFlux <T> pf, ISubscriber <U>[] subscribers)
 {
     if (pf.Parallelism != subscribers.Length)
     {
         Exception ex = new ArgumentException("Parallelism(" + pf.Parallelism + ") != Subscriber count (" + subscribers.Length + ")");
         foreach (ISubscriber <U> s in subscribers)
         {
             EmptySubscription <U> .Error(s, ex);
         }
         return(false);
     }
     return(true);
 }
        public void Subscribe(ISubscriber <T> s)
        {
            try
            {
                action();
            }
            catch (Exception ex)
            {
                ExceptionHelper.ThrowIfFatal(ex);
                EmptySubscription <T> .Error(s, ex);

                return;
            }
            EmptySubscription <T> .Complete(s);
        }
        public void Subscribe(ISubscriber <R> s)
        {
            ISubscriber <T> sr;

            try
            {
                sr = onLift(s);
            }
            catch (Exception ex)
            {
                EmptySubscription.Error(s, ex);
                return;
            }

            source.Subscribe(sr);
        }
        public void Subscribe(ISubscriber <A> s)
        {
            A accumulator;

            try
            {
                accumulator = initialSupplier();
            }
            catch (Exception ex)
            {
                ExceptionHelper.ThrowIfFatal(ex);
                EmptySubscription <A> .Error(s, ex);

                return;
            }
            source.Subscribe(new ScanWithSubscriber(s, accumulator, scanner));
        }
Exemple #13
0
 public void Subscribe(ISubscriber <T> s)
 {
     if (Interlocked.CompareExchange(ref once, 1, 0) == 0)
     {
         s.OnSubscribe(this);
         Volatile.Write(ref actual, s);
         if (Volatile.Read(ref cancelled))
         {
             actual = null;
             return;
         }
         Drain();
     }
     else
     {
         EmptySubscription <T> .Error(s, new InvalidOperationException("The group can be subscribed to at most once"));
     }
 }
        public static bool ToArray <T, U>(T[] values, IEnumerable <T> valuesEnumerable, ISubscriber <U> s, out int n, out T[] array)
        {
            if (values != null)
            {
                array = values;
                n     = values.Length;
            }
            else
            {
                var i = 0;
                var a = new T[8];

                try
                {
                    foreach (var e in valuesEnumerable)
                    {
                        if (i == a.Length)
                        {
                            var b = new T[i + (i >> 1)];
                            Array.Copy(a, 0, b, 0, i);
                            a = b;
                        }
                        a[i] = e;

                        i++;
                    }
                }
                catch (Exception ex)
                {
                    ExceptionHelper.ThrowIfFatal(ex);
                    EmptySubscription <U> .Error(s, ex);

                    array = null;
                    n     = 0;
                    return(false);
                }

                array = a;
                n     = i;
            }

            return(true);
        }
Exemple #15
0
        public void Subscribe(ISubscriber <T> s)
        {
            IEnumerator <IPublisher <T> > enumerator;

            bool hasValue;

            try
            {
                enumerator = sources.GetEnumerator();

                hasValue = enumerator.MoveNext();
            }
            catch (Exception ex)
            {
                ExceptionHelper.ThrowIfFatal(ex);
                EmptySubscription <T> .Error(s, ex);

                return;
            }

            if (!hasValue)
            {
                EmptySubscription <T> .Complete(s);

                return;
            }

            if (s is IConditionalSubscriber <T> )
            {
                ConcatConditionalSubscriber parent = new ConcatConditionalSubscriber((IConditionalSubscriber <T>)s, enumerator, delayError);
                s.OnSubscribe(parent);

                parent.Drain();
            }
            else
            {
                ConcatSubscriber parent = new ConcatSubscriber(s, enumerator, delayError);
                s.OnSubscribe(parent);

                parent.Drain();
            }
        }
        public void Subscribe(ISubscriber <C> s)
        {
            C c;

            try
            {
                c = collectionSupplier();
            }
            catch (Exception ex)
            {
                ExceptionHelper.ThrowIfFatal(ex);
                EmptySubscription <C> .Error(s, ex);

                return;
            }

            CollectSubscriber parent = new CollectSubscriber(s, c, collector);

            source.Subscribe(parent);
        }
Exemple #17
0
            internal bool TryConnect(
                IPublisher <T> source,
                Action <IDisposable> action,
                Func <IProcessor <T, T> > processorSupplier,
                Func <IFlux <T>, IPublisher <U> > selector)
            {
                if (Volatile.Read(ref done) != 0)
                {
                    return(false);
                }
                if (Interlocked.CompareExchange(ref once, 1, 0) == 0)
                {
                    try
                    {
                        processor = processorSupplier();

                        result = selector(Flux.Wrap(processor));
                    }
                    catch (Exception ex)
                    {
                        ExceptionHelper.ThrowIfFatal(ex);
                        Volatile.Write(ref done, 1);
                        foreach (var s in subscribers.Terminate())
                        {
                            EmptySubscription <U> .Error(s, ex);
                        }
                        return(false);
                    }

                    foreach (var s in subscribers.Terminate())
                    {
                        result.Subscribe(s);
                    }

                    action?.Invoke(this);

                    source.Subscribe(this);
                }
                return(true);
            }
        /// <inheritdoc/>
        public void Subscribe(ISubscriber <T> s)
        {
            if (Interlocked.CompareExchange(ref once, 1, 0) == 0)
            {
                s.OnSubscribe(this);

                if (s is IConditionalSubscriber <T> )
                {
                    Volatile.Write(ref conditional, (IConditionalSubscriber <T>)s);
                    if (lvCancelled())
                    {
                        conditional = null;
                    }
                    else
                    {
                        Drain();
                    }
                }
                else
                {
                    Volatile.Write(ref regular, s);
                    if (lvCancelled())
                    {
                        regular = null;
                    }
                    else
                    {
                        Drain();
                    }
                }
            }
            else
            {
                EmptySubscription <T> .Error(s, new InvalidOperationException("Only a one ISubscriber allowed. Use Publish() or Replay() to share an UnicastProcessor."));
            }
        }
        public void Subscribe(ISubscriber <T> s)
        {
            IEnumerator <T> enumerator;

            bool hasValue;

            try
            {
                enumerator = enumerable.GetEnumerator();

                hasValue = enumerator.MoveNext();
            }
            catch (Exception ex)
            {
                ExceptionHelper.ThrowIfFatal(ex);
                EmptySubscription <T> .Error(s, ex);

                return;
            }

            if (!hasValue)
            {
                EmptySubscription <T> .Complete(s);

                return;
            }

            if (s is IConditionalSubscriber <T> )
            {
                s.OnSubscribe(new EnumerableConditionalSubscription((IConditionalSubscriber <T>)s, enumerator));
            }
            else
            {
                s.OnSubscribe(new EnumerableSubscription(s, enumerator));
            }
        }
Exemple #20
0
        /// <summary>
        /// Applies shortcuts if the source is the empty instance or an ICallable.
        /// </summary>
        /// <param name="source">The source IPublisher.</param>
        /// <param name="s">The ISubscriber</param>
        /// <param name="mapper">The function that takes a source value and maps it into an IPublisher.</param>
        /// <returns>True if the optimizations were applied.</returns>
        internal static bool CallableXMap(IPublisher <T> source, ISubscriber <R> s, Func <T, IPublisher <R> > mapper)
        {
            if (source == PublisherEmpty <T> .Instance)
            {
                EmptySubscription <R> .Complete(s);

                return(true);
            }
            if (source is ICallable <T> )
            {
                T t;

                try
                {
                    t = (source as ICallable <T>).Value;
                }
                catch (Exception ex)
                {
                    ExceptionHelper.ThrowIfFatal(ex);

                    EmptySubscription <R> .Error(s, ex);

                    return(true);
                }

                IPublisher <R> p;

                try
                {
                    p = mapper(t);
                }
                catch (Exception ex)
                {
                    ExceptionHelper.ThrowIfFatal(ex);

                    EmptySubscription <R> .Error(s, ex);

                    return(true);
                }

                if (p == null)
                {
                    EmptySubscription <R> .Error(s, new NullReferenceException("The mapper returned a null IPublisher"));

                    return(true);
                }

                if (p == PublisherEmpty <R> .Instance)
                {
                    EmptySubscription <R> .Complete(s);

                    return(true);
                }

                if (p is ICallable <R> )
                {
                    R r;

                    try
                    {
                        r = (p as ICallable <R>).Value;
                    }
                    catch (Exception ex)
                    {
                        ExceptionHelper.ThrowIfFatal(ex);

                        EmptySubscription <R> .Error(s, ex);

                        return(true);
                    }

                    s.OnSubscribe(new ScalarSubscription <R>(s, r));
                    return(true);
                }

                p.Subscribe(s);

                return(true);
            }

            return(false);
        }
Exemple #21
0
        public void Subscribe(ISubscriber <T> s)
        {
            S state;

            try
            {
                state = stateFactory();
            }
            catch (Exception ex)
            {
                ExceptionHelper.ThrowIfFatal(ex);
                EmptySubscription <T> .Error(s, ex);

                return;
            }

            IPublisher <T> p;

            try
            {
                p = sourceFactory(state);
                if (p == null)
                {
                    throw new NullReferenceException("The sourceFactory returned a null IPublisher");
                }
            }
            catch (Exception ex)
            {
                ExceptionHelper.ThrowIfFatal(ex);

                if (eager)
                {
                    try
                    {
                        stateDisposer(state);
                    }
                    catch (Exception exc)
                    {
                        ExceptionHelper.ThrowIfFatal(ex);
                        ex = new AggregateException(ex, exc);
                    }
                }

                EmptySubscription <T> .Error(s, ex);

                if (!eager)
                {
                    try
                    {
                        stateDisposer(state);
                    }
                    catch (Exception exc)
                    {
                        ExceptionHelper.ThrowOrDrop(exc);
                    }
                }
                return;
            }


            if (s is IConditionalSubscriber <T> )
            {
                p.Subscribe(new UsingConditionalSubscriber((IConditionalSubscriber <T>)s, state, stateDisposer, eager));
            }
            else
            {
                p.Subscribe(new UsingSubscriber(s, state, stateDisposer, eager));
            }
        }