Example #1
0
        public UnsubscribeAction Connect(string key, IPipelineSink <T> sink)
        {
            _output.Set(sinks =>
            {
                MessageRouter <T> router;
                if (sinks.TryGetValue(key, out router) == false)
                {
                    router = new MessageRouter <T>();
                    router.Connect(sink);

                    return(new Dictionary <string, MessageRouter <T> >(sinks)
                    {
                        { key, router }
                    });
                }

                var result = new Dictionary <string, MessageRouter <T> >(sinks);

                router = new MessageRouter <T>(router.Sinks);
                router.Connect(sink);
                result[key] = router;

                return(result);
            });

            return(() => Disconnect(key, sink));
        }
Example #2
0
        bool Disconnect(string key, IPipelineSink <T> sink)
        {
            return(_output.Set(sinks =>
            {
                MessageRouter <T> router;
                if (sinks.TryGetValue(key, out router) == false)
                {
                    return sinks;
                }

                var result = new Dictionary <string, MessageRouter <T> >(sinks);

                List <IPipelineSink <T> > outputSinks = router.Sinks.Where(x => x != sink).ToList();
                if (outputSinks.Count == 0)
                {
                    result.Remove(key);
                }
                else
                {
                    result[key] = new MessageRouter <T>(outputSinks);
                }

                return result;
            }) != null);
        }