Skip to content

Commit 7225d43

Browse files
javachemeta-codesync[bot]
authored andcommitted
Remove default payload factory static storage (#59517)
Summary: Pull Request resolved: #59517 Return the stateless payload callable directly instead of caching a std::function, avoiding its dynamic static initialization and teardown without changing the factory API or payload behaviour. Changelog: [Internal] ___ landed-with-radar-review Differential Revision: D127583053 fbshipit-source-id: ac9e3b0dd13ef749b34a185d120ec09ca81a7530
1 parent ab6f07f commit 7225d43

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

‎packages/react-native/ReactCommon/react/renderer/core/EventEmitter.cpp‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ std::mutex& EventEmitter::DispatchMutex() {
4444
}
4545

4646
ValueFactory EventEmitter::defaultPayloadFactory() {
47-
static auto payloadFactory =
48-
ValueFactory{[](jsi::Runtime& runtime) { return jsi::Object(runtime); }};
49-
return payloadFactory;
47+
return [](jsi::Runtime& runtime) { return jsi::Object(runtime); };
5048
}
5149

5250
EventEmitter::EventEmitter(
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include <gtest/gtest.h>
9+
#include <hermes/hermes.h>
10+
#include <react/renderer/core/EventEmitter.h>
11+
12+
namespace facebook::react {
13+
14+
TEST(EventEmitterTest, defaultPayloadFactoryCreatesFreshEmptyObjects) {
15+
auto runtime = hermes::makeHermesRuntime();
16+
auto factory = EventEmitter::defaultPayloadFactory();
17+
18+
auto first = factory(*runtime).getObject(*runtime);
19+
EXPECT_EQ(first.getPropertyNames(*runtime).size(*runtime), 0u);
20+
first.setProperty(*runtime, "mutation", 42);
21+
EXPECT_EQ(first.getProperty(*runtime, "mutation").getNumber(), 42);
22+
23+
auto second = factory(*runtime).getObject(*runtime);
24+
EXPECT_EQ(second.getPropertyNames(*runtime).size(*runtime), 0u);
25+
EXPECT_FALSE(second.hasProperty(*runtime, "mutation"));
26+
}
27+
28+
TEST(EventEmitterTest, defaultPayloadFactoryUsesSuppliedRuntime) {
29+
auto firstRuntime = hermes::makeHermesRuntime();
30+
auto secondRuntime = hermes::makeHermesRuntime();
31+
auto factory = EventEmitter::defaultPayloadFactory();
32+
33+
auto first = factory(*firstRuntime).getObject(*firstRuntime);
34+
first.setProperty(*firstRuntime, "mutation", 42);
35+
36+
auto second = factory(*secondRuntime).getObject(*secondRuntime);
37+
EXPECT_EQ(second.getPropertyNames(*secondRuntime).size(*secondRuntime), 0u);
38+
EXPECT_FALSE(second.hasProperty(*secondRuntime, "mutation"));
39+
EXPECT_EQ(first.getProperty(*firstRuntime, "mutation").getNumber(), 42);
40+
}
41+
42+
} // namespace facebook::react

0 commit comments

Comments
 (0)