Esempio n. 1
0
        /// <summary>
        /// Listen for this message with a strongly typed payload
        /// </summary>
        /// <typeparam name="TPayload"></typeparam>
        /// <param name="onBroadcast"></param>
        /// <returns>A disposable token. After it is disposed, the message handler is removed. Caller must maintain a reference to the token!</returns>
        public IDisposable Listen(Func <TPayload, Task> onBroadcast)
        {
            var tuple = new Tuple <Type, Func <object, Task> >(this.GetType(), async arg => await onBroadcast((TPayload)arg));

            if (Broadcaster._subscribers.Contains(tuple))
            {
                return(null);
            }

            Broadcaster._subscribers.Add(tuple);

            var token = new ListenerToken(() => Broadcaster._subscribers.Remove(tuple));

            return(token);
        }
Esempio n. 2
0
        /// <summary>
        /// Listen for enum-based messages with loosely typed payloads
        /// </summary>
        /// <typeparam name="TMessageType"></typeparam>
        /// <param name="message"></param>
        /// <param name="onBroadcast"></param>
        /// <returns>Null if already subscribed!  A disposable token. After it is disposed, the message handler is removed. Caller must maintain a reference to the token!</returns>
        public IDisposable Listen(TMessageType message, Func <object, Task> onBroadcast)
        {
            if (!_broadcaster.TryGetTarget(out var broadcaster))
            {
                return(null);
            }

            var tuple = new Tuple <Enum, Func <object, Task> >(message, onBroadcast);

            if (broadcaster._subscribers.Contains(tuple))
            {
                return(null);
            }

            broadcaster._subscribers.Add(tuple);

            var token = new ListenerToken(() => broadcaster._subscribers.Remove(tuple));

            return(token);
        }