react-native-unistyles 3.0.0-alpha.1 → 3.0.0-alpha.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (264) hide show
  1. package/Unistyles.podspec +33 -0
  2. package/cxx/common/Constants.h +1 -0
  3. package/cxx/common/Helpers.h +92 -0
  4. package/cxx/core/HostStyle.cpp +18 -12
  5. package/cxx/core/HostStyle.h +3 -0
  6. package/cxx/core/StyleSheet.h +0 -1
  7. package/cxx/core/StyleSheetRegistry.cpp +10 -13
  8. package/cxx/core/StyleSheetRegistry.h +4 -4
  9. package/cxx/core/Unistyle.h +7 -10
  10. package/cxx/core/UnistyleData.h +23 -0
  11. package/cxx/core/UnistyleWrapper.h +25 -7
  12. package/cxx/core/UnistylesCommitHook.cpp +8 -7
  13. package/cxx/core/UnistylesCommitHook.h +6 -1
  14. package/cxx/core/UnistylesCommitShadowNode.h +13 -0
  15. package/cxx/core/UnistylesMountHook.cpp +40 -0
  16. package/cxx/core/UnistylesMountHook.h +30 -0
  17. package/cxx/core/UnistylesRegistry.cpp +44 -75
  18. package/cxx/core/UnistylesRegistry.h +10 -11
  19. package/cxx/core/UnistylesState.cpp +4 -4
  20. package/cxx/hybridObjects/HybridShadowRegistry.cpp +13 -6
  21. package/cxx/hybridObjects/HybridStyleSheet.cpp +35 -39
  22. package/cxx/hybridObjects/HybridStyleSheet.h +9 -6
  23. package/cxx/hybridObjects/HybridUnistylesRuntime.cpp +34 -15
  24. package/cxx/hybridObjects/HybridUnistylesRuntime.h +2 -0
  25. package/cxx/parser/Parser.cpp +87 -165
  26. package/cxx/parser/Parser.h +8 -14
  27. package/cxx/shadowTree/ShadowLeafUpdate.h +1 -1
  28. package/cxx/shadowTree/ShadowTreeManager.cpp +3 -8
  29. package/ios/UnistylesModuleOnLoad.h +2 -0
  30. package/ios/UnistylesModuleOnLoad.mm +8 -1
  31. package/lib/commonjs/specs/NavigtionBar/index.js +1 -1
  32. package/lib/commonjs/specs/NavigtionBar/index.js.map +1 -1
  33. package/lib/commonjs/specs/ShadowRegistry/index.js +3 -3
  34. package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
  35. package/lib/commonjs/specs/StatusBar/index.js +1 -1
  36. package/lib/commonjs/specs/StatusBar/index.js.map +1 -1
  37. package/lib/commonjs/specs/StyleSheet/index.js.map +1 -1
  38. package/lib/commonjs/specs/UnistylesRuntime/index.js +1 -1
  39. package/lib/commonjs/specs/UnistylesRuntime/index.js.map +1 -1
  40. package/lib/commonjs/specs/index.web.js +1 -1
  41. package/lib/commonjs/web/convert/boxShadow.js +77 -0
  42. package/lib/commonjs/web/convert/boxShadow.js.map +1 -0
  43. package/lib/commonjs/web/convert/breakpoint.js +25 -0
  44. package/lib/commonjs/web/convert/breakpoint.js.map +1 -0
  45. package/lib/commonjs/web/convert/index.js +76 -0
  46. package/lib/commonjs/web/convert/index.js.map +1 -0
  47. package/lib/commonjs/web/convert/module.d.js +2 -0
  48. package/lib/commonjs/web/convert/module.d.js.map +1 -0
  49. package/lib/commonjs/web/convert/shadow.js +68 -0
  50. package/lib/commonjs/web/convert/shadow.js.map +1 -0
  51. package/lib/commonjs/web/convert/style.js +89 -0
  52. package/lib/commonjs/web/convert/style.js.map +1 -0
  53. package/lib/commonjs/web/convert/textShadow.js +73 -0
  54. package/lib/commonjs/web/convert/textShadow.js.map +1 -0
  55. package/lib/commonjs/web/convert/transform.js +72 -0
  56. package/lib/commonjs/web/convert/transform.js.map +1 -0
  57. package/lib/commonjs/web/convert/types.js +9 -0
  58. package/lib/commonjs/web/convert/types.js.map +1 -0
  59. package/lib/commonjs/web/convert/utils.js +55 -0
  60. package/lib/commonjs/web/convert/utils.js.map +1 -0
  61. package/lib/commonjs/web/create.js +89 -0
  62. package/lib/commonjs/web/create.js.map +1 -0
  63. package/lib/commonjs/web/index.js +51 -0
  64. package/lib/commonjs/web/index.js.map +1 -0
  65. package/lib/commonjs/web/listener/index.js +13 -0
  66. package/lib/commonjs/web/listener/index.js.map +1 -0
  67. package/lib/commonjs/web/listener/listener.js +36 -0
  68. package/lib/commonjs/web/listener/listener.js.map +1 -0
  69. package/lib/commonjs/web/mock.js +37 -0
  70. package/lib/commonjs/web/mock.js.map +1 -0
  71. package/lib/commonjs/web/mq.js +23 -0
  72. package/lib/commonjs/web/mq.js.map +1 -0
  73. package/lib/commonjs/web/pseudo.js +11 -0
  74. package/lib/commonjs/web/pseudo.js.map +1 -0
  75. package/lib/commonjs/web/registry.js +37 -0
  76. package/lib/commonjs/web/registry.js.map +1 -0
  77. package/lib/commonjs/web/runtime.js +164 -0
  78. package/lib/commonjs/web/runtime.js.map +1 -0
  79. package/lib/commonjs/web/state.js +121 -0
  80. package/lib/commonjs/web/state.js.map +1 -0
  81. package/lib/commonjs/web/utils.js +78 -0
  82. package/lib/commonjs/web/utils.js.map +1 -0
  83. package/lib/commonjs/web/variants/getVariants.js +39 -0
  84. package/lib/commonjs/web/variants/getVariants.js.map +1 -0
  85. package/lib/commonjs/web/variants/index.js +28 -0
  86. package/lib/commonjs/web/variants/index.js.map +1 -0
  87. package/lib/commonjs/web/variants/useVariants.js +75 -0
  88. package/lib/commonjs/web/variants/useVariants.js.map +1 -0
  89. package/lib/module/specs/NavigtionBar/index.js +1 -1
  90. package/lib/module/specs/NavigtionBar/index.js.map +1 -1
  91. package/lib/module/specs/ShadowRegistry/index.js +3 -3
  92. package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
  93. package/lib/module/specs/StatusBar/index.js +1 -1
  94. package/lib/module/specs/StatusBar/index.js.map +1 -1
  95. package/lib/module/specs/StyleSheet/index.js.map +1 -1
  96. package/lib/module/specs/UnistylesRuntime/index.js +1 -1
  97. package/lib/module/specs/UnistylesRuntime/index.js.map +1 -1
  98. package/lib/module/specs/index.web.js +1 -1
  99. package/lib/module/specs/index.web.js.map +1 -1
  100. package/lib/module/web/convert/boxShadow.js +72 -0
  101. package/lib/module/web/convert/boxShadow.js.map +1 -0
  102. package/lib/module/web/convert/breakpoint.js +20 -0
  103. package/lib/module/web/convert/breakpoint.js.map +1 -0
  104. package/lib/module/web/convert/index.js +71 -0
  105. package/lib/module/web/convert/index.js.map +1 -0
  106. package/lib/module/web/convert/module.d.js +2 -0
  107. package/lib/module/web/convert/module.d.js.map +1 -0
  108. package/lib/module/web/convert/shadow.js +63 -0
  109. package/lib/module/web/convert/shadow.js.map +1 -0
  110. package/lib/module/web/convert/style.js +84 -0
  111. package/lib/module/web/convert/style.js.map +1 -0
  112. package/lib/module/web/convert/textShadow.js +68 -0
  113. package/lib/module/web/convert/textShadow.js.map +1 -0
  114. package/lib/module/web/convert/transform.js +67 -0
  115. package/lib/module/web/convert/transform.js.map +1 -0
  116. package/lib/module/web/convert/types.js +5 -0
  117. package/lib/module/web/convert/types.js.map +1 -0
  118. package/lib/module/web/convert/utils.js +43 -0
  119. package/lib/module/web/convert/utils.js.map +1 -0
  120. package/lib/module/web/create.js +84 -0
  121. package/lib/module/web/create.js.map +1 -0
  122. package/lib/module/web/index.js +24 -0
  123. package/lib/module/web/index.js.map +1 -0
  124. package/lib/module/web/listener/index.js +4 -0
  125. package/lib/module/web/listener/index.js.map +1 -0
  126. package/lib/module/web/listener/listener.js +31 -0
  127. package/lib/module/web/listener/listener.js.map +1 -0
  128. package/lib/module/web/mock.js +33 -0
  129. package/lib/module/web/mock.js.map +1 -0
  130. package/lib/module/web/mq.js +17 -0
  131. package/lib/module/web/mq.js.map +1 -0
  132. package/lib/module/web/pseudo.js +6 -0
  133. package/lib/module/web/pseudo.js.map +1 -0
  134. package/lib/module/web/registry.js +33 -0
  135. package/lib/module/web/registry.js.map +1 -0
  136. package/lib/module/web/runtime.js +160 -0
  137. package/lib/module/web/runtime.js.map +1 -0
  138. package/lib/module/web/state.js +117 -0
  139. package/lib/module/web/state.js.map +1 -0
  140. package/lib/module/web/utils.js +65 -0
  141. package/lib/module/web/utils.js.map +1 -0
  142. package/lib/module/web/variants/getVariants.js +34 -0
  143. package/lib/module/web/variants/getVariants.js.map +1 -0
  144. package/lib/module/web/variants/index.js +5 -0
  145. package/lib/module/web/variants/index.js.map +1 -0
  146. package/lib/module/web/variants/useVariants.js +70 -0
  147. package/lib/module/web/variants/useVariants.js.map +1 -0
  148. package/lib/typescript/example/App.d.ts.map +1 -1
  149. package/lib/typescript/example/Typography.d.ts +12 -0
  150. package/lib/typescript/example/Typography.d.ts.map +1 -0
  151. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts +2 -2
  152. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
  153. package/lib/typescript/src/specs/StyleSheet/index.d.ts +2 -1
  154. package/lib/typescript/src/specs/StyleSheet/index.d.ts.map +1 -1
  155. package/lib/typescript/src/specs/index.web.d.ts +1 -1
  156. package/lib/typescript/src/specs/index.web.d.ts.map +1 -1
  157. package/lib/typescript/src/types/stylesheet.d.ts +1 -1
  158. package/lib/typescript/src/types/stylesheet.d.ts.map +1 -1
  159. package/lib/typescript/src/web/convert/boxShadow.d.ts.map +1 -0
  160. package/lib/typescript/src/web/convert/breakpoint.d.ts.map +1 -0
  161. package/lib/typescript/{web → src/web}/convert/index.d.ts +1 -1
  162. package/lib/typescript/src/web/convert/index.d.ts.map +1 -0
  163. package/lib/typescript/src/web/convert/shadow.d.ts.map +1 -0
  164. package/lib/typescript/src/web/convert/style.d.ts.map +1 -0
  165. package/lib/typescript/src/web/convert/textShadow.d.ts.map +1 -0
  166. package/lib/typescript/src/web/convert/transform.d.ts.map +1 -0
  167. package/lib/typescript/{web → src/web}/convert/types.d.ts +1 -1
  168. package/lib/typescript/src/web/convert/types.d.ts.map +1 -0
  169. package/lib/typescript/src/web/convert/utils.d.ts.map +1 -0
  170. package/lib/typescript/{web → src/web}/create.d.ts +11 -11
  171. package/lib/typescript/{web → src/web}/create.d.ts.map +1 -1
  172. package/lib/typescript/{web → src/web}/index.d.ts +12 -12
  173. package/lib/typescript/{web → src/web}/index.d.ts.map +1 -1
  174. package/lib/typescript/src/web/listener/index.d.ts.map +1 -0
  175. package/lib/typescript/{web → src/web}/listener/listener.d.ts +1 -1
  176. package/lib/typescript/src/web/listener/listener.d.ts.map +1 -0
  177. package/lib/typescript/{web → src/web}/mock.d.ts +2 -2
  178. package/lib/typescript/src/web/mock.d.ts.map +1 -0
  179. package/lib/typescript/src/web/mq.d.ts.map +1 -0
  180. package/lib/typescript/src/web/pseudo.d.ts.map +1 -0
  181. package/lib/typescript/{web → src/web}/registry.d.ts +1 -1
  182. package/lib/typescript/src/web/registry.d.ts.map +1 -0
  183. package/lib/typescript/{web → src/web}/runtime.d.ts +6 -6
  184. package/lib/typescript/src/web/runtime.d.ts.map +1 -0
  185. package/lib/typescript/{web → src/web}/state.d.ts +4 -4
  186. package/lib/typescript/src/web/state.d.ts.map +1 -0
  187. package/lib/typescript/{web → src/web}/utils.d.ts +5 -3
  188. package/lib/typescript/src/web/utils.d.ts.map +1 -0
  189. package/lib/typescript/src/web/variants/getVariants.d.ts +3 -0
  190. package/lib/typescript/src/web/variants/getVariants.d.ts.map +1 -0
  191. package/lib/typescript/src/web/variants/index.d.ts +3 -0
  192. package/lib/typescript/src/web/variants/index.d.ts.map +1 -0
  193. package/lib/typescript/src/web/variants/useVariants.d.ts +3 -0
  194. package/lib/typescript/src/web/variants/useVariants.d.ts.map +1 -0
  195. package/package.json +2 -2
  196. package/plugin/__tests__/dependencies.spec.js +195 -107
  197. package/plugin/__tests__/ref.spec.js +537 -179
  198. package/plugin/__tests__/stylesheet.spec.js +174 -63
  199. package/plugin/index.js +68 -23
  200. package/plugin/ref.js +74 -12
  201. package/plugin/style.js +57 -24
  202. package/plugin/stylesheet.js +44 -1
  203. package/plugin/variants.js +33 -0
  204. package/src/specs/NavigtionBar/index.ts +1 -1
  205. package/src/specs/ShadowRegistry/index.ts +5 -5
  206. package/src/specs/StatusBar/index.ts +1 -1
  207. package/src/specs/StyleSheet/index.ts +3 -1
  208. package/src/specs/UnistylesRuntime/index.ts +1 -1
  209. package/src/specs/index.web.ts +1 -1
  210. package/src/types/stylesheet.ts +1 -1
  211. package/{web → src/web}/convert/breakpoint.ts +1 -1
  212. package/{web → src/web}/convert/index.ts +1 -1
  213. package/{web → src/web}/convert/types.ts +1 -1
  214. package/{web → src/web}/create.ts +36 -33
  215. package/{web → src/web}/listener/listener.ts +1 -1
  216. package/{web → src/web}/mock.ts +2 -2
  217. package/{web → src/web}/registry.ts +1 -1
  218. package/{web → src/web}/runtime.ts +4 -4
  219. package/{web → src/web}/state.ts +5 -5
  220. package/{web → src/web}/utils.ts +5 -5
  221. package/src/web/variants/getVariants.ts +42 -0
  222. package/src/web/variants/index.ts +2 -0
  223. package/{web → src/web/variants}/useVariants.ts +24 -44
  224. package/lib/typescript/web/convert/boxShadow.d.ts.map +0 -1
  225. package/lib/typescript/web/convert/breakpoint.d.ts.map +0 -1
  226. package/lib/typescript/web/convert/index.d.ts.map +0 -1
  227. package/lib/typescript/web/convert/shadow.d.ts.map +0 -1
  228. package/lib/typescript/web/convert/style.d.ts.map +0 -1
  229. package/lib/typescript/web/convert/textShadow.d.ts.map +0 -1
  230. package/lib/typescript/web/convert/transform.d.ts.map +0 -1
  231. package/lib/typescript/web/convert/types.d.ts.map +0 -1
  232. package/lib/typescript/web/convert/utils.d.ts.map +0 -1
  233. package/lib/typescript/web/listener/index.d.ts.map +0 -1
  234. package/lib/typescript/web/listener/listener.d.ts.map +0 -1
  235. package/lib/typescript/web/mock.d.ts.map +0 -1
  236. package/lib/typescript/web/mq.d.ts.map +0 -1
  237. package/lib/typescript/web/pseudo.d.ts.map +0 -1
  238. package/lib/typescript/web/registry.d.ts.map +0 -1
  239. package/lib/typescript/web/runtime.d.ts.map +0 -1
  240. package/lib/typescript/web/state.d.ts.map +0 -1
  241. package/lib/typescript/web/useVariants.d.ts +0 -3
  242. package/lib/typescript/web/useVariants.d.ts.map +0 -1
  243. package/lib/typescript/web/utils.d.ts.map +0 -1
  244. /package/lib/typescript/{web → src/web}/convert/boxShadow.d.ts +0 -0
  245. /package/lib/typescript/{web → src/web}/convert/breakpoint.d.ts +0 -0
  246. /package/lib/typescript/{web → src/web}/convert/shadow.d.ts +0 -0
  247. /package/lib/typescript/{web → src/web}/convert/style.d.ts +0 -0
  248. /package/lib/typescript/{web → src/web}/convert/textShadow.d.ts +0 -0
  249. /package/lib/typescript/{web → src/web}/convert/transform.d.ts +0 -0
  250. /package/lib/typescript/{web → src/web}/convert/utils.d.ts +0 -0
  251. /package/lib/typescript/{web → src/web}/listener/index.d.ts +0 -0
  252. /package/lib/typescript/{web → src/web}/mq.d.ts +0 -0
  253. /package/lib/typescript/{web → src/web}/pseudo.d.ts +0 -0
  254. /package/{web → src/web}/convert/boxShadow.ts +0 -0
  255. /package/{web → src/web}/convert/module.d.ts +0 -0
  256. /package/{web → src/web}/convert/shadow.ts +0 -0
  257. /package/{web → src/web}/convert/style.ts +0 -0
  258. /package/{web → src/web}/convert/textShadow.ts +0 -0
  259. /package/{web → src/web}/convert/transform.ts +0 -0
  260. /package/{web → src/web}/convert/utils.ts +0 -0
  261. /package/{web → src/web}/index.ts +0 -0
  262. /package/{web → src/web}/listener/index.ts +0 -0
  263. /package/{web → src/web}/mq.ts +0 -0
  264. /package/{web → src/web}/pseudo.ts +0 -0
