Skip to content

Commit 695a37f

Browse files
committed
refactor: remove HybridStyleRegistry:Impl
1 parent 321d204 commit 695a37f

2 files changed

Lines changed: 56 additions & 177 deletions

File tree

cpp/HybridStyleRegistry.cpp

Lines changed: 49 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -24,158 +24,19 @@ namespace margelo::nitro::cssnitro {
2424

2525
using AnyMap = ::margelo::nitro::AnyMap;
2626

27-
struct HybridStyleRegistry::Impl {
28-
Impl();
29-
30-
void
31-
setClassname(const std::string &className, const std::vector<HybridStyleRule> &styleRule);
32-
33-
void addStyleSheet(const HybridStyleSheet &stylesheet);
34-
35-
Declarations
36-
getDeclarations(const std::string &componentId, const std::string &classNames,
37-
const std::string &variableScope, const std::string &containerScope);
38-
39-
Styled
40-
registerComponent(const std::string &componentId, const std::function<void()> &rerender,
41-
const std::string &classNames, const std::string &variableScope,
42-
const std::string &containerScope);
43-
44-
void deregisterComponent(const std::string &componentId);
45-
46-
void updateComponentState(const std::string &componentId, UpdateComponentStateFns type);
47-
48-
jsi::Value linkComponent(jsi::Runtime &runtime,
49-
const jsi::Value &thisValue,
50-
const jsi::Value *args, size_t count);
51-
52-
void unlinkComponent(const std::string &componentId);
53-
54-
jsi::Value registerExternalMethods(jsi::Runtime &runtime,
55-
const jsi::Value &thisValue,
56-
const jsi::Value *args, size_t count);
57-
58-
void setWindowDimensions(double width, double height, double scale, double fontScale);
59-
60-
private:
61-
// Per-component link info and update source
62-
struct ComponentLink {
63-
facebook::react::Tag tag{0};
64-
jsi::Runtime *runtime{nullptr};
65-
std::shared_ptr<reactnativecss::Observable<folly::dynamic>> updates;
66-
};
67-
68-
std::unordered_map<std::string, ComponentLink> component_linking_;
69-
using UpdatesMap = std::unordered_map<facebook::react::Tag, folly::dynamic>;
70-
std::unordered_map<jsi::Runtime *, std::shared_ptr<reactnativecss::Observable<UpdatesMap>>> runtime_updates_;
71-
std::unordered_map<jsi::Runtime *, std::shared_ptr<reactnativecss::Computed<bool>>> runtime_effects_;
72-
73-
public:
74-
static std::once_flag flag_;
75-
static std::shared_ptr<Impl> inst_;
76-
77-
static std::shared_ptr<Impl> get();
78-
79-
private:
80-
std::unique_ptr<ShadowTreeUpdateManager> shadowUpdates_;
81-
std::unordered_map<std::string, std::shared_ptr<reactnativecss::Computed<Styled>>> computedMap_;
82-
std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<std::vector<HybridStyleRule>>>> styleRuleMap_;
83-
};
84-
85-
86-
// Storage for static members
87-
std::once_flag HybridStyleRegistry::Impl::flag_;
88-
std::shared_ptr<HybridStyleRegistry::Impl> HybridStyleRegistry::Impl::inst_;
89-
90-
std::shared_ptr<HybridStyleRegistry::Impl> HybridStyleRegistry::Impl::get() {
91-
std::call_once(flag_, [] { inst_ = std::make_shared<Impl>(); });
92-
return inst_;
93-
}
27+
// Initialize static members
28+
std::unique_ptr<ShadowTreeUpdateManager> HybridStyleRegistry::shadowUpdates_ =
29+
std::make_unique<ShadowTreeUpdateManager>();
30+
std::unordered_map<std::string, std::shared_ptr<reactnativecss::Computed<Styled>>> HybridStyleRegistry::computedMap_;
31+
std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<std::vector<HybridStyleRule>>>> HybridStyleRegistry::styleRuleMap_;
9432

9533
// Constructor, Destructor, and Method Implementations
96-
HybridStyleRegistry::HybridStyleRegistry() : HybridObject("HybridStyleRegistry"),
97-
impl_(Impl::get()) {}
34+
HybridStyleRegistry::HybridStyleRegistry() : HybridObject("HybridStyleRegistry") {}
9835

9936
HybridStyleRegistry::~HybridStyleRegistry() = default;
10037

10138
void HybridStyleRegistry::setClassname(const std::string &className,
102-
const std::vector<HybridStyleRule> &styleRule) {
103-
impl_->setClassname(className, styleRule);
104-
}
105-
106-
void HybridStyleRegistry::addStyleSheet(const HybridStyleSheet &stylesheet) {
107-
impl_->addStyleSheet(stylesheet);
108-
}
109-
110-
Declarations HybridStyleRegistry::getDeclarations(const std::string &componentId,
111-
const std::string &classNames,
112-
const std::string &variableScope,
113-
const std::string &containerScope) {
114-
return impl_->getDeclarations(componentId, classNames, variableScope, containerScope);
115-
}
116-
117-
Styled HybridStyleRegistry::registerComponent(const std::string &componentId,
118-
const std::function<void()> &rerender,
119-
const std::string &classNames,
120-
const std::string &variableScope,
121-
const std::string &containerScope) {
122-
return impl_->registerComponent(componentId, rerender, classNames, variableScope,
123-
containerScope);
124-
}
125-
126-
void HybridStyleRegistry::deregisterComponent(const std::string &componentId) {
127-
impl_->deregisterComponent(componentId);
128-
}
129-
130-
void HybridStyleRegistry::updateComponentState(const std::string &componentId,
131-
UpdateComponentStateFns type) {
132-
impl_->updateComponentState(componentId, type);
133-
}
134-
135-
void HybridStyleRegistry::unlinkComponent(const std::string &componentId) {
136-
impl_->unlinkComponent(componentId);
137-
}
138-
139-
void HybridStyleRegistry::setWindowDimensions(double width, double height, double scale,
140-
double fontScale) {
141-
impl_->setWindowDimensions(width, height, scale, fontScale);
142-
}
143-
144-
jsi::Value
145-
HybridStyleRegistry::linkComponent(jsi::Runtime &runtime, const jsi::Value &thisValue,
146-
const jsi::Value *args, size_t count) {
147-
return impl_->linkComponent(runtime, thisValue, args, count);
148-
}
149-
150-
jsi::Value
151-
HybridStyleRegistry::registerExternalMethods(jsi::Runtime &runtime, const jsi::Value &thisValue,
152-
const jsi::Value *args, size_t count) {
153-
return impl_->registerExternalMethods(runtime, thisValue, args, count);
154-
}
155-
156-
void HybridStyleRegistry::loadHybridMethods() {
157-
HybridStyleRegistrySpec::loadHybridMethods();
158-
registerHybrids(this, [](Prototype &prototype) {
159-
prototype.registerRawHybridMethod(
160-
"linkComponent",
161-
2,
162-
&HybridStyleRegistry::linkComponent);
163-
164-
prototype.registerRawHybridMethod(
165-
"registerExternalMethods",
166-
1,
167-
&HybridStyleRegistry::registerExternalMethods);
168-
});
169-
}
170-
171-
// PIMPL Method Implementations
172-
173-
HybridStyleRegistry::Impl::Impl() {
174-
shadowUpdates_ = std::make_unique<ShadowTreeUpdateManager>();
175-
}
176-
177-
void HybridStyleRegistry::Impl::setClassname(const std::string &className,
178-
const std::vector<HybridStyleRule> &styleRules) {
39+
const std::vector<HybridStyleRule> &styleRules) {
17940
// Reverse the style rules, this way later on we can bail early if values are already set
18041
auto reversedRules = styleRules;
18142
std::reverse(reversedRules.begin(), reversedRules.end());
@@ -190,7 +51,7 @@ namespace margelo::nitro::cssnitro {
19051
}
19152
}
19253

193-
void HybridStyleRegistry::Impl::addStyleSheet(const HybridStyleSheet &stylesheet) {
54+
void HybridStyleRegistry::addStyleSheet(const HybridStyleSheet &stylesheet) {
19455
// Create an Effect batch to process all style updates together
19556
reactnativecss::Effect::batch([this, &stylesheet]() {
19657
// If the key "s" exists, loop over every entry
@@ -209,10 +70,10 @@ namespace margelo::nitro::cssnitro {
20970
});
21071
}
21172

212-
Declarations HybridStyleRegistry::Impl::getDeclarations(const std::string &componentId,
213-
const std::string &classNames,
214-
const std::string &variableScope,
215-
const std::string &containerScope) {
73+
Declarations HybridStyleRegistry::getDeclarations(const std::string &componentId,
74+
const std::string &classNames,
75+
const std::string &variableScope,
76+
const std::string &containerScope) {
21677
Declarations declarations;
21778
declarations.classNames = classNames;
21879
declarations.variableScope = variableScope;
@@ -250,11 +111,11 @@ namespace margelo::nitro::cssnitro {
250111
}
251112

252113
Styled
253-
HybridStyleRegistry::Impl::registerComponent(const std::string &componentId,
254-
const std::function<void()> &rerender,
255-
const std::string &classNames,
256-
const std::string &variableScope,
257-
const std::string &containerScope) {
114+
HybridStyleRegistry::registerComponent(const std::string &componentId,
115+
const std::function<void()> &rerender,
116+
const std::string &classNames,
117+
const std::string &variableScope,
118+
const std::string &containerScope) {
258119
if (auto existing = computedMap_.find(componentId); existing != computedMap_.end()) {
259120
if (existing->second) {
260121
existing->second->dispose();
@@ -274,7 +135,7 @@ namespace margelo::nitro::cssnitro {
274135
return computed->get();
275136
}
276137

277-
void HybridStyleRegistry::Impl::deregisterComponent(const std::string &componentId) {
138+
void HybridStyleRegistry::deregisterComponent(const std::string &componentId) {
278139
auto it = computedMap_.find(componentId);
279140
if (it != computedMap_.end()) {
280141
if (it->second) {
@@ -284,15 +145,23 @@ namespace margelo::nitro::cssnitro {
284145
}
285146
}
286147

287-
void HybridStyleRegistry::Impl::updateComponentState(const std::string &componentId,
288-
UpdateComponentStateFns type) {
148+
void HybridStyleRegistry::updateComponentState(const std::string &componentId,
149+
UpdateComponentStateFns type) {
289150
// TODO: Implement this
290151
}
291152

153+
void HybridStyleRegistry::unlinkComponent(const std::string &componentId) {
154+
shadowUpdates_->unlinkComponent(componentId);
155+
}
156+
157+
void HybridStyleRegistry::setWindowDimensions(double width, double height, double scale,
158+
double fontScale) {
159+
reactnativecss::env::setWindowDimensions(width, height, scale, fontScale);
160+
}
292161

293-
jsi::Value HybridStyleRegistry::Impl::linkComponent(
294-
jsi::Runtime &runtime, const jsi::Value &thisValue, const jsi::Value *args,
295-
size_t count) {
162+
jsi::Value
163+
HybridStyleRegistry::linkComponent(jsi::Runtime &runtime, const jsi::Value &thisValue,
164+
const jsi::Value *args, size_t count) {
296165
(void) thisValue;
297166
if (count < 2) {
298167
return jsi::Value::undefined();
@@ -309,18 +178,9 @@ namespace margelo::nitro::cssnitro {
309178
return jsi::Value::undefined();
310179
}
311180

312-
void HybridStyleRegistry::Impl::unlinkComponent(const std::string &componentId) {
313-
shadowUpdates_->unlinkComponent(componentId);
314-
}
315-
316-
void HybridStyleRegistry::Impl::setWindowDimensions(double width, double height, double scale,
317-
double fontScale) {
318-
reactnativecss::env::setWindowDimensions(width, height, scale, fontScale);
319-
}
320-
321-
jsi::Value HybridStyleRegistry::Impl::registerExternalMethods(
322-
jsi::Runtime &runtime, const jsi::Value &thisValue, const jsi::Value *args,
323-
size_t count) {
181+
jsi::Value
182+
HybridStyleRegistry::registerExternalMethods(jsi::Runtime &runtime, const jsi::Value &thisValue,
183+
const jsi::Value *args, size_t count) {
324184
(void) thisValue;
325185

326186
if (!args[0].isObject()) {
@@ -336,6 +196,21 @@ namespace margelo::nitro::cssnitro {
336196
return jsi::Value::undefined();
337197
}
338198

199+
void HybridStyleRegistry::loadHybridMethods() {
200+
HybridStyleRegistrySpec::loadHybridMethods();
201+
registerHybrids(this, [](Prototype &prototype) {
202+
prototype.registerRawHybridMethod(
203+
"linkComponent",
204+
2,
205+
&HybridStyleRegistry::linkComponent);
206+
207+
prototype.registerRawHybridMethod(
208+
"registerExternalMethods",
209+
1,
210+
&HybridStyleRegistry::registerExternalMethods);
211+
});
212+
}
213+
339214
void HybridStyleRegistry::updateComponentInlineStyleKeys(
340215
const std::string &componentId,
341216
const std::vector<std::string> &inlineStyleKeys) {

cpp/HybridStyleRegistry.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ namespace reactnativecss {
2424

2525
namespace margelo::nitro::cssnitro {
2626

27+
class ShadowTreeUpdateManager;
28+
2729
class HybridStyleRegistry : public HybridStyleRegistrySpec {
2830
public:
2931
HybridStyleRegistry();
@@ -67,16 +69,18 @@ namespace margelo::nitro::cssnitro {
6769
void loadHybridMethods() override;
6870

6971
private:
70-
struct Impl;
71-
std::shared_ptr<Impl> impl_;
72-
7372
jsi::Value linkComponent(jsi::Runtime &runtime,
7473
const jsi::Value &thisValue,
7574
const jsi::Value *args, size_t count);
7675

7776
jsi::Value registerExternalMethods(jsi::Runtime &runtime,
7877
const jsi::Value &thisValue,
7978
const jsi::Value *args, size_t count);
79+
80+
// Static shared state
81+
static std::unique_ptr<ShadowTreeUpdateManager> shadowUpdates_;
82+
static std::unordered_map<std::string, std::shared_ptr<reactnativecss::Computed<Styled>>> computedMap_;
83+
static std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<std::vector<HybridStyleRule>>>> styleRuleMap_;
8084
};
8185

8286
} // namespace margelo::nitro::cssnitro

0 commit comments

Comments
 (0)