Fabric: Using shared pointer for event counter in ViewEventEmitter

Summary:
The callback `dispatchEvent` is called asynchronously on the JavaScript thread, so all data it uses must be copied to the lambda. To work around this constraint we use a shared pointer for the counter.

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: sammy-SC

Differential Revision: D24598840

fbshipit-source-id: fb5581858d54dc806863caf0c7c4f612ed6046e2
This commit is contained in:
Valentin Shergin
2020-10-28 19:48:10 -07:00
committed by Facebook GitHub Bot
parent aead26dddd
commit 306a8adade
2 changed files with 8 additions and 6 deletions
@@ -46,17 +46,17 @@ void ViewEventEmitter::onLayout(const LayoutMetrics &layoutMetrics) const {
lastLayoutMetrics_ = layoutMetrics;
}
std::atomic_uint_fast8_t *eventCounter = &eventCounter_;
uint_fast8_t expectedEventCount = ++*eventCounter;
auto expectedEventCount = ++*eventCounter_;
// dispatchUniqueEvent only drops consecutive onLayout events to the same
// node. We want to drop *any* unprocessed onLayout events when there's a
// newer one.
dispatchEvent(
"layout",
[frame = layoutMetrics.frame, expectedEventCount, eventCounter](
jsi::Runtime &runtime) {
uint_fast8_t actualEventCount = eventCounter->load();
[frame = layoutMetrics.frame,
expectedEventCount,
eventCounter = eventCounter_](jsi::Runtime &runtime) {
auto actualEventCount = eventCounter->load();
if (expectedEventCount != actualEventCount) {
// Drop stale events
return jsi::Value::null();
@@ -40,7 +40,9 @@ class ViewEventEmitter : public TouchEventEmitter {
private:
mutable std::mutex layoutMetricsMutex_;
mutable LayoutMetrics lastLayoutMetrics_;
mutable std::atomic_uint_fast8_t eventCounter_{0};
mutable std::shared_ptr<std::atomic_uint_fast8_t> eventCounter_{
std::make_shared<std::atomic_uint_fast8_t>(0)};
};
} // namespace react