Convert folly::dynamic event dispatch to r-value (#50133)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/50133

Allow move semantics for folly::dynamic event dispatching to avoid copying the folly::dynamic when dispatched.

## Changelog

[General][Breaking] - Dispatch folly::dynamic events with r-value instead of l-value

Reviewed By: NickGerleman

Differential Revision: D71423497

fbshipit-source-id: 5435772e14b025aab97d34df9983b91fd7285fd0
This commit is contained in:
Eric Rozell
2025-03-20 04:57:36 -07:00
committed by Facebook GitHub Bot
parent 146d809b6b
commit 12e5df844b
10 changed files with 14 additions and 67 deletions
@@ -212,12 +212,11 @@ static NSString *const kRCTLegacyInteropChildIndexKey = @"index";
if (!_adapter) {
_adapter = [[RCTLegacyViewManagerInteropCoordinatorAdapter alloc] initWithCoordinator:[self _coordinator]
reactTag:self.tag];
_adapter.eventInterceptor = ^(std::string eventName, folly::dynamic event) {
_adapter.eventInterceptor = ^(std::string eventName, folly::dynamic &&event) {
if (weakSelf) {
__typeof(self) strongSelf = weakSelf;
const auto &eventEmitter =
static_cast<const LegacyViewManagerInteropViewEventEmitter &>(*strongSelf->_eventEmitter);
eventEmitter.dispatchEvent(eventName, event);
const auto &eventEmitter = static_cast<const ViewEventEmitter &>(*strongSelf->_eventEmitter);
eventEmitter.dispatchEvent(eventName, std::move(event));
}
};
// Set props immediately. This is required to set the initial state of the view.
@@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (strong, nonatomic) UIView *paperView;
@property (nonatomic, copy, nullable) void (^eventInterceptor)(std::string eventName, folly::dynamic event);
@property (nonatomic, copy, nullable) void (^eventInterceptor)(std::string eventName, folly::dynamic &&event);
- (void)setProps:(const folly::dynamic &)props;
@@ -36,9 +36,9 @@
_paperView = [_coordinator createPaperViewWithTag:_tag];
__weak __typeof(self) weakSelf = self;
[_coordinator addObserveForTag:_tag
usingBlock:^(std::string eventName, folly::dynamic event) {
usingBlock:^(std::string eventName, folly::dynamic &&event) {
if (weakSelf.eventInterceptor) {
weakSelf.eventInterceptor(eventName, event);
weakSelf.eventInterceptor(eventName, std::move(event));
}
}];
}
@@ -8,7 +8,6 @@
#pragma once
#include <react/renderer/components/legacyviewmanagerinterop/LegacyViewManagerInteropState.h>
#include <react/renderer/components/legacyviewmanagerinterop/LegacyViewManagerInteropViewEventEmitter.h>
#include <react/renderer/components/legacyviewmanagerinterop/LegacyViewManagerInteropViewProps.h>
#include <react/renderer/components/view/ConcreteViewShadowNode.h>
@@ -19,7 +18,7 @@ extern const char LegacyViewManagerInteropComponentName[];
using LegacyViewManagerInteropShadowNode = ConcreteViewShadowNode<
LegacyViewManagerInteropComponentName,
LegacyViewManagerInteropViewProps,
LegacyViewManagerInteropViewEventEmitter,
ViewEventEmitter,
LegacyViewManagerInteropState>;
} // namespace facebook::react
@@ -1,18 +0,0 @@
/*
* 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 "LegacyViewManagerInteropViewEventEmitter.h"
#include <iostream>
namespace facebook::react {
void LegacyViewManagerInteropViewEventEmitter::dispatchEvent(
const std::string& type,
const folly::dynamic& payload) const {
EventEmitter::dispatchEvent(type, payload);
}
} // namespace facebook::react
@@ -1,31 +0,0 @@
/*
* 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 <memory>
#include <folly/dynamic.h>
#include <react/renderer/components/view/ViewEventEmitter.h>
#include <react/renderer/core/EventEmitter.h>
namespace facebook::react {
class LegacyViewManagerInteropViewEventEmitter;
using SharedLegacyViewManagerInteropViewEventEmitter =
std::shared_ptr<const LegacyViewManagerInteropViewEventEmitter>;
class LegacyViewManagerInteropViewEventEmitter : public ViewEventEmitter {
public:
using ViewEventEmitter::ViewEventEmitter;
void dispatchEvent(const std::string& type, const folly::dynamic& payload)
const;
};
} // namespace facebook::react
@@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
@class RCTBridge;
@class RCTBridgeProxy;
typedef void (^InterceptorBlock)(std::string eventName, folly::dynamic event);
typedef void (^InterceptorBlock)(std::string eventName, folly::dynamic &&event);
@interface RCTLegacyViewManagerInteropCoordinator : NSObject
@@ -7,7 +7,6 @@
#pragma once
#include <react/renderer/components/legacyviewmanagerinterop/LegacyViewManagerInteropViewEventEmitter.h>
#include <react/renderer/components/legacyviewmanagerinterop/LegacyViewManagerInteropViewProps.h>
#include <react/renderer/components/view/ConcreteViewShadowNode.h>
@@ -57,20 +57,20 @@ EventEmitter::EventEmitter(
void EventEmitter::dispatchEvent(
std::string type,
const folly::dynamic& payload,
folly::dynamic&& payload,
RawEvent::Category category) const {
dispatchEvent(
std::move(type),
std::make_shared<DynamicEventPayload>(folly::dynamic(payload)),
std::make_shared<DynamicEventPayload>(std::move(payload)),
category);
}
void EventEmitter::dispatchUniqueEvent(
std::string type,
const folly::dynamic& payload) const {
folly::dynamic&& payload) const {
dispatchUniqueEvent(
std::move(type),
std::make_shared<DynamicEventPayload>(folly::dynamic(payload)));
std::make_shared<DynamicEventPayload>(std::move(payload)));
}
void EventEmitter::dispatchEvent(
@@ -86,7 +86,7 @@ class EventEmitter {
void dispatchEvent(
std::string type,
const folly::dynamic& payload,
folly::dynamic&& payload,
RawEvent::Category category = RawEvent::Category::Unspecified) const;
void dispatchEvent(
@@ -94,8 +94,7 @@ class EventEmitter {
SharedEventPayload payload,
RawEvent::Category category = RawEvent::Category::Unspecified) const;
void dispatchUniqueEvent(std::string type, const folly::dynamic& payload)
const;
void dispatchUniqueEvent(std::string type, folly::dynamic&& payload) const;
void dispatchUniqueEvent(
std::string type,