public override IDisposable Subscribe(IEventHandlingSettings handlingSettings) { if (handlingSettings == null) { throw new ArgumentNullException("handlingSettings"); } if (!typeof(IEvent).IsAssignableFrom(handlingSettings.Type)) { throw new Exception(string.Format("'{0}' does not implement '{1}'.", handlingSettings.Type, typeof(IEvent))); } lock (sync) { IList<IEventHandlingSettings> internalEventHandlers; if (!Handlers.TryGetValue(handlingSettings.Type, out internalEventHandlers)) { var newHandlers = new Dictionary<Type, IList<IEventHandlingSettings>>(Handlers) { { handlingSettings.Type, new List<IEventHandlingSettings> { handlingSettings } } }; Handlers = newHandlers; } else { if (handlingSettings.Unique && internalEventHandlers.Count > 0 && internalEventHandlers.Contains(handlingSettings)) { return new Action(() => { }).AsDisposable(); } var newInternalEventHandlers = new List<IEventHandlingSettings>(internalEventHandlers) { handlingSettings }; Handlers[handlingSettings.Type] = newInternalEventHandlers; } } return UnsubscriptionAction(handlingSettings); }
protected override IDisposable UnsubscriptionAction(IEventHandlingSettings handlingSettings) { Action action = delegate { lock (sync) { IList<IEventHandlingSettings> internalEventHandlers; if (!Handlers.TryGetValue(handlingSettings.Type, out internalEventHandlers)) { return; } var newInternalEventHandlers = new List<IEventHandlingSettings>(internalEventHandlers); if (newInternalEventHandlers.Remove(handlingSettings)) { Handlers[handlingSettings.Type] = newInternalEventHandlers; } } }; return action.AsDisposable(); }