Comments (5)
I intend to keep the interface generic to avoid boxing but you can use the GlobalHandler in your specific case:
hub.RegisterGlobalHandler((type, eventObject) => Console.WriteLine($"Type: {type} - Event: {eventObject}"));
from easy.messagehub.
This won't work because the event object is not known until the Publish
phase.
I looked through the code and saw some ways to do it. Can we do this:
Make the Register method:
internal static Guid Register(TimeSpan throttleBy, Action<object> action, Type type)
{
var key = Guid.NewGuid();
var subscription = new Subscription(type, key, throttleBy, action);
lock (AllSubscriptions)
{
AllSubscriptions.Add(subscription);
_subscriptionsChangeCounter++;
}
return key;
}
These would now be the Subscribe
overloads:
public Guid Subscribe<T>(Action<T> action) => Subscribe(action, TimeSpan.Zero, typeof(T));
public Guid Subscribe<T>(Action<T> action, TimeSpan throttleBy) => Subscribe(action, throttleBy, typeof(T));
public Guid Subscribe(Action<object> action, TimeSpan throttleBy, Type type)
{
EnsureNotNull(action);
return Subscriptions.Register(throttleBy, action, type);
}
from easy.messagehub.
It does work, RegisterGlobalHandler receives every message published. You will have the type as well as the payload.
from easy.messagehub.
Okay, just tested and it works just for one Type
.. I have multiple Type
s ... So this won't work with my use case.
This is my test code:
_hub.RegisterGlobalHandler((type, eventObject) =>
{
if (type == externalType)
{
listener.Handle(eventObject);
}
});
Each time RegisterGlobalHandler
is called, it overrides the last one.
Any other way to accomplish this?
What do you think of my suggestion above?
from easy.messagehub.
It does work, I am not going to change the interface (add or remove).
The purpose of RegisterGlobalHandler is to register 1 single handler which will receive every message of every type passed through the hub. It is not meant to build up multiple handlers.
In your case you can compose a handler which knows how to behave based on the types that you are interested in at run-time then register that as the global handler.
_hub.RegisterGlobalHandler((type, eventObject) =>
{
if (type == firstType)
{
// handle eventObj
}
else if (type == secondType)
{
// handle eventObj
}
...
});
from easy.messagehub.
Related Issues (20)
- Registering IMessageHub with MessageHub HOT 1
- Subscriber is receiving 3 messages when publisher is only sending 1 HOT 7
- Publish & Subscribe from different Project? HOT 2
- Non-singleton instances HOT 8
- Sometimes Unsubscribe method hangs HOT 6
- How to setup one Server with Many cleints between ASP Core web and .net Console service HOT 1
- System.ObjectDisposedException: ThreadLocal object has been deleted. HOT 1
- NuGet package?
- Async handlers HOT 2
- Get MessageHub to play nicely with Autofac in a web application HOT 1
- Conditional subscription HOT 6
- Unflexible detection of the given type HOT 4
- Support for async actions HOT 2
- Support for more profiles? HOT 3
- Available for dotnet core? HOT 2
- Add async version HOT 6
- MessageHub is holding objects in Memory HOT 18
- UnSubscribe method causes ArgumentNullException HOT 1
- Publish and subscribe from different threads HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from easy.messagehub.