react-native-unistyles 3.0.0-alpha.3 → 3.0.0-alpha.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -38,8 +38,8 @@ jsi::Function HostStyle::createAddVariantsProxyFunction(jsi::Runtime& rt) {
38
38
  auto useVariantsFnName = jsi::PropNameID::forUtf8(rt, helpers::ADD_VARIANTS_FN);
39
39
 
40
40
  return jsi::Function::createFromHostFunction(rt, useVariantsFnName, 1, [&](jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count){
41
- helpers::assertThat(rt, count == 1, "useVariants expected to be called with one argument.");
42
- helpers::assertThat(rt, arguments[0].isObject(), "useVariants expected to be called with object.");
41
+ helpers::assertThat(rt, count == 1, "Unistyles: useVariants expected to be called with one argument.");
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
45
  auto pairs = parser.variantsToPairs(rt, arguments[0].asObject(rt));
@@ -17,7 +17,7 @@ std::shared_ptr<StyleSheet> StyleSheetRegistry::addStyleSheetFromValue(jsi::Runt
17
17
  std::shared_ptr<StyleSheet> StyleSheetRegistry::addFromFunction(jsi::Runtime& rt, unsigned int tag, jsi::Function styleSheetFn) {
18
18
  auto numberOfArgs = styleSheetFn.getProperty(rt, "length").getNumber();
19
19
 
20
- helpers::assertThat(rt, numberOfArgs <= 2, "expected up to 2 arguments.");
20
+ helpers::assertThat(rt, numberOfArgs <= 2, "StyleSheet.create expected up to 2 arguments.");
21
21
 
22
22
  auto& registry = UnistylesRegistry::get();
23
23
 
@@ -43,3 +43,8 @@ std::shared_ptr<StyleSheet> StyleSheetRegistry::addFromObject(jsi::Runtime& rt,
43
43
  return registry.addStyleSheet(tag, core::StyleSheetType::Static, std::move(rawStyleSheet));
44
44
  }
45
45
 
46
+ void StyleSheetRegistry::removeStyleSheetByTag(unsigned int tag) {
47
+ auto& registry = UnistylesRegistry::get();
48
+
49
+ registry.removeStyleSheet(tag);
50
+ }
@@ -17,7 +17,8 @@ struct StyleSheetRegistry {
17
17
  StyleSheetRegistry(StyleSheetRegistry&&) = delete;
18
18
 
19
19
  virtual std::shared_ptr<StyleSheet> addStyleSheetFromValue(jsi::Runtime& rt, jsi::Object rawStyleSheet);
20
-
20
+ virtual void removeStyleSheetByTag(unsigned int tag);
21
+
21
22
  private:
22
23
  virtual std::shared_ptr<StyleSheet> addFromFunction(jsi::Runtime& rt, unsigned int tag, jsi::Function styleSheetFn);
23
24
  virtual std::shared_ptr<StyleSheet> addFromObject(jsi::Runtime& rt, unsigned int tag, jsi::Object rawStyleSheet);
@@ -63,15 +63,15 @@ void core::UnistylesRegistry::updateTheme(jsi::Runtime& rt, std::string& themeNa
63
63
  auto& state = this->getState(rt);
64
64
  auto it = state._jsThemes.find(themeName);
65
65
 
66
- helpers::assertThat(rt, it != state._jsThemes.end(), "you're trying to update theme '" + themeName + "' but it wasn't registered.");
66
+ helpers::assertThat(rt, it != state._jsThemes.end(), "Unistyles: You're trying to update theme '" + themeName + "' but it wasn't registered.");
67
67
 
68
68
  auto currentThemeValue = it->second.lock(rt);
69
69
 
70
- helpers::assertThat(rt, currentThemeValue.isObject(), "unable to update your theme from C++. It was already garbage collected.");
70
+ helpers::assertThat(rt, currentThemeValue.isObject(), "Unistyles: Unable to update your theme from C++. It was already garbage collected.");
71
71
 
72
72
  auto result = callback.call(rt, currentThemeValue.asObject(rt));
73
73
 
74
- helpers::assertThat(rt, result.isObject(), "returned theme is not an object. Please check your updateTheme function.");
74
+ helpers::assertThat(rt, result.isObject(), "Unistyles: Returned theme is not an object. Please check your updateTheme function.");
75
75
 
76
76
  it->second = jsi::WeakObject(rt, result.asObject(rt));
77
77
  }
@@ -99,7 +99,7 @@ std::shared_ptr<core::StyleSheet> core::UnistylesRegistry::addStyleSheet(int tag
99
99
  return this->_styleSheetRegistry.back();
100
100
  }
101
101
 
102
- std::shared_ptr<core::StyleSheet> core::UnistylesRegistry::getStyleSheetById(int tag) {
102
+ void core::UnistylesRegistry::removeStyleSheet(int tag) {
103
103
  auto it = std::find_if(
104
104
  this->_styleSheetRegistry.begin(),
105
105
  this->_styleSheetRegistry.end(),
@@ -111,12 +111,13 @@ std::shared_ptr<core::StyleSheet> core::UnistylesRegistry::getStyleSheetById(int
111
111
  if (it == this->_styleSheetRegistry.cend()) {
112
112
  throw std::runtime_error("stylesheet with tag: " + std::to_string(tag) + " cannot be found.");
113
113
  }
114
-
115
- return *it;
114
+
115
+ this->_styleSheetRegistry.erase(it);
116
116
  }
117
117
 
118
118
  DependencyMap core::UnistylesRegistry::buildDependencyMap(std::vector<UnistyleDependency>& deps) {
119
119
  DependencyMap dependencyMap;
120
+ std::set<UnistyleDependency> uniqueDependencies(deps.begin(), deps.end());
120
121
 
121
122
  for (const auto& styleSheet : this->_styleSheetRegistry) {
122
123
  for (const auto& [_, unistyle] : styleSheet->unistyles) {
@@ -125,8 +126,8 @@ DependencyMap core::UnistylesRegistry::buildDependencyMap(std::vector<UnistyleDe
125
126
  bool hasAnyOfDependencies = std::any_of(
126
127
  unistyle->dependencies.begin(),
127
128
  unistyle->dependencies.end(),
128
- [&deps](UnistyleDependency dep) {
129
- return std::find(deps.begin(), deps.end(), dep) != deps.end();
129
+ [&uniqueDependencies](UnistyleDependency dep) {
130
+ return std::find(uniqueDependencies.begin(), uniqueDependencies.end(), dep) != uniqueDependencies.end();
130
131
  }
131
132
  );
132
133
 
@@ -1,5 +1,6 @@
1
1
  #pragma once
2
2
 
3
+ #include "set"
3
4
  #include <jsi/jsi.h>
4
5
  #include <react/renderer/uimanager/UIManager.h>
5
6
  #include <unordered_map>
@@ -38,7 +39,7 @@ struct UnistylesRegistry: public StyleSheetRegistry {
38
39
  void linkShadowNodeWithUnistyle(const ShadowNodeFamily*, const core::Unistyle::Shared);
39
40
  void unlinkShadowNodeWithUnistyle(const ShadowNodeFamily*, const core::Unistyle::Shared);
40
41
  std::shared_ptr<core::StyleSheet> addStyleSheet(int tag, core::StyleSheetType type, jsi::Object&& rawValue);
41
- std::shared_ptr<core::StyleSheet> getStyleSheetById(int tag);
42
+ void removeStyleSheet(int tag);
42
43
  DependencyMap buildDependencyMap(std::vector<UnistyleDependency>& deps);
43
44
  DependencyMap buildDependencyMap();
44
45
 
@@ -12,7 +12,7 @@ bool core::UnistylesState::hasAdaptiveThemes() {
12
12
  }
13
13
 
14
14
  void core::UnistylesState::setTheme(std::string themeName) {
15
- helpers::assertThat(*_rt, helpers::vecContainsKeys(this->_registeredThemeNames, {themeName}), "You're trying to set theme to: '" + std::string(themeName) + "', but it wasn't registered.");
15
+ helpers::assertThat(*_rt, helpers::vecContainsKeys(this->_registeredThemeNames, {themeName}), "Unistyles: You're trying to set theme to: '" + std::string(themeName) + "', but it wasn't registered.");
16
16
 
17
17
  if (themeName != this->_currentThemeName) {
18
18
  this->_currentThemeName = themeName;
@@ -31,15 +31,15 @@ jsi::Object core::UnistylesState::getJSTheme() {
31
31
  return jsi::Object(*_rt);
32
32
  }
33
33
 
34
- helpers::assertThat(*_rt, _currentThemeName.has_value(), "one of your stylesheets is trying to get the theme, but no theme has been selected yet. Did you forget to select an initial theme?");
34
+ helpers::assertThat(*_rt, _currentThemeName.has_value(), "Unistyles: One of your stylesheets is trying to get the theme, but no theme has been selected yet. Did you forget to select an initial theme?");
35
35
 
36
36
  auto it = this->_jsThemes.find(_currentThemeName.value());
37
37
 
38
- helpers::assertThat(*_rt, it != this->_jsThemes.end(), "you're trying to get theme '" + _currentThemeName.value() + "', but it was not registered. Did you forget to register it with StyleSheet.configure?");
38
+ helpers::assertThat(*_rt, it != this->_jsThemes.end(), "Unistyles: You're trying to get theme '" + _currentThemeName.value() + "', but it was not registered. Did you forget to register it with StyleSheet.configure?");
39
39
 
40
40
  auto maybeTheme = it->second.lock(*_rt);
41
41
 
42
- helpers::assertThat(*_rt, maybeTheme.isObject(), "unable to retrieve your theme from C++ as it has already been garbage collected, likely due to multiple hot reloads. Please live reload the app.");
42
+ helpers::assertThat(*_rt, maybeTheme.isObject(), "Unistyles: Unable to retrieve your theme from C++ as it has already been garbage collected, likely due to multiple hot reloads. Please live reload the app.");
43
43
 
44
44
  return maybeTheme.asObject(*_rt);
45
45
  }
@@ -4,7 +4,7 @@ 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 == 2, "Invalid babel transform. ShadowRegistry link expected two arguments.");
7
+ helpers::assertThat(rt, count == 2, "Unistyles: Invalid babel transform 'ShadowRegistry link' expected two arguments.");
8
8
 
9
9
  ShadowNode::Shared shadowNodeWrapper = shadowNodeFromValue(rt, args[0]);
10
10
  core::Unistyle::Shared unistyleWrapper = core::unistyleFromValue(rt, args[1]);
@@ -17,7 +17,7 @@ jsi::Value HybridShadowRegistry::link(jsi::Runtime &rt, const jsi::Value &thisVa
17
17
  }
18
18
 
19
19
  jsi::Value HybridShadowRegistry::unlink(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
20
- helpers::assertThat(rt, count == 2, "Invalid babel transform. ShadowRegistry unlink expected two arguments.");
20
+ helpers::assertThat(rt, count == 2, "Unistyles: Invalid babel transform 'ShadowRegistry unlink' expected two arguments.");
21
21
 
22
22
  ShadowNode::Shared shadowNodeWrapper = shadowNodeFromValue(rt, args[0]);
23
23
  core::Unistyle::Shared unistyleWrapper = core::unistyleFromValue(rt, args[1]);
@@ -22,11 +22,7 @@ jsi::Value HybridStyleSheet::create(jsi::Runtime &rt, const jsi::Value &thisVal,
22
22
 
23
23
  // this might happen only when hot reloading
24
24
  if (this->__unid != -1) {
25
- auto registeredStyleSheet = registry.getStyleSheetById(this->__unid);
26
- auto style = std::make_shared<core::HostStyle>(registeredStyleSheet, this->_unistylesRuntime);
27
- auto styleHostObject = jsi::Object::createFromHostObject(rt, style);
28
-
29
- return styleHostObject;
25
+ registry.removeStyleSheet(this->__unid);
30
26
  }
31
27
 
32
28
  jsi::Object rawStyleSheet = arguments[0].asObject(rt);
@@ -57,24 +53,24 @@ jsi::Value HybridStyleSheet::configure(jsi::Runtime &rt, const jsi::Value &thisV
57
53
 
58
54
  helpers::enumerateJSIObject(rt, config, [&](const std::string& propertyName, jsi::Value& propertyValue){
59
55
  if (propertyName == "settings") {
60
- helpers::assertThat(rt, propertyValue.isObject(), "settings must be an object.");
56
+ helpers::assertThat(rt, propertyValue.isObject(), "StyleSheet.configure's settings must be an object.");
61
57
 
62
58
  return this->parseSettings(rt, propertyValue.asObject(rt));
63
59
  }
64
60
 
65
61
  if (propertyName == "breakpoints") {
66
- helpers::assertThat(rt, propertyValue.isObject(), "breakpoints must be an object.");
62
+ helpers::assertThat(rt, propertyValue.isObject(), "StyleSheet.configure's breakpoints must be an object.");
67
63
 
68
64
  return this->parseBreakpoints(rt, propertyValue.asObject(rt));
69
65
  }
70
66
 
71
67
  if (propertyName == "themes") {
72
- helpers::assertThat(rt, propertyValue.isObject(), "themes must be an object.");
68
+ helpers::assertThat(rt, propertyValue.isObject(), "StyleSheet.configure's themes must be an object.");
73
69
 
74
70
  return this->parseThemes(rt, propertyValue.asObject(rt));
75
71
  }
76
72
 
77
- helpers::assertThat(rt, false, "received unexpected key: '" + std::string(propertyName) + "'.");
73
+ helpers::assertThat(rt, false, "StyleSheet.configure received unexpected key: '" + std::string(propertyName) + "'.");
78
74
  });
79
75
 
80
76
  verifyAndSelectTheme(rt);
@@ -88,7 +84,7 @@ void HybridStyleSheet::parseSettings(jsi::Runtime &rt, jsi::Object settings) {
88
84
 
89
85
  helpers::enumerateJSIObject(rt, settings, [&](const std::string& propertyName, jsi::Value& propertyValue){
90
86
  if (propertyName == "adaptiveThemes") {
91
- helpers::assertThat(rt, propertyValue.isBool(), "adaptiveThemes configuration must be of boolean type.");
87
+ helpers::assertThat(rt, propertyValue.isBool(), "StyleSheet.configure's adaptiveThemes must be of boolean type.");
92
88
 
93
89
  registry.setPrefersAdaptiveThemes(rt, propertyValue.asBool());
94
90
 
@@ -97,31 +93,31 @@ void HybridStyleSheet::parseSettings(jsi::Runtime &rt, jsi::Object settings) {
97
93
 
98
94
  if (propertyName == "initialTheme") {
99
95
  if (propertyValue.isObject()) {
100
- helpers::assertThat(rt, propertyValue.asObject(rt).isFunction(rt), "initialTheme configuration must be either a string or a function.");
96
+ helpers::assertThat(rt, propertyValue.asObject(rt).isFunction(rt), "StyleSheet.configure's initialTheme must be either a string or a function.");
101
97
 
102
98
  auto result = propertyValue.asObject(rt).asFunction(rt).call(rt);
103
99
 
104
- helpers::assertThat(rt, result.isString(), "initialTheme resolved from function is not a string. Please check your initialTheme function.");
100
+ helpers::assertThat(rt, result.isString(), "StyleSheet.configure's initialTheme resolved from function is not a string. Please check your initialTheme function.");
105
101
 
106
102
  return registry.setInitialThemeName(rt, result.asString(rt).utf8(rt));
107
103
  }
108
104
 
109
- helpers::assertThat(rt, propertyValue.isString(), "initialTheme configuration must be either a string or a function.");
105
+ helpers::assertThat(rt, propertyValue.isString(), "StyleSheet.configure's initialTheme must be either a string or a function.");
110
106
 
111
107
  registry.setInitialThemeName(rt, propertyValue.asString(rt).utf8(rt));
112
108
 
113
109
  return;
114
110
  }
115
111
 
116
- helpers::assertThat(rt, false, "settings received unexpected key: '" + std::string(propertyName) + "'");
112
+ helpers::assertThat(rt, false, "StyleSheet.configure's settings received unexpected key: '" + std::string(propertyName) + "'");
117
113
  });
118
114
  }
119
115
 
120
116
  void HybridStyleSheet::parseBreakpoints(jsi::Runtime &rt, jsi::Object breakpoints){
121
117
  helpers::Breakpoints sortedBreakpoints = helpers::jsiBreakpointsToVecPairs(rt, std::move(breakpoints));
122
118
 
123
- helpers::assertThat(rt, sortedBreakpoints.size() > 0, "registered breakpoints can't be empty.");
124
- helpers::assertThat(rt, sortedBreakpoints.front().second == 0, "first breakpoint must start from 0.");
119
+ helpers::assertThat(rt, sortedBreakpoints.size() > 0, "StyleSheet.configure's breakpoints can't be empty.");
120
+ helpers::assertThat(rt, sortedBreakpoints.front().second == 0, "StyleSheet.configure's first breakpoint must start from 0.");
125
121
 
126
122
  auto& registry = core::UnistylesRegistry::get();
127
123
  auto& state = registry.getState(rt);
@@ -134,7 +130,7 @@ void HybridStyleSheet::parseThemes(jsi::Runtime &rt, jsi::Object themes) {
134
130
  auto& registry = core::UnistylesRegistry::get();
135
131
 
136
132
  helpers::enumerateJSIObject(rt, themes, [&](const std::string& propertyName, jsi::Value& propertyValue){
137
- helpers::assertThat(rt, propertyValue.isObject(), "registered theme '" + propertyName + "' must be an object.");
133
+ helpers::assertThat(rt, propertyValue.isObject(), "StyleSheet.configure's registered theme '" + propertyName + "' must be an object.");
138
134
 
139
135
  registry.registerTheme(rt, propertyName, propertyValue.asObject(rt));
140
136
  });
@@ -152,7 +148,7 @@ void HybridStyleSheet::verifyAndSelectTheme(jsi::Runtime &rt) {
152
148
 
153
149
  // user tries to enable adaptive themes, but didn't register both 'light' and 'dark' themes
154
150
  if (prefersAdaptiveThemes && !hasAdaptiveThemes) {
155
- helpers::assertThat(rt, false, "you're trying to enable adaptiveThemes, but you didn't register both 'light' and 'dark' themes.");
151
+ helpers::assertThat(rt, false, "Unistyles: You're trying to enable adaptiveThemes, but you didn't register both 'light' and 'dark' themes.");
156
152
  }
157
153
 
158
154
  // user didn't select initial theme nor can have adaptive themes, and registered more than 1 theme
@@ -176,14 +172,14 @@ void HybridStyleSheet::verifyAndSelectTheme(jsi::Runtime &rt) {
176
172
  // user selected both initial theme and adaptive themes
177
173
  // we should throw an error as these options are mutually exclusive
178
174
  if (hasInitialTheme && hasAdaptiveThemes) {
179
- helpers::assertThat(rt, false, "you're trying to set initial theme and enable adaptiveThemes, but these options are mutually exclusive.");
175
+ helpers::assertThat(rt, false, "Unistyles: You're trying to set initial theme and enable adaptiveThemes, but these options are mutually exclusive.");
180
176
  }
181
177
 
182
178
  // user only selected initial theme
183
179
  // validate if following theme exist
184
180
  std::string selectedTheme = state.getInitialTheme().value();
185
181
 
186
- helpers::assertThat(rt, state.hasTheme(selectedTheme), "you're trying to select theme '" + selectedTheme + "' but it wasn't registered.");
182
+ helpers::assertThat(rt, state.hasTheme(selectedTheme), "Unistyles: You're trying to select theme '" + selectedTheme + "' but it wasn't registered.");
187
183
 
188
184
  state.setTheme(selectedTheme);
189
185
  }
@@ -202,18 +198,18 @@ void HybridStyleSheet::setThemeFromColorScheme(jsi::Runtime& rt) {
202
198
 
203
199
  return;
204
200
  default:
205
- throw std::runtime_error("unable to set adaptive theme as your device doesn't support it.");
201
+ throw std::runtime_error("Unistyles: Unable to set adaptive theme as your device doesn't support it.");
206
202
  }
207
203
  }
208
204
 
209
205
  void HybridStyleSheet::loadExternalMethods(const jsi::Value& thisValue, jsi::Runtime& rt) {
210
206
  auto jsMethods = thisValue.getObject(rt).getProperty(rt, "jsMethods");
211
207
 
212
- helpers::assertThat(rt, jsMethods.isObject(), "can't find jsMethods.");
208
+ helpers::assertThat(rt, jsMethods.isObject(), "Unistyles: Can't find jsMethods.");
213
209
 
214
210
  auto maybeProcessColorFn = jsMethods.asObject(rt).getProperty(rt, "processColor");
215
211
 
216
- helpers::assertThat(rt, maybeProcessColorFn.isObject(), "can't load processColor function from JS.");
212
+ helpers::assertThat(rt, maybeProcessColorFn.isObject(), "Unistyles: Can't load processColor function from JS.");
217
213
 
218
214
  auto processColorFn = maybeProcessColorFn.asObject(rt).asFunction(rt);
219
215
  auto& registry = core::UnistylesRegistry::get();
@@ -225,9 +221,17 @@ void HybridStyleSheet::loadExternalMethods(const jsi::Value& thisValue, jsi::Run
225
221
  void HybridStyleSheet::onPlatformDependenciesChange(std::vector<UnistyleDependency> dependencies) {
226
222
  auto& registry = core::UnistylesRegistry::get();
227
223
  auto parser = parser::Parser(this->_unistylesRuntime);
228
- auto dependencyMap = registry.buildDependencyMap(dependencies);
229
224
  auto& rt = this->_unistylesRuntime->getRuntime();
230
225
 
226
+ // check if color scheme changed and then if Unistyles state depend on it (adaptive themes)
227
+ auto colorSchemeIt = std::find(dependencies.begin(), dependencies.end(), UnistyleDependency::COLORSCHEME);
228
+
229
+ if (colorSchemeIt != dependencies.end()) {
230
+ this->_unistylesRuntime->includeDependenciesForColorSchemeChange(dependencies);
231
+ }
232
+
233
+ auto dependencyMap = registry.buildDependencyMap(dependencies);
234
+
231
235
  if (dependencyMap.size() == 0) {
232
236
  return;
233
237
  }
@@ -58,7 +58,7 @@ double HybridUnistylesRuntime::getFontScale() {
58
58
  };
59
59
 
60
60
  void HybridUnistylesRuntime::setTheme(const std::string &themeName) {
61
- helpers::assertThat(*_rt, !this->getHasAdaptiveThemes(), "You're trying to set theme to: '" + themeName + "', but adaptiveThemes are enabled.");
61
+ helpers::assertThat(*_rt, !this->getHasAdaptiveThemes(), "Unistyles: You're trying to set theme to: '" + themeName + "', but adaptiveThemes are enabled.");
62
62
 
63
63
  auto& state = core::UnistylesRegistry::get().getState(*_rt);
64
64
 
@@ -71,7 +71,7 @@ void HybridUnistylesRuntime::setAdaptiveThemes(bool isEnabled) {
71
71
 
72
72
  std::vector<UnistyleDependency> changedDependencies{};
73
73
 
74
- changedDependencies.reserve(5);
74
+ changedDependencies.reserve(3);
75
75
 
76
76
  bool hadAdaptiveThemes = this->getHasAdaptiveThemes();
77
77
 
@@ -92,6 +92,11 @@ void HybridUnistylesRuntime::setAdaptiveThemes(bool isEnabled) {
92
92
 
93
93
  // if user enabled adaptive themes, then we need to make sure
94
94
  // we selected theme based on color scheme
95
+ this->calculateNewThemeAndDependencies(changedDependencies);
96
+ this->_onDependenciesChange(changedDependencies);
97
+ };
98
+
99
+ void HybridUnistylesRuntime::calculateNewThemeAndDependencies(std::vector<UnistyleDependency>& changedDependencies) {
95
100
  auto& state = core::UnistylesRegistry::get().getState(*_rt);
96
101
  auto colorScheme = this->getColorScheme();
97
102
  auto currentThemeName = this->getThemeName();
@@ -105,9 +110,7 @@ void HybridUnistylesRuntime::setAdaptiveThemes(bool isEnabled) {
105
110
 
106
111
  state.setTheme(nextTheme);
107
112
  }
108
-
109
- this->_onDependenciesChange(changedDependencies);
110
- };
113
+ }
111
114
 
112
115
  jsi::Value HybridUnistylesRuntime::updateTheme(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
113
116
  helpers::assertThat(rt, count == 2, "UnistylesRuntime.updateTheme expected to be called with 2 arguments.");
@@ -117,7 +120,7 @@ jsi::Value HybridUnistylesRuntime::updateTheme(jsi::Runtime &rt, const jsi::Valu
117
120
  auto& registry = core::UnistylesRegistry::get();
118
121
  auto themeName = args[0].asString(rt).utf8(rt);
119
122
 
120
- helpers::assertThat(rt, args[1].asObject(rt).isFunction(rt), "second argument expected to be a function.");
123
+ helpers::assertThat(rt, args[1].asObject(rt).isFunction(rt), "UnistylesRuntime.updateTheme expected second argument to be a function.");
121
124
 
122
125
  registry.updateTheme(rt, themeName, args[1].asObject(rt).asFunction(rt));
123
126
 
@@ -201,3 +204,15 @@ void HybridUnistylesRuntime::registerPlatformListener(const std::function<void(s
201
204
  this->_nativePlatform.registerPlatformListener(listener);
202
205
  this->_onDependenciesChange = listener;
203
206
  }
207
+
208
+ void HybridUnistylesRuntime::includeDependenciesForColorSchemeChange(std::vector<UnistyleDependency>& deps) {
209
+ auto& registry = core::UnistylesRegistry::get();
210
+ auto& state = registry.getState(*this->_rt);
211
+
212
+ // ignore color scheme changes if user has no adaptive themes
213
+ if (!state.hasAdaptiveThemes()) {
214
+ return;
215
+ }
216
+
217
+ this->calculateNewThemeAndDependencies(deps);
218
+ }
@@ -58,6 +58,8 @@ struct HybridUnistylesRuntime: public HybridUnistylesRuntimeSpec {
58
58
  UnistylesCxxMiniRuntime getMiniRuntime() override;
59
59
  jsi::Value getMiniRuntimeAsValue(jsi::Runtime& rt);
60
60
  jsi::Runtime& getRuntime();
61
+ void includeDependenciesForColorSchemeChange(std::vector<UnistyleDependency>& deps);
62
+ void calculateNewThemeAndDependencies(std::vector<UnistyleDependency>& deps);
61
63
 
62
64
  private:
63
65
  jsi::Runtime* _rt;
@@ -11,7 +11,7 @@ void parser::Parser::buildUnistyles(jsi::Runtime& rt, std::shared_ptr<StyleSheet
11
11
  jsi::Object unwrappedStyleSheet = this->unwrapStyleSheet(rt, styleSheet);
12
12
 
13
13
  helpers::enumerateJSIObject(rt, unwrappedStyleSheet, [&](const std::string& styleKey, jsi::Value& propertyValue){
14
- helpers::assertThat(rt, propertyValue.isObject(), "style with name '" + styleKey + "' is not a function or object.");
14
+ helpers::assertThat(rt, propertyValue.isObject(), "Unistyles: Style with name '" + styleKey + "' is not a function or object.");
15
15
 
16
16
  jsi::Object styleValue = propertyValue.asObject(rt);
17
17
 
@@ -128,7 +128,7 @@ void parser::Parser::rebuildUnistyle(jsi::Runtime& rt, std::shared_ptr<StyleShee
128
128
  auto unistyleFn = std::dynamic_pointer_cast<UnistyleDynamicFunction>(unistyle);
129
129
  auto maybeMetadata = unistyleFn->dynamicFunctionMetadata;
130
130
 
131
- helpers::assertThat(rt, maybeMetadata.has_value(), "Your dynamic function '" + unistyleFn->styleKey + "' has no metadata and can't be processed.");
131
+ helpers::assertThat(rt, maybeMetadata.has_value(), "Unistyles: Your dynamic function '" + unistyleFn->styleKey + "' has no metadata and can't be processed.");
132
132
 
133
133
  // convert arguments to jsi::Value
134
134
  auto metadata = unistyleFn->dynamicFunctionMetadata.value();
@@ -316,10 +316,6 @@ jsi::Object parser::Parser::parseFirstLevel(jsi::Runtime& rt, Unistyle::Shared u
316
316
 
317
317
  parsedStyle.setProperty(rt, jsi::PropNameID::forUtf8(rt, propertyName), this->parseSecondLevel(rt, unistyle, valueFromBreakpoint));
318
318
  });
319
-
320
- if (shouldParseVariants) {
321
- unistyle->addDependency(UnistyleDependency::VARIANTS);
322
- }
323
319
 
324
320
  if (shouldParseVariants && !variants.empty()) {
325
321
  auto propertyValueObject = style.getProperty(rt, "variants").asObject(rt);
@@ -371,7 +367,7 @@ jsi::Function parser::Parser::createDynamicFunctionProxy(jsi::Runtime& rt, Unist
371
367
 
372
368
  // function convert babel generated dependencies to C++ dependencies
373
369
  std::vector<UnistyleDependency> parser::Parser::parseDependencies(jsi::Runtime &rt, jsi::Object&& dependencies) {
374
- helpers::assertThat(rt, dependencies.isArray(rt), "babel transform is invalid. Unexpected type for dependencies. Please report new Github issue.");
370
+ helpers::assertThat(rt, dependencies.isArray(rt), "Unistyles: Babel transform is invalid - unexpected type for dependencies.");
375
371
 
376
372
  std::vector<UnistyleDependency> parsedDependencies{};
377
373
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-unistyles",
3
- "version": "3.0.0-alpha.3",
3
+ "version": "3.0.0-alpha.5",
4
4
  "description": "Level up your React Native StyleSheet",
5
5
  "scripts": {
6
6
  "test": "jest",