@@ -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,71 +57,61 @@ 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
-
66
- helpers::assertThat(rt, it != state._jsThemes.end(), "you're trying to update theme '" + themeName + "' but it wasn't registered.");
67
-
60
+
61
+ helpers::assertThat(rt, it != state._jsThemes.end(), "Unistyles: You're trying to update theme '" + themeName + "' but it wasn't registered.");
62
+
68
63
  auto currentThemeValue = it->second.lock(rt);
69
-
70
- helpers::assertThat(rt, currentThemeValue.isObject(), "unable to update your theme from C++. It was already garbage collected.");
71
-
64
+
65
+ helpers::assertThat(rt, currentThemeValue.isObject(), "Unistyles: Unable to update your theme from C++. It was already garbage collected.");
66
+
72
67
  auto result = callback.call(rt, currentThemeValue.asObject(rt));
73
-
74
- helpers::assertThat(rt, result.isObject(), "returned theme is not an object. Please check your updateTheme function.");
68
+
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(const ShadowNodeFamily* shadowNodeFamily, const core::Unistyle::Shared unistyle) {
74
+ void core::UnistylesRegistry::linkShadowNodeWithUnistyle(
75
+ const ShadowNodeFamily* shadowNodeFamily,
76
+ const core::Unistyle::Shared unistyle,
77
+ Variants& variants,
78
+ std::vector<folly::dynamic>& arguments
79
+ ) {
80
80
  if (!this->_shadowRegistry.contains(shadowNodeFamily)) {
81
81
  this->_shadowRegistry[shadowNodeFamily] = {};
82
82
  }
83
-
84
- this->_shadowRegistry[shadowNodeFamily].emplace_back(unistyle);
83
+
84
+ this->_shadowRegistry[shadowNodeFamily].emplace_back(std::make_shared<UnistyleData>(unistyle, variants, arguments));
85
85
  }
86
86
 
87
87
  void core::UnistylesRegistry::unlinkShadowNodeWithUnistyle(const ShadowNodeFamily* shadowNodeFamily, const core::Unistyle::Shared unistyle) {
88
88
  auto& unistylesVec = this->_shadowRegistry[shadowNodeFamily];
89
- auto it = std::find(unistylesVec.begin(), unistylesVec.end(), unistyle);
90
-
89
+ auto it = std::find_if(unistylesVec.begin(), unistylesVec.end(), [unistyle](std::shared_ptr<UnistyleData> unistyleData){
90
+ return unistyleData->unistyle == unistyle;
91
+ });
92
+
91
93
  if (it != unistylesVec.end()) {
92
94
  this->_shadowRegistry[shadowNodeFamily].erase(it);
93
95
  }
94
96
  }
95
97
 
96
- std::shared_ptr<core::StyleSheet> core::UnistylesRegistry::addStyleSheet(int tag, core::StyleSheetType type, jsi::Object&& rawValue) {
97
- this->_styleSheetRegistry.emplace_back(std::make_shared<core::StyleSheet>(tag, type, std::move(rawValue)));
98
+ std::shared_ptr<core::StyleSheet> core::UnistylesRegistry::addStyleSheet(jsi::Runtime& rt, int unid, core::StyleSheetType type, jsi::Object&& rawValue) {
99
+ this->_styleSheetRegistry[&rt][unid] = std::make_shared<core::StyleSheet>(unid, type, std::move(rawValue));
98
100
 
99
- return this->_styleSheetRegistry.back();
101
+ return this->_styleSheetRegistry[&rt][unid];
100
102
  }
101
103
 
102
- std::shared_ptr<core::StyleSheet> core::UnistylesRegistry::getStyleSheetById(int tag) {
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
- return *it;
116
- }
117
-
118
- DependencyMap core::UnistylesRegistry::buildDependencyMap(std::vector<UnistyleDependency>& deps) {
104
+ core::DependencyMap core::UnistylesRegistry::buildDependencyMap(jsi::Runtime& rt, std::vector<UnistyleDependency>& deps) {
119
105
  DependencyMap dependencyMap;
120
-
121
- for (const auto& styleSheet : this->_styleSheetRegistry) {
122
- for (const auto& [_, unistyle] : styleSheet->unistyles) {
123
- // check if in the given stylesheet we have unistyle
124
- // that depends on something affected
106
+ std::set<UnistyleDependency> uniqueDependencies(deps.begin(), deps.end());
107
+
108
+ for (const auto& [family, unistyles] : this->_shadowRegistry) {
109
+ for (const auto& unistyleData : unistyles) {
125
110
  bool hasAnyOfDependencies = std::any_of(
126
- unistyle->dependencies.begin(),
127
- unistyle->dependencies.end(),
128
- [&deps](UnistyleDependency dep) {
129
- return std::find(deps.begin(), deps.end(), dep) != deps.end();
111
+ unistyleData->unistyle->dependencies.begin(),
112
+ unistyleData->unistyle->dependencies.end(),
113
+ [&uniqueDependencies](UnistyleDependency dep) {
114
+ return std::find(uniqueDependencies.begin(), uniqueDependencies.end(), dep) != uniqueDependencies.end();
130
115
  }
131
116
  );
132
117
 
@@ -134,41 +119,25 @@ DependencyMap core::UnistylesRegistry::buildDependencyMap(std::vector<UnistyleDe
134
119
  continue;
135
120
  }
136
121
 
137
- // if so, we need to find shadow family too
138
- for (const auto& pair : this->_shadowRegistry) {
139
- const auto& [family, unistyles] = pair;
140
-
141
- for (const auto& shadowUnistyle : unistyles) {
142
- if (unistyle != shadowUnistyle) {
143
- continue;
144
- }
145
-
146
- dependencyMap[styleSheet][family].push_back(shadowUnistyle);
147
- }
122
+ // we need to take in count all unistyles from the shadowNode
123
+ // as user might be using spreads and not all of them may have dependencies
124
+ for (const auto& unistyleData : unistyles) {
125
+ dependencyMap[family].emplace_back(unistyleData);
148
126
  }
127
+
128
+ break;
149
129
  }
150
130
  }
151
-
131
+
152
132
  return dependencyMap;
153
133
  }
154
134
 
155
- DependencyMap core::UnistylesRegistry::buildDependencyMap() {
135
+ core::DependencyMap core::UnistylesRegistry::buildDependencyMap(jsi::Runtime& rt) {
156
136
  DependencyMap dependencyMap;
157
-
158
- for (const auto& styleSheet : this->_styleSheetRegistry) {
159
- for (const auto& [_, unistyle] : styleSheet->unistyles) {
160
- for (const auto& pair : this->_shadowRegistry) {
161
- const auto& [family, unistyles] = pair;
162
-
163
- for (const auto& shadowUnistyle : unistyles) {
164
- if (unistyle != shadowUnistyle) {
165
- continue;
166
- }
167
-
168
- // make sure to use base Unistyle class
169
- dependencyMap[styleSheet][family].push_back(shadowUnistyle);
170
- }
171
- }
137
+
138
+ for (const auto& [family, unistyles] : this->_shadowRegistry) {
139
+ for (const auto& unistyleData : unistyles) {
140
+ dependencyMap[family].emplace_back(unistyleData);
172
141
  }
173
142
  }
174
143
 
@@ -1,6 +1,8 @@
1
1
  #pragma once
2
2
 
3
+ #include "set"
3
4
  #include <jsi/jsi.h>
5
+ #include <folly/dynamic.h>
4
6
  #include <react/renderer/uimanager/UIManager.h>
5
7
  #include <unordered_map>
6
8
  #include <unordered_set>
@@ -8,6 +10,7 @@
8
10
  #include "StyleSheetRegistry.h"
9
11
  #include "StyleSheet.h"
10
12
  #include "Unistyle.h"
13
+ #include "UnistyleData.h"
11
14
 
12
15
  namespace margelo::nitro::unistyles::core {
13
16
 
@@ -16,10 +19,7 @@ struct UnistylesState;
16
19
  using namespace facebook;
17
20
  using namespace facebook::react;
18
21
 
19
- using DependencyMap = std::unordered_map<
20
- std::shared_ptr<core::StyleSheet>,
21
- std::unordered_map<const ShadowNodeFamily*, std::vector<core::Unistyle::Shared>>
22
- >;
22
+ using DependencyMap = std::unordered_map<const ShadowNodeFamily*, std::vector<std::shared_ptr<UnistyleData>>>;
23
23
 
24
24
  struct UnistylesRegistry: public StyleSheetRegistry {
25
25
  static UnistylesRegistry& get();
@@ -35,19 +35,18 @@ struct UnistylesRegistry: public StyleSheetRegistry {
35
35
 
36
36
  UnistylesState& getState(jsi::Runtime& rt);
37
37
  void createState(jsi::Runtime& rt);
38
- void linkShadowNodeWithUnistyle(const ShadowNodeFamily*, const core::Unistyle::Shared);
38
+ void linkShadowNodeWithUnistyle(const ShadowNodeFamily*, const core::Unistyle::Shared, Variants& variants, std::vector<folly::dynamic>&);
39
39
  void unlinkShadowNodeWithUnistyle(const ShadowNodeFamily*, const core::Unistyle::Shared);
40
- std::shared_ptr<core::StyleSheet> addStyleSheet(int tag, core::StyleSheetType type, jsi::Object&& rawValue);
41
- std::shared_ptr<core::StyleSheet> getStyleSheetById(int tag);
42
- DependencyMap buildDependencyMap(std::vector<UnistyleDependency>& deps);
43
- DependencyMap buildDependencyMap();
40
+ std::shared_ptr<core::StyleSheet> addStyleSheet(jsi::Runtime& rt, int tag, core::StyleSheetType type, jsi::Object&& rawValue);
41
+ DependencyMap buildDependencyMap(jsi::Runtime& rt, std::vector<UnistyleDependency>& deps);
42
+ DependencyMap buildDependencyMap(jsi::Runtime& rt);
44
43
 
45
44
  private:
46
45
  UnistylesRegistry() = default;
47
46
 
48
47
  std::unordered_map<jsi::Runtime*, UnistylesState> _states{};
49
- std::vector<std::shared_ptr<core::StyleSheet>> _styleSheetRegistry{};
50
- std::unordered_map<const ShadowNodeFamily*, std::vector<core::Unistyle::Shared>> _shadowRegistry{};
48
+ std::unordered_map<jsi::Runtime*, std::unordered_map<int, std::shared_ptr<core::StyleSheet>>> _styleSheetRegistry{};
49
+ std::unordered_map<const ShadowNodeFamily*, std::vector<std::shared_ptr<UnistyleData>>> _shadowRegistry{};
51
50
  };
52
51
 
53
52
  UnistylesRegistry& UnistylesRegistry::get() {
@@ -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,23 +4,30 @@ 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 == 4, "Unistyles: Invalid babel transform 'ShadowRegistry link' expected 4 arguments.");
8
+
7
9
  ShadowNode::Shared shadowNodeWrapper = shadowNodeFromValue(rt, args[0]);
8
10
  core::Unistyle::Shared unistyleWrapper = core::unistyleFromValue(rt, args[1]);
9
-
11
+ core::Variants variants = helpers::variantsToPairs(rt, args[2].asObject(rt));
12
+ auto rawArguments = args[3].asObject(rt).asArray(rt);
13
+ std::vector<folly::dynamic> arguments = helpers::parseDynamicFunctionArguments(rt, rawArguments);
14
+
10
15
  auto& registry = core::UnistylesRegistry::get();
11
-
12
- registry.linkShadowNodeWithUnistyle(&shadowNodeWrapper->getFamily(), unistyleWrapper);
13
-
16
+
17
+ registry.linkShadowNodeWithUnistyle(&shadowNodeWrapper->getFamily(), unistyleWrapper, variants, arguments);
18
+
14
19
  return jsi::Value::undefined();
15
20
  }
16
21
 
17
22
  jsi::Value HybridShadowRegistry::unlink(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
23
+ helpers::assertThat(rt, count == 2, "Unistyles: Invalid babel transform 'ShadowRegistry unlink' expected 2 arguments.");
24
+
18
25
  ShadowNode::Shared shadowNodeWrapper = shadowNodeFromValue(rt, args[0]);
19
26
  core::Unistyle::Shared unistyleWrapper = core::unistyleFromValue(rt, args[1]);
20
-
27
+
21
28
  auto& registry = core::UnistylesRegistry::get();
22
29
 
23
30
  registry.unlinkShadowNodeWithUnistyle(&shadowNodeWrapper->getFamily(), unistyleWrapper);
24
-
31
+
25
32
  return jsi::Value::undefined();
26
33
  }
@@ -13,23 +13,17 @@ double HybridStyleSheet::get___unid() {
13
13
  return this->__unid;
14
14
  }
15
15
 
16
- jsi::Value HybridStyleSheet::create(jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) {
17
- helpers::assertThat(rt, arguments[0].isObject(), "expected to be called with object or function.");
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.");
19
+ helpers::assertThat(rt, arguments[0].isObject(), "StyleSheet.create expected to be called with object or function.");
18
20
 
19
21
  auto thisStyleSheet = thisVal.asObject(rt);
20
22
  auto& registry = core::UnistylesRegistry::get();
21
-
22
- // this might happen only when hot reloading
23
- if (this->__unid != -1) {
24
- auto registeredStyleSheet = registry.getStyleSheetById(this->__unid);
25
- auto style = std::make_shared<core::HostStyle>(registeredStyleSheet, this->_unistylesRuntime);
26
- auto styleHostObject = jsi::Object::createFromHostObject(rt, style);
27
-
28
- return styleHostObject;
29
- }
23
+ int unid = arguments[1].asNumber();
30
24
 
31
25
  jsi::Object rawStyleSheet = arguments[0].asObject(rt);
32
- auto registeredStyleSheet = registry.addStyleSheetFromValue(rt, std::move(rawStyleSheet));
26
+ auto registeredStyleSheet = registry.addStyleSheetFromValue(rt, std::move(rawStyleSheet), unid);
33
27
 
34
28
  this->__unid = registeredStyleSheet->tag;
35
29
 
@@ -45,7 +39,8 @@ jsi::Value HybridStyleSheet::create(jsi::Runtime &rt, const jsi::Value &thisVal,
45
39
  }
46
40
 
47
41
  jsi::Value HybridStyleSheet::configure(jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) {
48
- helpers::assertThat(rt, arguments[0].isObject(), "expected to be called with object.");
42
+ helpers::assertThat(rt, count == 1, "StyleSheet.configure expected to be called with one argument.");
43
+ helpers::assertThat(rt, arguments[0].isObject(), "StyleSheet.configure expected to be called with object.");
49
44
 
50
45
  // create new state
51
46
  auto config = arguments[0].asObject(rt);
@@ -55,29 +50,28 @@ jsi::Value HybridStyleSheet::configure(jsi::Runtime &rt, const jsi::Value &thisV
55
50
 
56
51
  helpers::enumerateJSIObject(rt, config, [&](const std::string& propertyName, jsi::Value& propertyValue){
57
52
  if (propertyName == "settings") {
58
- helpers::assertThat(rt, propertyValue.isObject(), "settings must be an object.");
53
+ helpers::assertThat(rt, propertyValue.isObject(), "StyleSheet.configure's settings must be an object.");
59
54
 
60
55
  return this->parseSettings(rt, propertyValue.asObject(rt));
61
56
  }
62
57
 
63
58
  if (propertyName == "breakpoints") {
64
- helpers::assertThat(rt, propertyValue.isObject(), "breakpoints must be an object.");
59
+ helpers::assertThat(rt, propertyValue.isObject(), "StyleSheet.configure's breakpoints must be an object.");
65
60
 
66
61
  return this->parseBreakpoints(rt, propertyValue.asObject(rt));
67
62
  }
68
63
 
69
64
  if (propertyName == "themes") {
70
- helpers::assertThat(rt, propertyValue.isObject(), "themes must be an object.");
65
+ helpers::assertThat(rt, propertyValue.isObject(), "StyleSheet.configure's themes must be an object.");
71
66
 
72
67
  return this->parseThemes(rt, propertyValue.asObject(rt));
73
68
  }
74
69
 
75
- helpers::assertThat(rt, false, "received unexpected key: '" + std::string(propertyName) + "'.");
70
+ helpers::assertThat(rt, false, "StyleSheet.configure received unexpected key: '" + std::string(propertyName) + "'.");
76
71
  });
77
72
 
78
73
  verifyAndSelectTheme(rt);
79
74
  loadExternalMethods(thisVal, rt);
80
- registerCommitHook(rt);
81
75
 
82
76
  return jsi::Value::undefined();
83
77
  }
@@ -87,7 +81,7 @@ void HybridStyleSheet::parseSettings(jsi::Runtime &rt, jsi::Object settings) {
87
81
 
88
82
  helpers::enumerateJSIObject(rt, settings, [&](const std::string& propertyName, jsi::Value& propertyValue){
89
83
  if (propertyName == "adaptiveThemes") {
90
- helpers::assertThat(rt, propertyValue.isBool(), "adaptiveThemes configuration must be of boolean type.");
84
+ helpers::assertThat(rt, propertyValue.isBool(), "StyleSheet.configure's adaptiveThemes must be of boolean type.");
91
85
 
92
86
  registry.setPrefersAdaptiveThemes(rt, propertyValue.asBool());
93
87
 
@@ -96,31 +90,31 @@ void HybridStyleSheet::parseSettings(jsi::Runtime &rt, jsi::Object settings) {
96
90
 
97
91
  if (propertyName == "initialTheme") {
98
92
  if (propertyValue.isObject()) {
99
- helpers::assertThat(rt, propertyValue.asObject(rt).isFunction(rt), "initialTheme configuration must be either a string or a function.");
93
+ helpers::assertThat(rt, propertyValue.asObject(rt).isFunction(rt), "StyleSheet.configure's initialTheme must be either a string or a function.");
100
94
 
101
95
  auto result = propertyValue.asObject(rt).asFunction(rt).call(rt);
102
96
 
103
- helpers::assertThat(rt, result.isString(), "initialTheme resolved from function is not a string. Please check your initialTheme function.");
97
+ helpers::assertThat(rt, result.isString(), "StyleSheet.configure's initialTheme resolved from function is not a string. Please check your initialTheme function.");
104
98
 
105
99
  return registry.setInitialThemeName(rt, result.asString(rt).utf8(rt));
106
100
  }
107
101
 
108
- helpers::assertThat(rt, propertyValue.isString(), "initialTheme configuration must be either a string or a function.");
102
+ helpers::assertThat(rt, propertyValue.isString(), "StyleSheet.configure's initialTheme must be either a string or a function.");
109
103
 
110
104
  registry.setInitialThemeName(rt, propertyValue.asString(rt).utf8(rt));
111
105
 
112
106
  return;
113
107
  }
114
108
 
115
- helpers::assertThat(rt, false, "settings received unexpected key: '" + std::string(propertyName) + "'");
109
+ helpers::assertThat(rt, false, "StyleSheet.configure's settings received unexpected key: '" + std::string(propertyName) + "'");
116
110
  });
117
111
  }
118
112
 
119
113
  void HybridStyleSheet::parseBreakpoints(jsi::Runtime &rt, jsi::Object breakpoints){
120
114
  helpers::Breakpoints sortedBreakpoints = helpers::jsiBreakpointsToVecPairs(rt, std::move(breakpoints));
121
115
 
122
- helpers::assertThat(rt, sortedBreakpoints.size() > 0, "registered breakpoints can't be empty.");
123
- helpers::assertThat(rt, sortedBreakpoints.front().second == 0, "first breakpoint must start from 0.");
116
+ helpers::assertThat(rt, sortedBreakpoints.size() > 0, "StyleSheet.configure's breakpoints can't be empty.");
117
+ helpers::assertThat(rt, sortedBreakpoints.front().second == 0, "StyleSheet.configure's first breakpoint must start from 0.");
124
118
 
125
119
  auto& registry = core::UnistylesRegistry::get();
126
120
  auto& state = registry.getState(rt);
@@ -133,7 +127,7 @@ void HybridStyleSheet::parseThemes(jsi::Runtime &rt, jsi::Object themes) {
133
127
  auto& registry = core::UnistylesRegistry::get();
134
128
 
135
129
  helpers::enumerateJSIObject(rt, themes, [&](const std::string& propertyName, jsi::Value& propertyValue){
136
- helpers::assertThat(rt, propertyValue.isObject(), "registered theme '" + propertyName + "' must be an object.");
130
+ helpers::assertThat(rt, propertyValue.isObject(), "StyleSheet.configure's registered theme '" + propertyName + "' must be an object.");
137
131
 
138
132
  registry.registerTheme(rt, propertyName, propertyValue.asObject(rt));
139
133
  });
@@ -151,7 +145,7 @@ void HybridStyleSheet::verifyAndSelectTheme(jsi::Runtime &rt) {
151
145
 
152
146
  // user tries to enable adaptive themes, but didn't register both 'light' and 'dark' themes
153
147
  if (prefersAdaptiveThemes && !hasAdaptiveThemes) {
154
- helpers::assertThat(rt, false, "you're trying to enable adaptiveThemes, but you didn't register both 'light' and 'dark' themes.");
148
+ helpers::assertThat(rt, false, "Unistyles: You're trying to enable adaptiveThemes, but you didn't register both 'light' and 'dark' themes.");
155
149
  }
156
150
 
157
151
  // user didn't select initial theme nor can have adaptive themes, and registered more than 1 theme
@@ -175,14 +169,14 @@ void HybridStyleSheet::verifyAndSelectTheme(jsi::Runtime &rt) {
175
169
  // user selected both initial theme and adaptive themes
176
170
  // we should throw an error as these options are mutually exclusive
177
171
  if (hasInitialTheme && hasAdaptiveThemes) {
178
- helpers::assertThat(rt, false, "you're trying to set initial theme and enable adaptiveThemes, but these options are mutually exclusive.");
172
+ helpers::assertThat(rt, false, "Unistyles: You're trying to set initial theme and enable adaptiveThemes, but these options are mutually exclusive.");
179
173
  }
180
174
 
181
175
  // user only selected initial theme
182
176
  // validate if following theme exist
183
177
  std::string selectedTheme = state.getInitialTheme().value();
184
178
 
185
- helpers::assertThat(rt, state.hasTheme(selectedTheme), "you're trying to select theme '" + selectedTheme + "' but it wasn't registered.");
179
+ helpers::assertThat(rt, state.hasTheme(selectedTheme), "Unistyles: You're trying to select theme '" + selectedTheme + "' but it wasn't registered.");
186
180
 
187
181
  state.setTheme(selectedTheme);
188
182
  }
@@ -201,18 +195,18 @@ void HybridStyleSheet::setThemeFromColorScheme(jsi::Runtime& rt) {
201
195
 
202
196
  return;
203
197
  default:
204
- throw std::runtime_error("unable to set adaptive theme as your device doesn't support it.");
198
+ throw std::runtime_error("Unistyles: Unable to set adaptive theme as your device doesn't support it.");
205
199
  }
206
200
  }
207
201
 
208
202
  void HybridStyleSheet::loadExternalMethods(const jsi::Value& thisValue, jsi::Runtime& rt) {
209
203
  auto jsMethods = thisValue.getObject(rt).getProperty(rt, "jsMethods");
210
204
 
211
- helpers::assertThat(rt, jsMethods.isObject(), "can't find jsMethods.");
205
+ helpers::assertThat(rt, jsMethods.isObject(), "Unistyles: Can't find jsMethods.");
212
206
 
213
207
  auto maybeProcessColorFn = jsMethods.asObject(rt).getProperty(rt, "processColor");
214
208
 
215
- helpers::assertThat(rt, maybeProcessColorFn.isObject(), "can't load processColor function from JS.");
209
+ helpers::assertThat(rt, maybeProcessColorFn.isObject(), "Unistyles: Can't load processColor function from JS.");
216
210
 
217
211
  auto processColorFn = maybeProcessColorFn.asObject(rt).asFunction(rt);
218
212
  auto& registry = core::UnistylesRegistry::get();
@@ -224,9 +218,17 @@ void HybridStyleSheet::loadExternalMethods(const jsi::Value& thisValue, jsi::Run
224
218
  void HybridStyleSheet::onPlatformDependenciesChange(std::vector<UnistyleDependency> dependencies) {
225
219
  auto& registry = core::UnistylesRegistry::get();
226
220
  auto parser = parser::Parser(this->_unistylesRuntime);
227
- auto dependencyMap = registry.buildDependencyMap(dependencies);
228
221
  auto& rt = this->_unistylesRuntime->getRuntime();
229
222
 
223
+ // check if color scheme changed and then if Unistyles state depend on it (adaptive themes)
224
+ auto colorSchemeIt = std::find(dependencies.begin(), dependencies.end(), UnistyleDependency::COLORSCHEME);
225
+
226
+ if (colorSchemeIt != dependencies.end()) {
227
+ this->_unistylesRuntime->includeDependenciesForColorSchemeChange(dependencies);
228
+ }
229
+
230
+ auto dependencyMap = registry.buildDependencyMap(rt, dependencies);
231
+
230
232
  if (dependencyMap.size() == 0) {
231
233
  return;
232
234
  }
@@ -237,9 +239,3 @@ void HybridStyleSheet::onPlatformDependenciesChange(std::vector<UnistyleDependen
237
239
 
238
240
  shadow::ShadowTreeManager::updateShadowTree(rt, shadowLeafUpdates);
239
241
  }
240
-
241
- void HybridStyleSheet::registerCommitHook(jsi::Runtime &rt) {
242
- UIManager& uiManager = const_cast<UIManager&>(UIManagerBinding::getBinding(rt)->getUIManager());
243
-
244
- this->_unistylesCommitHook = std::make_shared<core::UnistylesCommitHook>(uiManager, this->_unistylesRuntime);
245
- }
@@ -11,16 +11,19 @@
11
11
  #include "Parser.h"
12
12
  #include "ShadowTreeManager.h"
13
13
  #include "UnistylesCommitHook.h"
14
+ #include "UnistylesMountHook.h"
14
15
 
15
16
  using namespace margelo::nitro::unistyles;
16
17
  using namespace facebook::react;
17
18
 
18
19
  struct HybridStyleSheet: public HybridUnistylesStyleSheetSpec {
19
- HybridStyleSheet(std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime)
20
- : HybridObject(TAG), _unistylesRuntime{unistylesRuntime} {
21
- this->_unistylesRuntime->registerPlatformListener(
22
- std::bind(&HybridStyleSheet::onPlatformDependenciesChange, this, std::placeholders::_1)
23
- );
20
+ HybridStyleSheet(std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime, std::shared_ptr<UIManager> uiManager)
21
+ : HybridObject(TAG), _unistylesRuntime{unistylesRuntime} {
22
+ this->_unistylesCommitHook = std::make_shared<core::UnistylesCommitHook>(uiManager, unistylesRuntime);
23
+ this->_unistylesMountHook = std::make_shared<core::UnistylesMountHook>(uiManager, unistylesRuntime);
24
+ this->_unistylesRuntime->registerPlatformListener(
25
+ std::bind(&HybridStyleSheet::onPlatformDependenciesChange, this, std::placeholders::_1)
26
+ );
24
27
  }
25
28
 
26
29
  jsi::Value create(jsi::Runtime& rt,
@@ -52,10 +55,10 @@ private:
52
55
  void setThemeFromColorScheme(jsi::Runtime& rt);
53
56
  void loadExternalMethods(const jsi::Value& thisValue, jsi::Runtime& rt);
54
57
  void onPlatformDependenciesChange(std::vector<UnistyleDependency> dependencies);
55
- void registerCommitHook(jsi::Runtime& rt);
56
58
 
57
59
  double __unid = -1;
58
60
  std::shared_ptr<HybridUnistylesRuntime> _unistylesRuntime;
59
61
  std::shared_ptr<core::UnistylesCommitHook> _unistylesCommitHook;
62
+ std::shared_ptr<core::UnistylesMountHook> _unistylesMountHook;
60
63
  };
61
64
 
@@ -45,7 +45,7 @@ Insets HybridUnistylesRuntime::getInsets() {
45
45
 
46
46
  Orientation HybridUnistylesRuntime::getOrientation() {
47
47
  int orientation = this->_nativePlatform.getOrientation();
48
-
48
+
49
49
  return static_cast<Orientation>(orientation);
50
50
  };
51
51
 
@@ -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
 
@@ -68,14 +68,17 @@ void HybridUnistylesRuntime::setTheme(const std::string &themeName) {
68
68
 
69
69
  void HybridUnistylesRuntime::setAdaptiveThemes(bool isEnabled) {
70
70
  auto& registry = core::UnistylesRegistry::get();
71
-
71
+
72
72
  std::vector<UnistyleDependency> changedDependencies{};
73
+
74
+ changedDependencies.reserve(3);
75
+
73
76
  bool hadAdaptiveThemes = this->getHasAdaptiveThemes();
74
77
 
75
78
  registry.setPrefersAdaptiveThemes(*_rt, isEnabled);
76
-
79
+
77
80
  bool haveAdaptiveThemes = this->getHasAdaptiveThemes();
78
-
81
+
79
82
  if (hadAdaptiveThemes != haveAdaptiveThemes) {
80
83
  changedDependencies.push_back(UnistyleDependency::ADAPTIVETHEMES);
81
84
  }
@@ -83,12 +86,17 @@ void HybridUnistylesRuntime::setAdaptiveThemes(bool isEnabled) {
83
86
  // if user disabled it, or can't have adaptive themes, do nothing
84
87
  if (!this->getHasAdaptiveThemes()) {
85
88
  this->_onDependenciesChange(changedDependencies);
86
-
89
+
87
90
  return;
88
91
  }
89
92
 
90
93
  // if user enabled adaptive themes, then we need to make sure
91
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) {
92
100
  auto& state = core::UnistylesRegistry::get().getState(*_rt);
93
101
  auto colorScheme = this->getColorScheme();
94
102
  auto currentThemeName = this->getThemeName();
@@ -99,24 +107,23 @@ void HybridUnistylesRuntime::setAdaptiveThemes(bool isEnabled) {
99
107
  if (!currentThemeName.has_value() || nextTheme != currentThemeName.value()) {
100
108
  changedDependencies.push_back(UnistyleDependency::THEME);
101
109
  changedDependencies.push_back(UnistyleDependency::THEMENAME);
102
-
110
+
103
111
  state.setTheme(nextTheme);
104
112
  }
105
-
106
- this->_onDependenciesChange(changedDependencies);
107
- };
113
+ }
108
114
 
109
115
  jsi::Value HybridUnistylesRuntime::updateTheme(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
110
- helpers::assertThat(rt, args[0].isString(), "first argument expected to be a string.");
111
- helpers::assertThat(rt, args[1].isObject(), "second argument expected to be a function.");
116
+ helpers::assertThat(rt, count == 2, "UnistylesRuntime.updateTheme expected to be called with 2 arguments.");
117
+ helpers::assertThat(rt, args[0].isString(), "UnistylesRuntime.updateTheme expected first argument to be a string.");
118
+ helpers::assertThat(rt, args[1].isObject(), "UnistylesRuntime.updateTheme expected first argument to be a function.");
112
119
 
113
120
  auto& registry = core::UnistylesRegistry::get();
114
121
  auto themeName = args[0].asString(rt).utf8(rt);
115
122
 
116
- 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.");
117
124
 
118
125
  registry.updateTheme(rt, themeName, args[1].asObject(rt).asFunction(rt));
119
-
126
+
120
127
  this->_onDependenciesChange({UnistyleDependency::THEME});
121
128
 
122
129
  return jsi::Value::undefined();
@@ -163,7 +170,7 @@ UnistylesCxxMiniRuntime HybridUnistylesRuntime::getMiniRuntime() {
163
170
  nativeMiniRuntime.statusBar,
164
171
  nativeMiniRuntime.navigationBar
165
172
  };
166
-
173
+
167
174
  return cxxMiniRuntime;
168
175
  }
169
176
 
@@ -197,3 +204,15 @@ void HybridUnistylesRuntime::registerPlatformListener(const std::function<void(s
197
204
  this->_nativePlatform.registerPlatformListener(listener);
198
205
  this->_onDependenciesChange = listener;
199
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
+ }