diff --git a/src/TinyPubSub/TinyPubSub.cs b/src/TinyPubSub/TinyPubSub.cs index 96ce4e7..6a79c9f 100644 --- a/src/TinyPubSub/TinyPubSub.cs +++ b/src/TinyPubSub/TinyPubSub.cs @@ -37,6 +37,8 @@ public static class TinyPubSub { private static readonly ConcurrentDictionary> Channels = new ConcurrentDictionary>(); private static int order; + + private static object genericOwner = new {}; private static ConcurrentDictionary GetOrCreateChannel(string channel) { @@ -61,7 +63,12 @@ private static ISubscription CreateAndAdd(object owner, string channel, Action a private static ISubscription CreateAndAdd(object owner, string channel, Action action, bool disableAfterFirstUse = false) { Interlocked.Increment(ref order); - + + if (owner == null) + { + owner = genericOwner; + } + var current = GetOrCreateChannel(channel); var subscription = new Subscription(owner, action, disableAfterFirstUse, order); current.TryAdd(subscription, subscription); @@ -71,7 +78,12 @@ private static ISubscription CreateAndAdd(object owner, string channel, Actio private static ISubscription CreateAndAdd(object owner, string channel, Action action, bool disableAfterFirstUse = false) { Interlocked.Increment(ref order); - + + if(owner == null) + { + owner = genericOwner; + } + var current = GetOrCreateChannel(channel); var subscription = new Subscription(owner, action, disableAfterFirstUse, order); current.TryAdd(subscription, subscription); @@ -91,7 +103,7 @@ private static ISubscription CreateAndAdd(object owner, string channel, Actio /// A tag that can be used to unsubscribe. public static string Subscribe(string channel, Action action) { - var subscription = CreateAndAdd(null, channel, action); + var subscription = CreateAndAdd(genericOwner, channel, action); return subscription.Tag; } @@ -103,7 +115,7 @@ public static string Subscribe(string channel, Action action) /// A tag that can be used to unsubscribe. public static string Subscribe(string channel, Action action) { - var subscription = CreateAndAdd(null, channel, action); + var subscription = CreateAndAdd(genericOwner, channel, action); return subscription.Tag; } @@ -146,7 +158,7 @@ public static string Subscribe(object owner, string channel, Action acti /// The type to subscribe to. public static string Subscribe(string channel, Action action) { - var subscription = CreateAndAdd(null, channel, action); + var subscription = CreateAndAdd(genericOwner, channel, action); return subscription.Tag; } @@ -158,7 +170,7 @@ public static string Subscribe(string channel, Action action) /// The action to execute. public static string Subscribe(string channel, Action action) { - var subscription = CreateAndAdd(null, channel, action); + var subscription = CreateAndAdd(Channels, channel, action); return subscription.Tag; } @@ -199,7 +211,7 @@ public static string Subscribe(object owner, string channel, ActionThe type to subscribe to. public static string Subscribe(string channel, Action action) { - var subscription = CreateAndAdd(null, channel, action); + var subscription = CreateAndAdd(genericOwner, channel, action); return subscription.Tag; } @@ -334,6 +346,7 @@ public static TinyEventArgs PublishControlled(string channel, T instance, Act if (subscription.Owner.Target == null) { subscriptionsToRemove.Add(subscription); + continue; } if (subscription.ActionWithArgumentAndArgs != null)