GithubHelp home page GithubHelp logo

Remove Listener Example about eventpp HOT 5 CLOSED

DarkTyger avatar DarkTyger commented on July 17, 2024
Remove Listener Example

from eventpp.

Comments (5)

wqking avatar wqking commented on July 17, 2024

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.

DarkTyger avatar DarkTyger commented on July 17, 2024

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.

wqking avatar wqking commented on July 17, 2024

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.

DarkTyger avatar DarkTyger commented on July 17, 2024

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.

wqking avatar wqking commented on July 17, 2024

The documents cover all information you need. The example code won't be changed.

from eventpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.