react-native-unistyles 3.0.0-alpha.5 → 3.0.0-alpha.7
Sign up to get free protection for your applications and to get access to all the features.
- package/cxx/common/Helpers.h +24 -0
- package/cxx/core/HostStyle.cpp +4 -5
- package/cxx/core/HostStyle.h +4 -0
- package/cxx/core/StyleSheet.h +0 -1
- package/cxx/core/StyleSheetRegistry.cpp +9 -17
- package/cxx/core/StyleSheetRegistry.h +3 -4
- package/cxx/core/UnistyleData.h +22 -0
- package/cxx/core/UnistylesCommitHook.cpp +4 -1
- package/cxx/core/UnistylesRegistry.cpp +33 -49
- package/cxx/core/UnistylesRegistry.h +8 -8
- package/cxx/hybridObjects/HybridShadowRegistry.cpp +4 -3
- package/cxx/hybridObjects/HybridStyleSheet.cpp +8 -11
- package/cxx/parser/Parser.cpp +42 -68
- package/cxx/parser/Parser.h +8 -13
- package/lib/commonjs/specs/NavigtionBar/index.js +1 -1
- package/lib/commonjs/specs/NavigtionBar/index.js.map +1 -1
- package/lib/commonjs/specs/ShadowRegistry/index.js +2 -2
- package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
- package/lib/commonjs/specs/StatusBar/index.js +1 -1
- package/lib/commonjs/specs/StatusBar/index.js.map +1 -1
- package/lib/commonjs/specs/StyleSheet/index.js.map +1 -1
- package/lib/commonjs/specs/UnistylesRuntime/index.js +1 -1
- package/lib/commonjs/specs/UnistylesRuntime/index.js.map +1 -1
- package/lib/module/specs/NavigtionBar/index.js +1 -1
- package/lib/module/specs/NavigtionBar/index.js.map +1 -1
- package/lib/module/specs/ShadowRegistry/index.js +2 -2
- package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
- package/lib/module/specs/StatusBar/index.js +1 -1
- package/lib/module/specs/StatusBar/index.js.map +1 -1
- package/lib/module/specs/StyleSheet/index.js.map +1 -1
- package/lib/module/specs/UnistylesRuntime/index.js +1 -1
- package/lib/module/specs/UnistylesRuntime/index.js.map +1 -1
- package/lib/typescript/example/App.d.ts.map +1 -1
- package/lib/typescript/example/Typography.d.ts +11 -0
- package/lib/typescript/example/Typography.d.ts.map +1 -0
- package/lib/typescript/src/specs/ShadowRegistry/index.d.ts +2 -2
- package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
- package/lib/typescript/src/specs/StyleSheet/index.d.ts +2 -1
- package/lib/typescript/src/specs/StyleSheet/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/plugin/__tests__/dependencies.spec.js +181 -103
- package/plugin/__tests__/ref.spec.js +222 -158
- package/plugin/__tests__/stylesheet.spec.js +148 -55
- package/plugin/index.js +22 -11
- package/plugin/ref.js +35 -7
- package/plugin/style.js +5 -22
- package/plugin/stylesheet.js +44 -1
- package/plugin/variants.js +33 -0
- package/src/specs/NavigtionBar/index.ts +1 -1
- package/src/specs/ShadowRegistry/index.ts +4 -4
- package/src/specs/StatusBar/index.ts +1 -1
- package/src/specs/StyleSheet/index.ts +3 -1
- package/src/specs/UnistylesRuntime/index.ts +1 -1
package/cxx/common/Helpers.h
CHANGED
@@ -7,6 +7,8 @@ using namespace facebook;
|
|
7
7
|
|
8
8
|
namespace margelo::nitro::unistyles::helpers {
|
9
9
|
|
10
|
+
using Variants = std::vector<std::pair<std::string, std::string>>;
|
11
|
+
|
10
12
|
inline void assertThat(jsi::Runtime& rt, bool condition, const std::string& message) {
|
11
13
|
if (!condition) {
|
12
14
|
throw jsi::JSError(rt, message);
|
@@ -90,4 +92,26 @@ inline bool isPlatformColor(jsi::Runtime& rt, jsi::Object& maybePlatformColor) {
|
|
90
92
|
return maybePlatformColor.hasProperty(rt, "resource_paths") && maybePlatformColor.getProperty(rt, "resource_paths").isObject();
|
91
93
|
}
|
92
94
|
|
95
|
+
inline Variants variantsToPairs(jsi::Runtime& rt, jsi::Object&& variants) {
|
96
|
+
Variants pairs{};
|
97
|
+
|
98
|
+
helpers::enumerateJSIObject(rt, variants, [&](const std::string& variantName, jsi::Value& variantValue){
|
99
|
+
if (variantValue.isUndefined() || variantValue.isNull()) {
|
100
|
+
return;
|
101
|
+
}
|
102
|
+
|
103
|
+
if (variantValue.isBool()) {
|
104
|
+
pairs.emplace_back(std::make_pair(variantName, variantValue.asBool() ? "true" : "false"));
|
105
|
+
|
106
|
+
return;
|
107
|
+
}
|
108
|
+
|
109
|
+
if (variantValue.isString()) {
|
110
|
+
pairs.emplace_back(std::make_pair(variantName, variantValue.asString(rt).utf8(rt)));
|
111
|
+
}
|
112
|
+
});
|
113
|
+
|
114
|
+
return pairs;
|
115
|
+
}
|
116
|
+
|
93
117
|
}
|
package/cxx/core/HostStyle.cpp
CHANGED
@@ -42,15 +42,14 @@ jsi::Function HostStyle::createAddVariantsProxyFunction(jsi::Runtime& rt) {
|
|
42
42
|
helpers::assertThat(rt, arguments[0].isObject(), "Unistyles: useVariants expected to be called with object.");
|
43
43
|
|
44
44
|
auto parser = parser::Parser(this->_unistylesRuntime);
|
45
|
-
auto pairs =
|
45
|
+
auto pairs = helpers::variantsToPairs(rt, arguments[0].asObject(rt));
|
46
46
|
|
47
|
-
if (pairs == this->
|
47
|
+
if (pairs == this->_variants) {
|
48
48
|
return jsi::Value::undefined();
|
49
49
|
}
|
50
50
|
|
51
|
-
this->
|
52
|
-
|
53
|
-
parser.rebuildUnistylesWithVariants(rt, this->_styleSheet);
|
51
|
+
this->_variants = pairs;
|
52
|
+
parser.rebuildUnistylesWithVariants(rt, this->_styleSheet, this->_variants);
|
54
53
|
|
55
54
|
return jsi::Value::undefined();
|
56
55
|
});
|
package/cxx/core/HostStyle.h
CHANGED
@@ -8,6 +8,8 @@
|
|
8
8
|
|
9
9
|
namespace margelo::nitro::unistyles::core {
|
10
10
|
|
11
|
+
using Variants = std::vector<std::pair<std::string, std::string>>;
|
12
|
+
|
11
13
|
struct JSI_EXPORT HostStyle : public jsi::HostObject {
|
12
14
|
HostStyle(std::shared_ptr<StyleSheet> styleSheet, std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime)
|
13
15
|
: _styleSheet{styleSheet}, _unistylesRuntime{unistylesRuntime} {};
|
@@ -20,6 +22,8 @@ struct JSI_EXPORT HostStyle : public jsi::HostObject {
|
|
20
22
|
private:
|
21
23
|
std::shared_ptr<StyleSheet> _styleSheet;
|
22
24
|
std::shared_ptr<HybridUnistylesRuntime> _unistylesRuntime;
|
25
|
+
std::vector<std::pair<std::string, std::string>> _variants{};
|
26
|
+
|
23
27
|
};
|
24
28
|
|
25
29
|
}
|
package/cxx/core/StyleSheet.h
CHANGED
@@ -4,17 +4,15 @@
|
|
4
4
|
using namespace margelo::nitro::unistyles::core;
|
5
5
|
using namespace facebook;
|
6
6
|
|
7
|
-
std::shared_ptr<StyleSheet> StyleSheetRegistry::addStyleSheetFromValue(jsi::Runtime& rt, jsi::Object rawStyleSheet) {
|
8
|
-
static unsigned int tag = 0;
|
9
|
-
|
7
|
+
std::shared_ptr<StyleSheet> StyleSheetRegistry::addStyleSheetFromValue(jsi::Runtime& rt, jsi::Object rawStyleSheet, int unid) {
|
10
8
|
if (rawStyleSheet.isFunction(rt)) {
|
11
|
-
return this->addFromFunction(rt,
|
9
|
+
return this->addFromFunction(rt, unid, rawStyleSheet.asFunction(rt));
|
12
10
|
}
|
13
11
|
|
14
|
-
return this->addFromObject(rt,
|
12
|
+
return this->addFromObject(rt, unid, std::move(rawStyleSheet));
|
15
13
|
}
|
16
14
|
|
17
|
-
std::shared_ptr<StyleSheet> StyleSheetRegistry::addFromFunction(jsi::Runtime& rt,
|
15
|
+
std::shared_ptr<StyleSheet> StyleSheetRegistry::addFromFunction(jsi::Runtime& rt, int unid, jsi::Function styleSheetFn) {
|
18
16
|
auto numberOfArgs = styleSheetFn.getProperty(rt, "length").getNumber();
|
19
17
|
|
20
18
|
helpers::assertThat(rt, numberOfArgs <= 2, "StyleSheet.create expected up to 2 arguments.");
|
@@ -25,26 +23,20 @@ std::shared_ptr<StyleSheet> StyleSheetRegistry::addFromFunction(jsi::Runtime& rt
|
|
25
23
|
if (numberOfArgs == 0) {
|
26
24
|
auto staticStyleSheet = styleSheetFn.call(rt).asObject(rt);
|
27
25
|
|
28
|
-
return registry.addStyleSheet(
|
26
|
+
return registry.addStyleSheet(rt, unid, core::StyleSheetType::Static, std::move(staticStyleSheet));
|
29
27
|
}
|
30
28
|
|
31
29
|
// stylesheet depends only on theme
|
32
30
|
if (numberOfArgs == 1) {
|
33
|
-
return registry.addStyleSheet(
|
31
|
+
return registry.addStyleSheet(rt, unid, core::StyleSheetType::Themable, std::move(styleSheetFn));
|
34
32
|
}
|
35
33
|
|
36
34
|
// stylesheet depends on theme and mini runtime
|
37
|
-
return registry.addStyleSheet(
|
38
|
-
}
|
39
|
-
|
40
|
-
std::shared_ptr<StyleSheet> StyleSheetRegistry::addFromObject(jsi::Runtime& rt, unsigned int tag, jsi::Object rawStyleSheet) {
|
41
|
-
auto& registry = UnistylesRegistry::get();
|
42
|
-
|
43
|
-
return registry.addStyleSheet(tag, core::StyleSheetType::Static, std::move(rawStyleSheet));
|
35
|
+
return registry.addStyleSheet(rt, unid, core::StyleSheetType::ThemableWithMiniRuntime, std::move(styleSheetFn));
|
44
36
|
}
|
45
37
|
|
46
|
-
|
38
|
+
std::shared_ptr<StyleSheet> StyleSheetRegistry::addFromObject(jsi::Runtime& rt, int tag, jsi::Object rawStyleSheet) {
|
47
39
|
auto& registry = UnistylesRegistry::get();
|
48
40
|
|
49
|
-
registry.
|
41
|
+
return registry.addStyleSheet(rt, tag, core::StyleSheetType::Static, std::move(rawStyleSheet));
|
50
42
|
}
|
@@ -16,12 +16,11 @@ struct StyleSheetRegistry {
|
|
16
16
|
StyleSheetRegistry(const StyleSheetRegistry&) = delete;
|
17
17
|
StyleSheetRegistry(StyleSheetRegistry&&) = delete;
|
18
18
|
|
19
|
-
virtual std::shared_ptr<StyleSheet> addStyleSheetFromValue(jsi::Runtime& rt, jsi::Object rawStyleSheet);
|
20
|
-
virtual void removeStyleSheetByTag(unsigned int tag);
|
19
|
+
virtual std::shared_ptr<StyleSheet> addStyleSheetFromValue(jsi::Runtime& rt, jsi::Object rawStyleSheet, int unid);
|
21
20
|
|
22
21
|
private:
|
23
|
-
virtual std::shared_ptr<StyleSheet> addFromFunction(jsi::Runtime& rt,
|
24
|
-
virtual std::shared_ptr<StyleSheet> addFromObject(jsi::Runtime& rt,
|
22
|
+
virtual std::shared_ptr<StyleSheet> addFromFunction(jsi::Runtime& rt, int unid, jsi::Function styleSheetFn);
|
23
|
+
virtual std::shared_ptr<StyleSheet> addFromObject(jsi::Runtime& rt, int unid, jsi::Object rawStyleSheet);
|
25
24
|
};
|
26
25
|
|
27
26
|
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
#pragma once
|
2
|
+
|
3
|
+
#include <jsi/jsi.h>
|
4
|
+
#include "Unistyle.h"
|
5
|
+
|
6
|
+
namespace margelo::nitro::unistyles::core {
|
7
|
+
|
8
|
+
using Variants = std::vector<std::pair<std::string, std::string>>;
|
9
|
+
|
10
|
+
struct UnistyleData {
|
11
|
+
UnistyleData(Unistyle::Shared unistyle, const Variants& variants)
|
12
|
+
: unistyle{unistyle}, variants(std::move(variants)) {}
|
13
|
+
|
14
|
+
UnistyleData(const UnistyleData&) = delete;
|
15
|
+
UnistyleData(UnistyleData&& other): unistyle{other.unistyle}, variants(std::move(other.variants)) {}
|
16
|
+
|
17
|
+
core::Unistyle::Shared unistyle;
|
18
|
+
core::Variants variants;
|
19
|
+
std::optional<jsi::Object> parsedStyle = std::nullopt;
|
20
|
+
};
|
21
|
+
|
22
|
+
}
|
@@ -43,8 +43,11 @@ RootShadowNode::Unshared core::UnistylesCommitHook::shadowTreeWillCommit(
|
|
43
43
|
|
44
44
|
shadow::ShadowLeafUpdates core::UnistylesCommitHook::getUnistylesUpdates() {
|
45
45
|
auto& registry = core::UnistylesRegistry::get();
|
46
|
+
auto& rt = this->_unistylesRuntime->getRuntime();
|
46
47
|
auto parser = parser::Parser(this->_unistylesRuntime);
|
47
|
-
auto dependencyMap = registry.buildDependencyMap();
|
48
|
+
auto dependencyMap = registry.buildDependencyMap(rt);
|
49
|
+
|
50
|
+
parser.rebuildUnistylesInDependencyMap(rt, dependencyMap);
|
48
51
|
|
49
52
|
return parser.dependencyMapToShadowLeafUpdates(dependencyMap);
|
50
53
|
}
|
@@ -5,11 +5,6 @@ using namespace margelo::nitro::unistyles;
|
|
5
5
|
using namespace facebook;
|
6
6
|
using namespace facebook::react;
|
7
7
|
|
8
|
-
using DependencyMap = std::unordered_map<
|
9
|
-
std::shared_ptr<core::StyleSheet>,
|
10
|
-
std::unordered_map<const ShadowNodeFamily*, std::vector<core::Unistyle::Shared>>
|
11
|
-
>;
|
12
|
-
|
13
8
|
void core::UnistylesRegistry::registerTheme(jsi::Runtime& rt, std::string name, jsi::Object&& theme) {
|
14
9
|
auto& state = this->getState(rt);
|
15
10
|
|
@@ -62,64 +57,54 @@ void core::UnistylesRegistry::createState(jsi::Runtime& rt) {
|
|
62
57
|
void core::UnistylesRegistry::updateTheme(jsi::Runtime& rt, std::string& themeName, jsi::Function&& callback) {
|
63
58
|
auto& state = this->getState(rt);
|
64
59
|
auto it = state._jsThemes.find(themeName);
|
65
|
-
|
60
|
+
|
66
61
|
helpers::assertThat(rt, it != state._jsThemes.end(), "Unistyles: You're trying to update theme '" + themeName + "' but it wasn't registered.");
|
67
|
-
|
62
|
+
|
68
63
|
auto currentThemeValue = it->second.lock(rt);
|
69
|
-
|
64
|
+
|
70
65
|
helpers::assertThat(rt, currentThemeValue.isObject(), "Unistyles: Unable to update your theme from C++. It was already garbage collected.");
|
71
|
-
|
66
|
+
|
72
67
|
auto result = callback.call(rt, currentThemeValue.asObject(rt));
|
73
|
-
|
68
|
+
|
74
69
|
helpers::assertThat(rt, result.isObject(), "Unistyles: Returned theme is not an object. Please check your updateTheme function.");
|
75
70
|
|
76
71
|
it->second = jsi::WeakObject(rt, result.asObject(rt));
|
77
72
|
}
|
78
73
|
|
79
|
-
void core::UnistylesRegistry::linkShadowNodeWithUnistyle(
|
74
|
+
void core::UnistylesRegistry::linkShadowNodeWithUnistyle(
|
75
|
+
const ShadowNodeFamily* shadowNodeFamily,
|
76
|
+
const core::Unistyle::Shared unistyle,
|
77
|
+
Variants& variants
|
78
|
+
) {
|
80
79
|
if (!this->_shadowRegistry.contains(shadowNodeFamily)) {
|
81
80
|
this->_shadowRegistry[shadowNodeFamily] = {};
|
82
81
|
}
|
83
|
-
|
84
|
-
this->_shadowRegistry[shadowNodeFamily].emplace_back(unistyle);
|
82
|
+
|
83
|
+
this->_shadowRegistry[shadowNodeFamily].emplace_back(std::make_pair(unistyle, std::move(variants)));
|
85
84
|
}
|
86
85
|
|
87
86
|
void core::UnistylesRegistry::unlinkShadowNodeWithUnistyle(const ShadowNodeFamily* shadowNodeFamily, const core::Unistyle::Shared unistyle) {
|
88
87
|
auto& unistylesVec = this->_shadowRegistry[shadowNodeFamily];
|
89
|
-
auto it = std::
|
90
|
-
|
88
|
+
auto it = std::find_if(unistylesVec.begin(), unistylesVec.end(), [unistyle](std::pair<core::Unistyle::Shared, Variants> pair){
|
89
|
+
return pair.first == unistyle;
|
90
|
+
});
|
91
|
+
|
91
92
|
if (it != unistylesVec.end()) {
|
92
93
|
this->_shadowRegistry[shadowNodeFamily].erase(it);
|
93
94
|
}
|
94
95
|
}
|
95
96
|
|
96
|
-
std::shared_ptr<core::StyleSheet> core::UnistylesRegistry::addStyleSheet(int
|
97
|
-
this->_styleSheetRegistry
|
97
|
+
std::shared_ptr<core::StyleSheet> core::UnistylesRegistry::addStyleSheet(jsi::Runtime& rt, int unid, core::StyleSheetType type, jsi::Object&& rawValue) {
|
98
|
+
this->_styleSheetRegistry[&rt][unid] = std::make_shared<core::StyleSheet>(unid, type, std::move(rawValue));
|
98
99
|
|
99
|
-
return this->_styleSheetRegistry
|
100
|
+
return this->_styleSheetRegistry[&rt][unid];
|
100
101
|
}
|
101
102
|
|
102
|
-
|
103
|
-
auto it = std::find_if(
|
104
|
-
this->_styleSheetRegistry.begin(),
|
105
|
-
this->_styleSheetRegistry.end(),
|
106
|
-
[tag](std::shared_ptr<StyleSheet> styleSheet){
|
107
|
-
return styleSheet->tag == tag;
|
108
|
-
}
|
109
|
-
);
|
110
|
-
|
111
|
-
if (it == this->_styleSheetRegistry.cend()) {
|
112
|
-
throw std::runtime_error("stylesheet with tag: " + std::to_string(tag) + " cannot be found.");
|
113
|
-
}
|
114
|
-
|
115
|
-
this->_styleSheetRegistry.erase(it);
|
116
|
-
}
|
117
|
-
|
118
|
-
DependencyMap core::UnistylesRegistry::buildDependencyMap(std::vector<UnistyleDependency>& deps) {
|
103
|
+
core::DependencyMap core::UnistylesRegistry::buildDependencyMap(jsi::Runtime& rt, std::vector<UnistyleDependency>& deps) {
|
119
104
|
DependencyMap dependencyMap;
|
120
105
|
std::set<UnistyleDependency> uniqueDependencies(deps.begin(), deps.end());
|
121
|
-
|
122
|
-
for (const auto& styleSheet : this->_styleSheetRegistry) {
|
106
|
+
|
107
|
+
for (const auto& [_, styleSheet] : this->_styleSheetRegistry[&rt]) {
|
123
108
|
for (const auto& [_, unistyle] : styleSheet->unistyles) {
|
124
109
|
// check if in the given stylesheet we have unistyle
|
125
110
|
// that depends on something affected
|
@@ -130,21 +115,21 @@ DependencyMap core::UnistylesRegistry::buildDependencyMap(std::vector<UnistyleDe
|
|
130
115
|
return std::find(uniqueDependencies.begin(), uniqueDependencies.end(), dep) != uniqueDependencies.end();
|
131
116
|
}
|
132
117
|
);
|
133
|
-
|
118
|
+
|
134
119
|
if (!hasAnyOfDependencies) {
|
135
120
|
continue;
|
136
121
|
}
|
137
|
-
|
122
|
+
|
138
123
|
// if so, we need to find shadow family too
|
139
124
|
for (const auto& pair : this->_shadowRegistry) {
|
140
125
|
const auto& [family, unistyles] = pair;
|
141
|
-
|
142
|
-
for (const auto& shadowUnistyle : unistyles) {
|
126
|
+
|
127
|
+
for (const auto& [shadowUnistyle, variants] : unistyles) {
|
143
128
|
if (unistyle != shadowUnistyle) {
|
144
129
|
continue;
|
145
130
|
}
|
146
131
|
|
147
|
-
dependencyMap[styleSheet][family].
|
132
|
+
dependencyMap[styleSheet][family].emplace_back(unistyle, variants);
|
148
133
|
}
|
149
134
|
}
|
150
135
|
}
|
@@ -153,21 +138,20 @@ DependencyMap core::UnistylesRegistry::buildDependencyMap(std::vector<UnistyleDe
|
|
153
138
|
return dependencyMap;
|
154
139
|
}
|
155
140
|
|
156
|
-
DependencyMap core::UnistylesRegistry::buildDependencyMap() {
|
141
|
+
core::DependencyMap core::UnistylesRegistry::buildDependencyMap(jsi::Runtime& rt) {
|
157
142
|
DependencyMap dependencyMap;
|
158
|
-
|
159
|
-
for (const auto& styleSheet : this->_styleSheetRegistry) {
|
143
|
+
|
144
|
+
for (const auto& [_, styleSheet] : this->_styleSheetRegistry[&rt]) {
|
160
145
|
for (const auto& [_, unistyle] : styleSheet->unistyles) {
|
161
146
|
for (const auto& pair : this->_shadowRegistry) {
|
162
147
|
const auto& [family, unistyles] = pair;
|
163
|
-
|
164
|
-
for (const auto& shadowUnistyle : unistyles) {
|
148
|
+
|
149
|
+
for (const auto& [shadowUnistyle, variants] : unistyles) {
|
165
150
|
if (unistyle != shadowUnistyle) {
|
166
151
|
continue;
|
167
152
|
}
|
168
153
|
|
169
|
-
|
170
|
-
dependencyMap[styleSheet][family].push_back(shadowUnistyle);
|
154
|
+
dependencyMap[styleSheet][family].emplace_back(unistyle, variants);
|
171
155
|
}
|
172
156
|
}
|
173
157
|
}
|
@@ -9,6 +9,7 @@
|
|
9
9
|
#include "StyleSheetRegistry.h"
|
10
10
|
#include "StyleSheet.h"
|
11
11
|
#include "Unistyle.h"
|
12
|
+
#include "UnistyleData.h"
|
12
13
|
|
13
14
|
namespace margelo::nitro::unistyles::core {
|
14
15
|
|
@@ -19,7 +20,7 @@ using namespace facebook::react;
|
|
19
20
|
|
20
21
|
using DependencyMap = std::unordered_map<
|
21
22
|
std::shared_ptr<core::StyleSheet>,
|
22
|
-
std::unordered_map<const ShadowNodeFamily*, std::vector<
|
23
|
+
std::unordered_map<const ShadowNodeFamily*, std::vector<UnistyleData>>
|
23
24
|
>;
|
24
25
|
|
25
26
|
struct UnistylesRegistry: public StyleSheetRegistry {
|
@@ -36,19 +37,18 @@ struct UnistylesRegistry: public StyleSheetRegistry {
|
|
36
37
|
|
37
38
|
UnistylesState& getState(jsi::Runtime& rt);
|
38
39
|
void createState(jsi::Runtime& rt);
|
39
|
-
void linkShadowNodeWithUnistyle(const ShadowNodeFamily*, const core::Unistyle::Shared);
|
40
|
+
void linkShadowNodeWithUnistyle(const ShadowNodeFamily*, const core::Unistyle::Shared, Variants& variants);
|
40
41
|
void unlinkShadowNodeWithUnistyle(const ShadowNodeFamily*, const core::Unistyle::Shared);
|
41
|
-
std::shared_ptr<core::StyleSheet> addStyleSheet(int tag, core::StyleSheetType type, jsi::Object&& rawValue);
|
42
|
-
|
43
|
-
DependencyMap buildDependencyMap(
|
44
|
-
DependencyMap buildDependencyMap();
|
42
|
+
std::shared_ptr<core::StyleSheet> addStyleSheet(jsi::Runtime& rt, int tag, core::StyleSheetType type, jsi::Object&& rawValue);
|
43
|
+
DependencyMap buildDependencyMap(jsi::Runtime& rt, std::vector<UnistyleDependency>& deps);
|
44
|
+
DependencyMap buildDependencyMap(jsi::Runtime& rt);
|
45
45
|
|
46
46
|
private:
|
47
47
|
UnistylesRegistry() = default;
|
48
48
|
|
49
49
|
std::unordered_map<jsi::Runtime*, UnistylesState> _states{};
|
50
|
-
std::
|
51
|
-
std::unordered_map<const ShadowNodeFamily*, std::vector<core::Unistyle::Shared
|
50
|
+
std::unordered_map<jsi::Runtime*, std::unordered_map<int, std::shared_ptr<core::StyleSheet>>> _styleSheetRegistry{};
|
51
|
+
std::unordered_map<const ShadowNodeFamily*, std::vector<std::pair<core::Unistyle::Shared, Variants>>> _shadowRegistry{};
|
52
52
|
};
|
53
53
|
|
54
54
|
UnistylesRegistry& UnistylesRegistry::get() {
|
@@ -4,20 +4,21 @@ using namespace margelo::nitro::unistyles;
|
|
4
4
|
using namespace facebook::react;
|
5
5
|
|
6
6
|
jsi::Value HybridShadowRegistry::link(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
|
7
|
-
helpers::assertThat(rt, count ==
|
7
|
+
helpers::assertThat(rt, count == 3, "Unistyles: Invalid babel transform 'ShadowRegistry link' expected 3 arguments.");
|
8
8
|
|
9
9
|
ShadowNode::Shared shadowNodeWrapper = shadowNodeFromValue(rt, args[0]);
|
10
10
|
core::Unistyle::Shared unistyleWrapper = core::unistyleFromValue(rt, args[1]);
|
11
|
+
core::Variants variants = helpers::variantsToPairs(rt, args[2].asObject(rt));
|
11
12
|
|
12
13
|
auto& registry = core::UnistylesRegistry::get();
|
13
14
|
|
14
|
-
registry.linkShadowNodeWithUnistyle(&shadowNodeWrapper->getFamily(), unistyleWrapper);
|
15
|
+
registry.linkShadowNodeWithUnistyle(&shadowNodeWrapper->getFamily(), unistyleWrapper, variants);
|
15
16
|
|
16
17
|
return jsi::Value::undefined();
|
17
18
|
}
|
18
19
|
|
19
20
|
jsi::Value HybridShadowRegistry::unlink(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
|
20
|
-
helpers::assertThat(rt, count == 2, "Unistyles: Invalid babel transform 'ShadowRegistry unlink' expected
|
21
|
+
helpers::assertThat(rt, count == 2, "Unistyles: Invalid babel transform 'ShadowRegistry unlink' expected 2 arguments.");
|
21
22
|
|
22
23
|
ShadowNode::Shared shadowNodeWrapper = shadowNodeFromValue(rt, args[0]);
|
23
24
|
core::Unistyle::Shared unistyleWrapper = core::unistyleFromValue(rt, args[1]);
|
@@ -13,20 +13,17 @@ double HybridStyleSheet::get___unid() {
|
|
13
13
|
return this->__unid;
|
14
14
|
}
|
15
15
|
|
16
|
-
jsi::Value HybridStyleSheet::create(jsi::Runtime
|
17
|
-
|
16
|
+
jsi::Value HybridStyleSheet::create(jsi::Runtime& rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) {
|
17
|
+
// second argument is hidden, so validation is perfectly fine
|
18
|
+
helpers::assertThat(rt, count == 2, "StyleSheet.create expected to be called with one argument.");
|
18
19
|
helpers::assertThat(rt, arguments[0].isObject(), "StyleSheet.create expected to be called with object or function.");
|
19
20
|
|
20
21
|
auto thisStyleSheet = thisVal.asObject(rt);
|
21
22
|
auto& registry = core::UnistylesRegistry::get();
|
22
|
-
|
23
|
-
// this might happen only when hot reloading
|
24
|
-
if (this->__unid != -1) {
|
25
|
-
registry.removeStyleSheet(this->__unid);
|
26
|
-
}
|
23
|
+
int unid = arguments[1].asNumber();
|
27
24
|
|
28
25
|
jsi::Object rawStyleSheet = arguments[0].asObject(rt);
|
29
|
-
auto registeredStyleSheet = registry.addStyleSheetFromValue(rt, std::move(rawStyleSheet));
|
26
|
+
auto registeredStyleSheet = registry.addStyleSheetFromValue(rt, std::move(rawStyleSheet), unid);
|
30
27
|
|
31
28
|
this->__unid = registeredStyleSheet->tag;
|
32
29
|
|
@@ -225,12 +222,12 @@ void HybridStyleSheet::onPlatformDependenciesChange(std::vector<UnistyleDependen
|
|
225
222
|
|
226
223
|
// check if color scheme changed and then if Unistyles state depend on it (adaptive themes)
|
227
224
|
auto colorSchemeIt = std::find(dependencies.begin(), dependencies.end(), UnistyleDependency::COLORSCHEME);
|
228
|
-
|
225
|
+
|
229
226
|
if (colorSchemeIt != dependencies.end()) {
|
230
227
|
this->_unistylesRuntime->includeDependenciesForColorSchemeChange(dependencies);
|
231
228
|
}
|
232
|
-
|
233
|
-
auto dependencyMap = registry.buildDependencyMap(dependencies);
|
229
|
+
|
230
|
+
auto dependencyMap = registry.buildDependencyMap(rt, dependencies);
|
234
231
|
|
235
232
|
if (dependencyMap.size() == 0) {
|
236
233
|
return;
|