Skip to content

Commit 50f2dd9

Browse files
sammy-SCfacebook-github-bot
authored andcommittedJan 11, 2021
Make layout event asynchronous unbatched
Summary: Changelog: [internal] Making "layout" event asynchronous unbatched will bring the behaviour closer to Paper. Paper puts "layout" event in the event queue from ShadowQueue and schedules a flush of event queue on JavaScript queue. Before this, Fabric puts "layout" event in the event queue from JavaScript queue, waits until main run loop is about to go to sleep and then schedules a flush of event queue on JavaScript thread. Now Fabric no longer waits for main run loop to go to sleep but induces a flush of event queue on JavaScript thread right away. Reviewed By: JoshuaGross, shergin Differential Revision: D25849468 fbshipit-source-id: 366e99364507f7bc1d09325c04a604bcb2043e3d
1 parent e37e56b commit 50f2dd9

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed
 

‎ReactCommon/react/renderer/components/view/ViewEventEmitter.cpp

+34-31
Original file line numberDiff line numberDiff line change
@@ -79,37 +79,40 @@ void ViewEventEmitter::onLayout(const LayoutMetrics &layoutMetrics) const {
7979
layoutEventState->isDispatching = true;
8080
}
8181

82-
dispatchEvent("layout", [layoutEventState](jsi::Runtime &runtime) {
83-
auto frame = Rect{};
84-
85-
{
86-
std::lock_guard<std::mutex> guard(layoutEventState->mutex);
87-
88-
layoutEventState->isDispatching = false;
89-
90-
// If some *particular* `frame` was already dispatched before,
91-
// and since then there were no other new values of the `frame` observed,
92-
// do nothing.
93-
if (layoutEventState->wasDispatched) {
94-
return jsi::Value::null();
95-
}
96-
97-
frame = layoutEventState->frame;
98-
99-
// If some *particular* `frame` was *not* already dispatched before,
100-
// it's time to dispatch it and mark as dispatched.
101-
layoutEventState->wasDispatched = true;
102-
}
103-
104-
auto layout = jsi::Object(runtime);
105-
layout.setProperty(runtime, "x", frame.origin.x);
106-
layout.setProperty(runtime, "y", frame.origin.y);
107-
layout.setProperty(runtime, "width", frame.size.width);
108-
layout.setProperty(runtime, "height", frame.size.height);
109-
auto payload = jsi::Object(runtime);
110-
payload.setProperty(runtime, "layout", std::move(layout));
111-
return jsi::Value(std::move(payload));
112-
});
82+
dispatchEvent(
83+
"layout",
84+
[layoutEventState](jsi::Runtime &runtime) {
85+
auto frame = Rect{};
86+
87+
{
88+
std::lock_guard<std::mutex> guard(layoutEventState->mutex);
89+
90+
layoutEventState->isDispatching = false;
91+
92+
// If some *particular* `frame` was already dispatched before,
93+
// and since then there were no other new values of the `frame`
94+
// observed, do nothing.
95+
if (layoutEventState->wasDispatched) {
96+
return jsi::Value::null();
97+
}
98+
99+
frame = layoutEventState->frame;
100+
101+
// If some *particular* `frame` was *not* already dispatched before,
102+
// it's time to dispatch it and mark as dispatched.
103+
layoutEventState->wasDispatched = true;
104+
}
105+
106+
auto layout = jsi::Object(runtime);
107+
layout.setProperty(runtime, "x", frame.origin.x);
108+
layout.setProperty(runtime, "y", frame.origin.y);
109+
layout.setProperty(runtime, "width", frame.size.width);
110+
layout.setProperty(runtime, "height", frame.size.height);
111+
auto payload = jsi::Object(runtime);
112+
payload.setProperty(runtime, "layout", std::move(layout));
113+
return jsi::Value(std::move(payload));
114+
},
115+
EventPriority::AsynchronousUnbatched);
113116
}
114117

115118
} // namespace react

0 commit comments

Comments
 (0)
Please sign in to comment.