Files
react-native/ReactCommon/react/renderer/components/text/ParagraphEventEmitter.cpp
T
Samuel Susla 412fc7d324 Prevent calling onTextLayout with the same value
Summary:
Changelog: [internal]

In D23648430 (https://github.com/facebook/react-native/commit/a315e4cd30e4b8da841f587650146a62c868f67d) I made a mistake. I prevented calling `onTextLayout` unless there are attachments in the component. It fixed the problem because I unintentionally prevented `onTextLayout`  to be called. Therefore, changes from D23648430 (https://github.com/facebook/react-native/commit/a315e4cd30e4b8da841f587650146a62c868f67d) need to be reverted.

To prevent infinite loop in `onTextLayout`, ParagraphEventEmitter checks if `linesMeasurements` have changed before dispatching it to JS.

Reviewed By: shergin

Differential Revision: D23782717

fbshipit-source-id: 0e84ae4f46d79ce0cf4c7340cd32be6f562ae179
2020-09-20 08:18:55 -07:00

56 lines
1.9 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "ParagraphEventEmitter.h"
namespace facebook {
namespace react {
static jsi::Value linesMeasurementsPayload(
jsi::Runtime &runtime,
LinesMeasurements const &linesMeasurements) {
auto payload = jsi::Object(runtime);
auto lines = jsi::Array(runtime, linesMeasurements.size());
for (size_t i = 0; i < linesMeasurements.size(); ++i) {
auto const &lineMeasurement = linesMeasurements[i];
auto jsiLine = jsi::Object(runtime);
jsiLine.setProperty(runtime, "text", lineMeasurement.text);
jsiLine.setProperty(runtime, "x", lineMeasurement.frame.origin.x);
jsiLine.setProperty(runtime, "y", lineMeasurement.frame.origin.y);
jsiLine.setProperty(runtime, "width", lineMeasurement.frame.size.width);
jsiLine.setProperty(runtime, "height", lineMeasurement.frame.size.height);
jsiLine.setProperty(runtime, "descender", lineMeasurement.descender);
jsiLine.setProperty(runtime, "capHeight", lineMeasurement.capHeight);
jsiLine.setProperty(runtime, "ascender", lineMeasurement.ascender);
jsiLine.setProperty(runtime, "xHeight", lineMeasurement.xHeight);
lines.setValueAtIndex(runtime, i, jsiLine);
}
payload.setProperty(runtime, "lines", lines);
return payload;
}
void ParagraphEventEmitter::onTextLayout(
LinesMeasurements const &linesMeasurements) const {
{
std::lock_guard<std::mutex> guard(linesMeasurementsMutex_);
if (linesMeasurementsMetrics_ == linesMeasurements) {
return;
}
linesMeasurementsMetrics_ = linesMeasurements;
}
dispatchEvent("textLayout", [linesMeasurements](jsi::Runtime &runtime) {
return linesMeasurementsPayload(runtime, linesMeasurements);
});
}
} // namespace react
} // namespace facebook