Suppose I have some class MyClass. In one part of my code I want something like this:
Observable.Subscribe<MyClass>(myClass => DoSomething(myClass));
Then in another place (file/project/time) I have something like this:
Observable.Publish(instanceOfMyClass);
This second line triggers all the methods that were subscribed with that exact class type. Is this something that the Reactive Extensions (v1 or v2) supports?
It would be useful to specify a SynchronizationContext as part of the Subscribe call. It would also be nice to specify there whether or not the method should be held with a WeakReference. And the Publish method should have the ability to do it all synchronously or give me something I can wait on.
This wouldn’t be hard to create.
You just need an internal
Dictionary<Type, Object>and use it to store eachSubject<T>(as the object) byType.You can then just write the two
SubscribeandPublishmethods to work off of the internal dictionary.Should be quite simple in fact.
Rather than just say it was simple I thought I’d give it a go.
Here’s my Rx Pub/Sub class:
And this is how it is used:
The result is that this code will write
2to the console only.Enjoy!