Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event listeners should be lambda friendly #236

Open
bradphelan opened this issue Feb 29, 2024 · 5 comments
Open

Event listeners should be lambda friendly #236

bradphelan opened this issue Feb 29, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@bradphelan
Copy link

bradphelan commented Feb 29, 2024

Currently it's not possible to pass lambdas as event listeners

void EventDispatcher::addEventListener(const std::string& type, EventListener* listener) 

You enforce a virtual base type and also force to pass by pointer. Both of these make it impossible to use lambdas. This makes the code much more noisy forcing users to create classes instead of simple callbacks. Instead of a base class

    struct EventListener {

        virtual void onEvent(Event& event) = 0;

        virtual ~EventListener() = default;
    };

It would be better simply to define

    using EventListener = std::function<void(Event&event)>;

and pass and store the callback by value. The problem then becomes how to remove the event. Easy is to use a std::shared_ptr with and return it. So we end up with

    std::shared_ptr<EventListenerHandle> addEventListener(std::function<void(Event&event)>);

The user then hangs onto the EventListenerHandle till they don't need it anymore. As soon as the last copy of this goes out of scope then the listener will be removed. I know you want to make this like threejs but c++ is different and you should take advantage of RAII lifetimes and lambdas.

appModel.listenerSubscription =  mesh.addEventListener(
    [](Event & event){ std::cout << "my mesh was clicked" << std::endl; }
);
@markaren
Copy link
Owner

Yeah, I totally agree that the EventListener API needs modifications (it was worse before). I'll take your suggestions into consideration.

@markaren
Copy link
Owner

markaren commented Feb 29, 2024

While I like your idea with a handler, I hope the commits in https://github.com/markaren/threepp/tree/refactor_evt would be a step in the right direction.

@markaren
Copy link
Owner

I think the main issue with the handle suggestion for this particular API is that internally one listener is re-used N times. I would then have to store the result in a container etc. So internally, the usage becomes messier.

@bradphelan
Copy link
Author

bradphelan commented Feb 29, 2024 via email

@bradphelan
Copy link
Author

bradphelan commented Feb 29, 2024 via email

@markaren markaren added the enhancement New feature or request label Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants