Skip to content

Commit 847f42f

Browse files
committed
refactor: clean up ShadowTreeUpdateManager 3/n
1 parent 906ce4e commit 847f42f

2 files changed

Lines changed: 54 additions & 63 deletions

File tree

cpp/ShadowTreeUpdateManager.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <cassert>
1717

1818
namespace margelo::nitro::cssnitro {
19-
2019
using jsi::Runtime;
2120
using reactnativecss::Observable;
2221
namespace nitro_ns = ::margelo::nitro;
@@ -81,7 +80,6 @@ namespace margelo::nitro::cssnitro {
8180
Runtime &runtime,
8281
const nitro_ns::AnyMap &entry) {
8382
folly::dynamic obj = folly::dynamic::object();
84-
// Iterate key-value pairs from AnyMap
8583
for (const auto &kv: entry.getMap()) {
8684
const auto &v = static_cast<const nitro_ns::VariantType &>(kv.second);
8785
auto dynVal = convert(self, runtime, v);
@@ -121,7 +119,6 @@ namespace margelo::nitro::cssnitro {
121119
obs = Observable<UpdatesMap>::create(UpdatesMap{});
122120
}
123121

124-
// Convert each style entry into a dynamic update object (merged into a single object per tag)
125122
folly::dynamic payload = folly::dynamic::object();
126123
for (const auto &p: styleEntries) {
127124
if (!p) {
@@ -154,17 +151,19 @@ namespace margelo::nitro::cssnitro {
154151
}
155152
if (runtime_effects_.find(rt) == runtime_effects_.end()) {
156153
auto obs = rtObsIt->second;
157-
auto holder = std::make_shared<RuntimeEffectHolder>();
158-
holder->effect = std::make_shared<reactnativecss::Effect>(
159-
[obs, rt, holder]() {
160-
const ShadowTreeUpdateManager::UpdatesMap &updates = obs->get(
161-
*holder->effect);
154+
auto weakBox = std::make_shared<std::weak_ptr<reactnativecss::Effect>>();
155+
auto effect = std::make_shared<reactnativecss::Effect>(
156+
[obs, rt, weakBox]() {
157+
auto eff = weakBox->lock();
158+
if (!eff) return;
159+
const ShadowTreeUpdateManager::UpdatesMap &updates = obs->get(*eff);
162160
if (updates.empty()) return;
163161
ShadowTreeUpdateManager::applyUpdates(*rt, updates);
164162
obs->set(ShadowTreeUpdateManager::UpdatesMap{});
165163
});
166-
holder->effect->run();
167-
runtime_effects_.emplace(rt, std::move(holder));
164+
*weakBox = effect;
165+
effect->run();
166+
runtime_effects_.emplace(rt, std::move(effect));
168167
}
169168
}
170169

cpp/ShadowTreeUpdateManager.hpp

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,71 @@
66
#include <vector>
77

88
#include <folly/dynamic.h>
9-
#include <react/renderer/core/ReactPrimitives.h> // facebook::react::Tag
10-
#include <jsi/jsi.h>
9+
#include <react/renderer/core/ReactPrimitives.h>
1110

12-
#include "Observable.hpp"
13-
#include "Effect.hpp"
11+
namespace facebook::jsi {
12+
class Runtime;
1413

15-
// Forward declarations for Nitro Any types
16-
namespace margelo {
17-
namespace nitro {
18-
class AnyMap;
19-
}
14+
class Function;
2015
}
2116

22-
// Provide a convenient alias matching React Native's JSI namespace
23-
namespace jsi = facebook::jsi;
17+
namespace reactnativecss {
18+
template<typename T>
19+
class Observable;
20+
21+
class Effect;
22+
}
2423

25-
namespace margelo {
26-
namespace nitro {
27-
namespace cssnitro {
24+
namespace margelo::nitro { class AnyMap; }
25+
26+
namespace jsi = facebook::jsi;
2827

29-
struct VariantConverter; // forward decl for friend
28+
namespace margelo::nitro::cssnitro {
3029

31-
class ShadowTreeUpdateManager final {
32-
public:
33-
using UpdatesMap = std::unordered_map<facebook::react::Tag, folly::dynamic>;
30+
struct VariantConverter;
3431

35-
ShadowTreeUpdateManager();
32+
class ShadowTreeUpdateManager final {
33+
public:
34+
using UpdatesMap = std::unordered_map<facebook::react::Tag, folly::dynamic>;
3635

37-
void linkComponent(jsi::Runtime &runtime,
38-
const std::string &componentId,
39-
facebook::react::Tag tag);
36+
ShadowTreeUpdateManager();
4037

41-
void unlinkComponent(const std::string &componentId);
38+
void linkComponent(jsi::Runtime &runtime,
39+
const std::string &componentId,
40+
facebook::react::Tag tag);
4241

43-
// Accept the style entries from Styled.next.style and convert internally
44-
void addUpdates(const std::string &componentId,
45-
const std::vector<std::shared_ptr<::margelo::nitro::AnyMap>> &styleEntries);
42+
void unlinkComponent(const std::string &componentId);
4643

47-
void registerProcessColorFunction(jsi::Function &&fn);
44+
void addUpdates(const std::string &componentId,
45+
const std::vector<std::shared_ptr<::margelo::nitro::AnyMap>> &styleEntries);
4846

49-
private:
50-
friend struct VariantConverter;
51-
struct ComponentLink {
52-
facebook::react::Tag tag{0};
53-
jsi::Runtime *runtime{nullptr};
54-
};
47+
void registerProcessColorFunction(jsi::Function &&fn);
5548

56-
std::shared_ptr<jsi::Function> process_color_;
57-
// Cache for processed color strings -> int result
58-
std::unordered_map<std::string, int> process_color_cache_;
49+
private:
50+
friend struct VariantConverter;
51+
struct ComponentLink {
52+
facebook::react::Tag tag{0};
53+
jsi::Runtime *runtime{nullptr};
54+
};
5955

60-
// componentId -> {tag, runtime*}
61-
std::unordered_map<std::string, ComponentLink> component_links_;
56+
std::shared_ptr<jsi::Function> process_color_;
57+
std::unordered_map<std::string, int> process_color_cache_;
6258

63-
// Per-runtime updates observable/effect
64-
std::unordered_map<jsi::Runtime *, std::shared_ptr<reactnativecss::Observable<UpdatesMap>>> runtime_updates_;
59+
std::unordered_map<std::string, ComponentLink> component_links_;
6560

66-
struct RuntimeEffectHolder {
67-
std::shared_ptr<reactnativecss::Effect> effect;
68-
};
69-
std::unordered_map<jsi::Runtime *, std::shared_ptr<RuntimeEffectHolder>> runtime_effects_;
61+
std::unordered_map<jsi::Runtime *, std::shared_ptr<reactnativecss::Observable<UpdatesMap>>> runtime_updates_;
7062

71-
void ensureRuntimeEffect(jsi::Runtime &runtime);
63+
// Keep one effect per runtime
64+
std::unordered_map<jsi::Runtime *, std::shared_ptr<reactnativecss::Effect>> runtime_effects_;
7265

73-
static void applyUpdates(jsi::Runtime &runtime, const UpdatesMap &updates);
66+
void ensureRuntimeEffect(jsi::Runtime &runtime);
7467

75-
// String color processing (with caching)
76-
folly::dynamic
77-
processColorDynamic(jsi::Runtime &runtime, const folly::dynamic &value);
68+
static void applyUpdates(jsi::Runtime &runtime, const UpdatesMap &updates);
7869

79-
// styleEntryToUpdate was inlined into addUpdates
80-
};
70+
// String color processing (with caching)
71+
folly::dynamic
72+
processColorDynamic(jsi::Runtime &runtime, const folly::dynamic &value);
8173

82-
}
83-
}
74+
// styleEntryToUpdate was inlined into addUpdates
75+
};
8476
} // namespace margelo::nitro::cssnitro

0 commit comments

Comments
 (0)