Comments (5)
The example code doesn't need to include every functions. You may refer to the document for how to remove listeners.
And you may like the utility class ScopedRemover.
from eventpp.
The example code doesn't need to include every functions.
It took me hours to figure out the syntax for how to remove listeners, mostly because it wasn't clear to me that we needed to capture the subscription token from appendListener
. Having a working example for calling queue.removeListener
would have saved me a lot of time. The solution I came up with is O(n), though.
Certainly, not every function needs to have an example. An example for removing listeners would likely save others a fair amount of time. Especially if an O(1) solution for removing listeners is possible.
FWIW, I've read the documentation and it didn't help me. I also looked at ScopedRemover
and didn't understand how to use it within the context of how we're using eventpp. There are many technical details missing. That's why examples are useful. Here's the API we've exposed:
using namespace eventpp::internal_;
class EventBus {
private:
using Listener = std::function<void( const EventPointer& )>;
using Callback = CallbackListBase<void( const EventPointer& ), EventPolicy>;
using Tuple = std::vector<std::pair<Callback::Handle, Listener>>;
std::unordered_map<EventType, Tuple> mSubscriptions;
public:
void subscribe( EventType eventType, const Listener& listener ) {
auto subscription = sBus.mQueue.appendListener( eventType, listener );
sBus.mSubscriptions[ eventType ].emplace_back( subscription, listener );
}
void unsubscribe( EventType eventType, const Listener& listener ) {
auto iterator = sBus.mSubscriptions.find( eventType );
if( iterator != sBus.mSubscriptions.end() ) {
auto& listeners = iterator->second;
for( auto i = listeners.begin(); i != listeners.end(); i++ ) {
if( i->second.target_type() == listener.target_type() ) {
sBus.mQueue.removeListener( eventType, i->first );
listeners.erase( i );
break;
}
}
}
}
This allows a logger to subscribe to all events, then unsubscribe to certain events that we don't want to log:
void EventLogger::subscribe() {
for( int i = (int)EventType::started; i < (int)EventType::LAST; i++ ) {
auto et = static_cast<EventType>( i );
EventBus::subscribe( et, handle );
}
EventBus::unsubscribe( EventType::rtp_payload, handle );
EventBus::unsubscribe( EventType::audio, handle );
}
void EventLogger::handle( const std::unique_ptr<Event>& event ) {
...
}
While this works, you can see that calling unsubscribe
isn't O(1), but O(n) because we loop through all the subscriptions. An example for how to use eventpp to properly remove listeners in O(1) time would be quite helpful.
Also, relying on eventpp::internal_
doesn't feel future-proof. My understanding is that internal_
implies the underlying implementation could change with subsequent releases and that code should not depend on anything in that namespace.
from eventpp.
I've improved the EventQueue document, added a link to EventDispatcher for how to add/remove listeners.
You never need eventpp::internal_, seeing you are using CallbackListBase instead of CallbackList, and implement your own EventBus instead of using EventQueue or EventDispatcher, apparently you didn't read any eventpp document. Please read the document, you can't get all knowledge from sample code.
from eventpp.
apparently you didn't read any eventpp document
That's an accusatory statement, which is neither necessary nor correct.
We read the documentation, especially the helpful eventqueue.md page. We implemented an EventBus, which is a thin wrapper around an EventQueue, to avoid coupling the rest of the system to eventpp, in the case we need to switch out for a different message dispatch system. Effectively we have the following:
class EventBus {
private:
using Listener = std::function<void( const EventPointer& )>;
using Callback = CallbackListBase<void( const EventPointer& ), EventPolicy>;
using Tuple = std::vector<std::pair<Callback::Handle, Listener>>;
eventpp::EventQueue<EventType, void( const EventPointer& ), EventPolicy> mQueue;
std::unordered_map<EventType, Tuple> mSubscriptions;
I've since swapped CallbackListBase
for CallbackList
, thank you.
from eventpp.
The documents cover all information you need. The example code won't be changed.
from eventpp.
Related Issues (20)
- Clang compile error HOT 2
- [Question] Are event cbs, after a dispatch, processed async or sync? HOT 2
- Thread safety ? HOT 2
- Confusion about the parameters of the `eventpp::EventQueue.enqueue` HOT 1
- Run unittest failed HOT 4
- What is the difference between eventpp and libevent/libuv/libev HOT 1
- Dispatcher模板类型限定,如何适配任意的事件 HOT 1
- Heter Dispatchers - why aren't they default? HOT 4
- 如何实现不定参数调用? HOT 2
- New version release (vcpkg) HOT 3
- Function in CallbackList for removing all callbacks HOT 3
- Documentation for eventmaker is out of date HOT 1
- Compilation failed when using 'counterRemover' for the 'HeterEventDispatcher' of two function lists HOT 2
- Break on receiving false from callback/event dispatch? HOT 2
- Conan instructions HOT 5
- shadowing error HOT 2
- Could EventQueue.add_listener be called in a thread other than the event processing thread? HOT 3
- Correct checking whether queue is empty HOT 2
- If it is possible to control the order in which listeners are invoked? 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 eventpp.