コード例 #1
0
        public IJsonStream Subscribe(IJsonStreamObserver observer, params string[] streamNames)
        {
            if (streamNames == null || !streamNames.Any())
            {
                throw new ArgumentException($"{GetType().Name}.{nameof(Subscribe)}: A a stream name must be specified.");
            }

            lock (Sync)
            {
                foreach (var streamName in streamNames)
                {
                    Logger?.LogDebug($"{GetType().Name}.{nameof(Subscribe)}: \"{streamName}\"  [thread: {Thread.CurrentThread.ManagedThreadId}]");

                    if (!Subscribers.ContainsKey(streamName))
                    {
                        Logger?.LogDebug($"{GetType().Name}.{nameof(Subscribe)}: Adding stream ({streamName}).  [thread: {Thread.CurrentThread.ManagedThreadId}]");
                        Subscribers[streamName] = new List <IJsonStreamObserver>();
                        StreamNames.Add(streamName);

                        AbortStreaming();
                        OnProvidedStreamsChanged();
                    }

                    if (observer == null || Subscribers[streamName].Contains(observer))
                    {
                        continue;
                    }

                    Logger?.LogDebug($"{GetType().Name}.{nameof(Subscribe)}: Adding observer of stream ({streamName})  [thread: {Thread.CurrentThread.ManagedThreadId}]");
                    Subscribers[streamName].Add(observer);
                }
            }

            return(this);
        }
コード例 #2
0
        private void Unsubscribe(IJsonStreamObserver observer)
        {
            if (observer == null)
            {
                UnsubscribeAll();
                return;
            }

            lock (Sync)
            {
                foreach (var streamAndSubscribers in Subscribers.ToArray())
                {
                    if (streamAndSubscribers.Value.Contains(observer))
                    {
                        Logger?.LogDebug($"{GetType().Name}.{nameof(Unsubscribe)}: Removing observer of stream ({streamAndSubscribers.Key})  [thread: {Thread.CurrentThread.ManagedThreadId}]");
                        streamAndSubscribers.Value.Remove(observer);
                    }

                    // Unsubscribe stream if there are no callbacks.
                    // ReSharper disable once InvertIf
                    if (!streamAndSubscribers.Value.Any())
                    {
                        RemoveStream(streamAndSubscribers.Key);
                    }
                }
            }
        }
コード例 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="jsonStream"></param>
        /// <param name="observer"></param>
        /// <param name="streamNames"></param>
        public static void Unsubscribe(this IJsonStream jsonStream, IJsonStreamObserver observer, IEnumerable <string> streamNames)
        {
            Throw.IfNull(jsonStream, nameof(jsonStream));
            Throw.IfNull(streamNames, nameof(streamNames));

            jsonStream.Unsubscribe(observer, streamNames.ToArray());
        }
コード例 #4
0
        public IJsonStream Unsubscribe(IJsonStreamObserver observer, params string[] streamNames)
        {
            if (streamNames == null || !streamNames.Any())
            {
                Unsubscribe(observer);
                return(this);
            }

            lock (Sync)
            {
                foreach (var streamName in streamNames)
                {
                    Logger?.LogDebug($"{GetType().Name}.{nameof(Unsubscribe)}: \"{streamName}\"  [thread: {Thread.CurrentThread.ManagedThreadId}]");
                    // NOTE: Allow unsubscribe even if IJsonClient is still observing stream to support unlink functionality.

                    if (!Subscribers.ContainsKey(streamName))
                    {
                        Logger?.LogError($"{GetType().Name}.{nameof(Unsubscribe)}: Not subscribed to stream ({streamName}).  [thread: {Thread.CurrentThread.ManagedThreadId}]");
                        throw new InvalidOperationException($"{GetType().Name}.{nameof(Unsubscribe)}: Not subscribed to stream ({streamName}).");
                    }

                    if (observer != null && Subscribers[streamName].Contains(observer))
                    {
                        Logger?.LogDebug($"{GetType().Name}.{nameof(Unsubscribe)}: Removing callback for stream ({streamName}).  [thread: {Thread.CurrentThread.ManagedThreadId}]");
                        Subscribers[streamName].Remove(observer);
                    }

                    // Unsubscribe stream if there are no callbacks.
                    // ReSharper disable once InvertIf
                    if (!Subscribers[streamName].Any())
                    {
                        RemoveStream(streamName);
                    }
                }
            }

            return(this);
        }
コード例 #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="jsonStream"></param>
        /// <param name="observer"></param>
        public static void Unsubscribe(this IJsonStream jsonStream, IJsonStreamObserver observer)
        {
            Throw.IfNull(jsonStream, nameof(jsonStream));

            jsonStream.Unsubscribe(observer, null);
        }