Files
react-native/ReactCommon/fabric/components/scrollview/ScrollViewEventEmitter.cpp
T
Samuel Susla b31149cf90 Introduce BatchedEventQueue::enqueueUniqueEvent
Summary:
Changelog: [Internal]

`BatchedEventQueue::enqueueUniqueEvent` goes over event queue and deletes previous event of the same type and same target.

This is useful for ScrollView for example where only the latest event is relevant.
This only affects ScrollView scroll event, other events take the original code path.

Reviewed By: mdvacca

Differential Revision: D21648906

fbshipit-source-id: a80ad652058fd50ebb55e24a87229cdc1764b591
2020-05-21 11:56:22 -07:00

102 lines
3.2 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 "ScrollViewEventEmitter.h"
namespace facebook {
namespace react {
static jsi::Value scrollViewMetricsPayload(
jsi::Runtime &runtime,
const ScrollViewMetrics &scrollViewMetrics) {
auto payload = jsi::Object(runtime);
{
auto contentOffset = jsi::Object(runtime);
contentOffset.setProperty(runtime, "x", scrollViewMetrics.contentOffset.x);
contentOffset.setProperty(runtime, "y", scrollViewMetrics.contentOffset.y);
payload.setProperty(runtime, "contentOffset", contentOffset);
}
{
auto contentInset = jsi::Object(runtime);
contentInset.setProperty(
runtime, "top", scrollViewMetrics.contentInset.top);
contentInset.setProperty(
runtime, "left", scrollViewMetrics.contentInset.left);
contentInset.setProperty(
runtime, "bottom", scrollViewMetrics.contentInset.bottom);
contentInset.setProperty(
runtime, "right", scrollViewMetrics.contentInset.right);
payload.setProperty(runtime, "contentInset", contentInset);
}
{
auto contentSize = jsi::Object(runtime);
contentSize.setProperty(
runtime, "width", scrollViewMetrics.contentSize.width);
contentSize.setProperty(
runtime, "height", scrollViewMetrics.contentSize.height);
payload.setProperty(runtime, "contentSize", contentSize);
}
{
auto containerSize = jsi::Object(runtime);
containerSize.setProperty(
runtime, "width", scrollViewMetrics.containerSize.width);
containerSize.setProperty(
runtime, "height", scrollViewMetrics.containerSize.height);
payload.setProperty(runtime, "layoutMeasurement", containerSize);
}
payload.setProperty(runtime, "zoomScale", scrollViewMetrics.zoomScale);
return payload;
}
void ScrollViewEventEmitter::onScroll(
const ScrollViewMetrics &scrollViewMetrics) const {
dispatchUniqueEvent("scroll", [scrollViewMetrics](jsi::Runtime &runtime) {
return scrollViewMetricsPayload(runtime, scrollViewMetrics);
});
}
void ScrollViewEventEmitter::onScrollBeginDrag(
const ScrollViewMetrics &scrollViewMetrics) const {
dispatchScrollViewEvent("scrollBeginDrag", scrollViewMetrics);
}
void ScrollViewEventEmitter::onScrollEndDrag(
const ScrollViewMetrics &scrollViewMetrics) const {
dispatchScrollViewEvent("scrollEndDrag", scrollViewMetrics);
}
void ScrollViewEventEmitter::onMomentumScrollBegin(
const ScrollViewMetrics &scrollViewMetrics) const {
dispatchScrollViewEvent("momentumScrollBegin", scrollViewMetrics);
}
void ScrollViewEventEmitter::onMomentumScrollEnd(
const ScrollViewMetrics &scrollViewMetrics) const {
dispatchScrollViewEvent("momentumScrollEnd", scrollViewMetrics);
}
void ScrollViewEventEmitter::dispatchScrollViewEvent(
const std::string &name,
const ScrollViewMetrics &scrollViewMetrics,
EventPriority priority) const {
dispatchEvent(
name,
[scrollViewMetrics](jsi::Runtime &runtime) {
return scrollViewMetricsPayload(runtime, scrollViewMetrics);
},
priority);
}
} // namespace react
} // namespace facebook