forked from microsoft/react-native-macos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add event listeners to Scheduler (microsoft#1481)
Summary: Minimal set of changes to intercept events in external modules. Current intended use-case is for Reanimated to handle events for the Animated properties. Changelog: [Added] Add listeners to allow intercepting events in C++ core. Reviewed By: cipolleschi Differential Revision: D35312534 fbshipit-source-id: ec924b57fd0c0dabf7be7b886dbef23bf3170d6c # Conflicts: # packages/rn-tester/Podfile.lock Co-authored-by: Nicola Corti <[email protected]>
- Loading branch information
1 parent
1a129bc
commit 1e57c15
Showing
6 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include "EventListener.h" | ||
|
||
namespace facebook::react { | ||
|
||
bool EventListenerContainer::willDispatchEvent(const RawEvent &event) { | ||
std::shared_lock<butter::shared_mutex> lock(mutex_); | ||
|
||
bool handled = false; | ||
for (auto const &listener : eventListeners_) { | ||
handled = handled || listener->operator()(event); | ||
} | ||
return handled; | ||
} | ||
|
||
void EventListenerContainer::addListener( | ||
const std::shared_ptr<EventListener const> &listener) { | ||
std::unique_lock<butter::shared_mutex> lock(mutex_); | ||
|
||
eventListeners_.push_back(listener); | ||
} | ||
|
||
void EventListenerContainer::removeListener( | ||
const std::shared_ptr<EventListener const> &listener) { | ||
std::unique_lock<butter::shared_mutex> lock(mutex_); | ||
|
||
auto it = std::find(eventListeners_.begin(), eventListeners_.end(), listener); | ||
if (it != eventListeners_.end()) { | ||
eventListeners_.erase(it); | ||
} | ||
} | ||
|
||
} // namespace facebook::react |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include <react/renderer/core/RawEvent.h> | ||
|
||
#include <butter/mutex.h> | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
/** | ||
* Listener for events dispatched to JS runtime. | ||
* Return `true` to interrupt default dispatch to JS event emitter, `false` to | ||
* pass through to default handlers. | ||
*/ | ||
using EventListener = std::function<bool(const RawEvent &event)>; | ||
|
||
class EventListenerContainer { | ||
public: | ||
/* | ||
* Invoke listeners in this container with the event. | ||
* Returns true if event was handled by the listener, false to continue | ||
* default dispatch. | ||
*/ | ||
bool willDispatchEvent(const RawEvent &event); | ||
|
||
void addListener(const std::shared_ptr<EventListener const> &listener); | ||
void removeListener(const std::shared_ptr<EventListener const> &listener); | ||
|
||
private: | ||
butter::shared_mutex mutex_; | ||
std::vector<std::shared_ptr<EventListener const>> eventListeners_; | ||
}; | ||
|
||
} // namespace react | ||
} // namespace facebook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters