Ibus needs to be unique on tag as well as type.
This commit is contained in:
1 parent
6f965aa9c3
commit
f647c38db5
2 files changed
+11
-5
No files matched your search
@@ -7,7 +7,7 @@ using System.Threading.Tasks;
|
||||
namespace bts.api
|
||||
{
|
||||
public interface ITag { }
|
||||
public interface ISubscribe : IDisposable
|
||||
public interface ISubscribe
|
||||
{
|
||||
IObservable<T> subscribe<T>(ITag tag);
|
||||
}
|
||||
|
||||
@@ -13,15 +13,21 @@ namespace bts.core
|
||||
public class SingletonBus : IBus {
|
||||
|
||||
private class SubjectSingleton<T> {
|
||||
public static ISubject<T> instance = new Subject<T>();
|
||||
public static Dictionary<ITag, ISubject<T>> instance = new Dictionary<ITag, ISubject<T>>();
|
||||
}
|
||||
|
||||
private ISubject<T> subject<T>(ITag tag) {
|
||||
return SubjectSingleton<T>.instance;
|
||||
var inst = SubjectSingleton<T>.instance;
|
||||
lock (inst) {
|
||||
if (inst.TryGetValue(tag, out ISubject<T> result)) {
|
||||
return result;
|
||||
}
|
||||
result = new Subject<T>();
|
||||
inst[tag] = result;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() { }
|
||||
|
||||
public void post<T>(ITag tag, T data) {
|
||||
subject<T>(tag).OnNext(data);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user