Example #1
0
        public IDisposable SubscribeRaw(IObserver <TSource> observer, bool enableSafeguard)
        {
            IDisposable             run;
            ISafeObserver <TSource> safeObserver = null;

            //
            // See AutoDetachObserver.cs for more information on the safeguarding requirement and
            // its implementation aspects.
            //
            if (enableSafeguard)
            {
                observer = safeObserver = SafeObserver <TSource> .Wrap(observer);
            }

            if (CurrentThreadScheduler.IsScheduleRequired)
            {
                var runAssignable = new SingleAssignmentDisposable();

                CurrentThreadScheduler.Instance.ScheduleAction(
                    (@this: this, runAssignable, observer),
                    tuple => tuple.runAssignable.Disposable = [email protected](tuple.observer));

                run = runAssignable;
            }
            else
            {
                run = Run(observer);
            }

            safeObserver?.SetResource(run);

            return(run);
        }
Example #2
0
        public IDisposable SubscribeRaw(IObserver <TTarget> observer, bool enableSafeguard)
        {
            ISafeObserver <TTarget> safeObserver = null;

            //
            // See AutoDetachObserver.cs for more information on the safeguarding requirement and
            // its implementation aspects.
            //
            if (enableSafeguard)
            {
                observer = safeObserver = SafeObserver <TTarget> .Wrap(observer);
            }

            var sink = CreateSink(observer);

            safeObserver?.SetResource(sink);

            if (CurrentThreadScheduler.IsScheduleRequired)
            {
                CurrentThreadScheduler.Instance.ScheduleAction(
                    (@this: this, sink),
                    tuple => [email protected](tuple.sink));
            }
            else
            {
                Run(sink);
            }

            return(sink);
        }
Example #3
0
        private static void Subscribe_ <T>(this IObservable <T> source, IObserver <T> observer, CancellationToken token)
        {
            if (token.CanBeCanceled)
            {
                if (!token.IsCancellationRequested)
                {
                    var consumer = SafeObserver <T> .Wrap(observer);

                    //
                    // [OK] Use of unsafe Subscribe: exception during Subscribe doesn't orphan CancellationTokenRegistration.
                    //
                    var d = source.Subscribe/*Unsafe*/ (consumer);

                    consumer.SetResource(token.Register(state => ((IDisposable)state).Dispose(), d));
                }
            }
            else
            {
                source.Subscribe(observer);
            }
        }