Exemple #1
0
 public Subscription(SimpleSubject <T> parent, IObserver <T> observer)
 {
     _parent   = parent;
     _observer = observer;
 }
        /// <summary>
        /// Ensures that the resource management mechanisms are set up, including the tollbooth and the collector.
        /// </summary>
        /// <param name="recovery">Indicates whether the operator was recovered or started afresh.</param>
        protected void EnsureResourceManagement(bool recovery)
        {
            var tollbooth = default(IMultiSubject <bool, bool>);
            var collector = default(IMultiSubject <Uri, Uri>);

            if (!recovery)
            {
                var guid = Guid.NewGuid().ToString("D");

                TollboothUri = new Uri("rx://tollbooth/" + guid);
                CollectorUri = new Uri("rx://collector/" + guid);

                StateChanged = true;

                if (TryGetHigherOrderExecutionEnvironment(out var env))
                {
                    // Set up the toll booth responsible to monitor subscription flow on tunnels to
                    // keep a refcount of the number of outstanding subscriptions.
                    env.CreateSimpleSubject <bool>(TollboothUri, _context);

                    // Set up the collector responsible to monitor deletions of tunnels in order to
                    // clean up operator state.
                    env.CreateSimpleSubject <Uri>(CollectorUri, _context);
                }
                else
                {
#pragma warning disable IDE0079 // Remove unnecessary suppression.
#pragma warning disable CA2000  // Dispose objects before losing scope. (No need to dispose toolbooth. SimplySubject<T>.Dispose is trivial. The object just gets GC'ed when the operator goes away.)

                    tollbooth = new SimpleSubject <bool>();

#pragma warning restore CA2000
#pragma warning restore IDE0079
                }
            }

            if (_context.ExecutionEnvironment != null)
            {
                if (TollboothUri != null)
                {
                    tollbooth = _context.ExecutionEnvironment.GetSubject <bool, bool>(TollboothUri);
                }

                if (CollectorUri != null)
                {
                    collector = _context.ExecutionEnvironment.GetSubject <Uri, Uri>(CollectorUri);
                }
            }

            if (tollbooth != null)
            {
                var sub = tollbooth.Subscribe(new RefCountManager(this));
                SubscriptionInitializeVisitor.Initialize(sub, _context);

                Tollbooth = tollbooth.CreateObserver();
            }

            if (collector != null)
            {
                var sub = collector.Subscribe(new Collector(this));
                SubscriptionInitializeVisitor.Initialize(sub, _context);
            }
        }