Skip to content

Commit

Permalink
Pass EventDispatcher listeners by value (facebook#45077)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#45077

Changelog: [Internal]

Reviewed By: sammy-SC

Differential Revision: D58791938

fbshipit-source-id: 079fbfad4dd309a41c360e1bdb93e56f8793e6b3
  • Loading branch information
javache authored and facebook-github-bot committed Jun 20, 2024
1 parent a2cb343 commit 2e6f105
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ void EventDispatcher::dispatchUniqueEvent(RawEvent&& rawEvent) const {
if (eventListeners_.willDispatchEvent(rawEvent)) {
return;
}

eventQueue_.enqueueUniqueEvent(std::move(rawEvent));
}

void EventDispatcher::addListener(
const std::shared_ptr<const EventListener>& listener) const {
eventListeners_.addListener(listener);
std::shared_ptr<const EventListener> listener) const {
eventListeners_.addListener(std::move(listener));
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class EventDispatcher {
/*
* Adds provided event listener to the event dispatcher.
*/
void addListener(const std::shared_ptr<const EventListener>& listener) const;
void addListener(std::shared_ptr<const EventListener> listener) const;

/*
* Removes provided event listener to the event dispatcher.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ bool EventListenerContainer::willDispatchEvent(const RawEvent& event) {

bool handled = false;
for (const auto& listener : eventListeners_) {
handled = handled || listener->operator()(event);
handled = (*listener)(event);
if (handled) {
break;
}
}
return handled;
}

void EventListenerContainer::addListener(
const std::shared_ptr<const EventListener>& listener) {
std::shared_ptr<const EventListener> listener) {
std::unique_lock lock(mutex_);

eventListeners_.push_back(listener);
eventListeners_.push_back(std::move(listener));
}

void EventListenerContainer::removeListener(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class EventListenerContainer {
*/
bool willDispatchEvent(const RawEvent& event);

void addListener(const std::shared_ptr<const EventListener>& listener);
void addListener(std::shared_ptr<const EventListener> listener);
void removeListener(const std::shared_ptr<const EventListener>& listener);

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,9 @@ std::shared_ptr<UIManager> Scheduler::getUIManager() const {
}

void Scheduler::addEventListener(
const std::shared_ptr<const EventListener>& listener) {
std::shared_ptr<const EventListener> listener) {
if (eventDispatcher_->has_value()) {
eventDispatcher_->value().addListener(listener);
eventDispatcher_->value().addListener(std::move(listener));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Scheduler final : public UIManagerDelegate {
void reportMount(SurfaceId surfaceId) const;

#pragma mark - Event listeners
void addEventListener(const std::shared_ptr<const EventListener>& listener);
void addEventListener(std::shared_ptr<const EventListener> listener);
void removeEventListener(
const std::shared_ptr<const EventListener>& listener);

Expand Down

0 comments on commit 2e6f105

Please sign in to comment.