public static IDisposable Subscribe(IGlobalSubscriber subscriber) { List <Type> implementedGlobalSubscribers = EventBusHelper.GetImplementedGlobalSubscribers(subscriber); bool wasIterating = s_IsIterating; s_IsIterating = true; foreach (Type interfaceType in implementedGlobalSubscribers) { List <GlobalSubscriberNode> correspondingList; if (s_GlobalSubscribers.ContainsKey(interfaceType)) { correspondingList = s_GlobalSubscribers[interfaceType]; } else { correspondingList = new List <GlobalSubscriberNode>(); s_GlobalSubscribers[interfaceType] = correspondingList; } correspondingList.Add(new GlobalSubscriberNode(subscriber)); } s_IsIterating = wasIterating; if (!s_IsIterating) { CleanUp(); } return(new DisposeAction(() => Unsubscribe(subscriber))); }
private static void Unsubscribe(IGlobalSubscriber subscriber) { List <Type> implementedGlobalSubscribers = EventBusHelper.GetImplementedGlobalSubscribers(subscriber); foreach (Type interfaceType in implementedGlobalSubscribers) { if (s_GlobalSubscribers.ContainsKey(interfaceType)) { List <GlobalSubscriberNode> subscribers = s_GlobalSubscribers[interfaceType]; subscribers.FirstOrDefault(node => node.SubscriberEquals(subscriber))?.Release(); } } if (!s_IsIterating) { CleanUp(); } }