private void ApolloClient_SubscriptionRouteRemoved(object sender, ApolloSubscriptionFieldEventArgs e)
        {
            var client = sender as ApolloClientProxy <TSchema>;

            if (client == null)
            {
                return;
            }

            var names = SubscriptionEventName.FromGraphField(_schema, e.Field);

            foreach (var name in names)
            {
                lock (_syncLock)
                {
                    if (_subCountByName.TryGetValue(name, out var clients))
                    {
                        if (clients.Contains(client))
                        {
                            clients.Remove(client);
                            if (clients.Count == 0)
                            {
                                _subCountByName.Remove(name);
                                _eventRouter.RemoveReceiver(name, this);
                                _logger?.EventMonitorEnded(name);
                            }
                        }
                    }
                }
            }
        }
        private void ApolloClient_SubscriptionRouteAdded(object sender, ApolloSubscriptionFieldEventArgs e)
        {
            var client = sender as ApolloClientProxy <TSchema>;

            if (client == null)
            {
                return;
            }

            var names = SubscriptionEventName.FromGraphField(_schema, e.Field);

            foreach (var name in names)
            {
                lock (_syncLock)
                {
                    if (!_subCountByName.ContainsKey(name))
                    {
                        _subCountByName.Add(name, new HashSet <ApolloClientProxy <TSchema> >());
                    }

                    _subCountByName[name].Add(client);
                    if (_subCountByName[name].Count == 1)
                    {
                        _eventRouter.AddReceiver(name, this);
                        _logger?.EventMonitorStarted(name);
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// Attempts to generate a dictionary of value relating field path to the possible event names.
        /// </summary>
        /// <param name="schema">The schema.</param>
        /// <returns>Dictionary&lt;System.String, GraphFieldPath&gt;.</returns>
        public static Dictionary <SubscriptionEventName, GraphFieldPath> CreateEventMap(ISchema schema)
        {
            var dic = new Dictionary <SubscriptionEventName, GraphFieldPath>(SubscriptionEventNameEqualityComparer.Instance);

            if (schema == null || !schema.OperationTypes.ContainsKey(GraphCollection.Subscription))
            {
                return(dic);
            }

            foreach (var field in schema.KnownTypes.OfType <IObjectGraphType>()
                     .SelectMany(x => x.Fields.OfType <ISubscriptionGraphField>()))
            {
                var route = field.Route.Clone();
                foreach (var eventName in SubscriptionEventName.FromGraphField(schema, field))
                {
                    if (dic.ContainsKey(eventName))
                    {
                        var path = dic[eventName];
                        throw new DuplicateNameException(
                                  $"Duplciate Subscription Event Name. Unable to register the field '{route.Path}' " +
                                  $"with event name '{eventName}'. The schema '{schema.GetType().FriendlyName()}' already contains " +
                                  $"a field with the event name '{eventName}'. (Event Owner: {path.Path}).");
                    }

                    dic.Add(eventName, route);
                }
            }

            return(dic);
        }