react-native-unistyles 3.0.0-alpha.6 → 3.0.0-alpha.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (233) hide show
  1. package/cxx/common/Helpers.h +24 -0
  2. package/cxx/core/HostStyle.cpp +4 -5
  3. package/cxx/core/HostStyle.h +4 -0
  4. package/cxx/core/StyleSheet.h +0 -1
  5. package/cxx/core/StyleSheetRegistry.cpp +9 -17
  6. package/cxx/core/StyleSheetRegistry.h +3 -4
  7. package/cxx/core/UnistyleData.h +22 -0
  8. package/cxx/core/UnistylesCommitHook.cpp +4 -1
  9. package/cxx/core/UnistylesRegistry.cpp +33 -49
  10. package/cxx/core/UnistylesRegistry.h +8 -8
  11. package/cxx/hybridObjects/HybridShadowRegistry.cpp +4 -3
  12. package/cxx/hybridObjects/HybridStyleSheet.cpp +8 -11
  13. package/cxx/parser/Parser.cpp +41 -57
  14. package/cxx/parser/Parser.h +8 -13
  15. package/lib/commonjs/specs/ShadowRegistry/index.js +2 -2
  16. package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
  17. package/lib/commonjs/specs/StyleSheet/index.js.map +1 -1
  18. package/lib/commonjs/specs/index.web.js +1 -1
  19. package/lib/commonjs/web/convert/boxShadow.js +77 -0
  20. package/lib/commonjs/web/convert/boxShadow.js.map +1 -0
  21. package/lib/commonjs/web/convert/breakpoint.js +25 -0
  22. package/lib/commonjs/web/convert/breakpoint.js.map +1 -0
  23. package/lib/commonjs/web/convert/index.js +76 -0
  24. package/lib/commonjs/web/convert/index.js.map +1 -0
  25. package/lib/commonjs/web/convert/module.d.js +2 -0
  26. package/lib/commonjs/web/convert/module.d.js.map +1 -0
  27. package/lib/commonjs/web/convert/shadow.js +68 -0
  28. package/lib/commonjs/web/convert/shadow.js.map +1 -0
  29. package/lib/commonjs/web/convert/style.js +89 -0
  30. package/lib/commonjs/web/convert/style.js.map +1 -0
  31. package/lib/commonjs/web/convert/textShadow.js +73 -0
  32. package/lib/commonjs/web/convert/textShadow.js.map +1 -0
  33. package/lib/commonjs/web/convert/transform.js +72 -0
  34. package/lib/commonjs/web/convert/transform.js.map +1 -0
  35. package/lib/commonjs/web/convert/types.js +9 -0
  36. package/lib/commonjs/web/convert/types.js.map +1 -0
  37. package/lib/commonjs/web/convert/utils.js +55 -0
  38. package/lib/commonjs/web/convert/utils.js.map +1 -0
  39. package/lib/commonjs/web/create.js +89 -0
  40. package/lib/commonjs/web/create.js.map +1 -0
  41. package/lib/commonjs/web/index.js +51 -0
  42. package/lib/commonjs/web/index.js.map +1 -0
  43. package/lib/commonjs/web/listener/index.js +13 -0
  44. package/lib/commonjs/web/listener/index.js.map +1 -0
  45. package/lib/commonjs/web/listener/listener.js +36 -0
  46. package/lib/commonjs/web/listener/listener.js.map +1 -0
  47. package/lib/commonjs/web/mock.js +37 -0
  48. package/lib/commonjs/web/mock.js.map +1 -0
  49. package/lib/commonjs/web/mq.js +23 -0
  50. package/lib/commonjs/web/mq.js.map +1 -0
  51. package/lib/commonjs/web/pseudo.js +11 -0
  52. package/lib/commonjs/web/pseudo.js.map +1 -0
  53. package/lib/commonjs/web/registry.js +37 -0
  54. package/lib/commonjs/web/registry.js.map +1 -0
  55. package/lib/commonjs/web/runtime.js +164 -0
  56. package/lib/commonjs/web/runtime.js.map +1 -0
  57. package/lib/commonjs/web/state.js +121 -0
  58. package/lib/commonjs/web/state.js.map +1 -0
  59. package/lib/commonjs/web/utils.js +78 -0
  60. package/lib/commonjs/web/utils.js.map +1 -0
  61. package/lib/commonjs/web/variants/getVariants.js +39 -0
  62. package/lib/commonjs/web/variants/getVariants.js.map +1 -0
  63. package/lib/commonjs/web/variants/index.js +28 -0
  64. package/lib/commonjs/web/variants/index.js.map +1 -0
  65. package/lib/commonjs/web/variants/useVariants.js +75 -0
  66. package/lib/commonjs/web/variants/useVariants.js.map +1 -0
  67. package/lib/module/specs/ShadowRegistry/index.js +2 -2
  68. package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
  69. package/lib/module/specs/StyleSheet/index.js.map +1 -1
  70. package/lib/module/specs/index.web.js +1 -1
  71. package/lib/module/specs/index.web.js.map +1 -1
  72. package/lib/module/web/convert/boxShadow.js +72 -0
  73. package/lib/module/web/convert/boxShadow.js.map +1 -0
  74. package/lib/module/web/convert/breakpoint.js +20 -0
  75. package/lib/module/web/convert/breakpoint.js.map +1 -0
  76. package/lib/module/web/convert/index.js +71 -0
  77. package/lib/module/web/convert/index.js.map +1 -0
  78. package/lib/module/web/convert/module.d.js +2 -0
  79. package/lib/module/web/convert/module.d.js.map +1 -0
  80. package/lib/module/web/convert/shadow.js +63 -0
  81. package/lib/module/web/convert/shadow.js.map +1 -0
  82. package/lib/module/web/convert/style.js +84 -0
  83. package/lib/module/web/convert/style.js.map +1 -0
  84. package/lib/module/web/convert/textShadow.js +68 -0
  85. package/lib/module/web/convert/textShadow.js.map +1 -0
  86. package/lib/module/web/convert/transform.js +67 -0
  87. package/lib/module/web/convert/transform.js.map +1 -0
  88. package/lib/module/web/convert/types.js +5 -0
  89. package/lib/module/web/convert/types.js.map +1 -0
  90. package/lib/module/web/convert/utils.js +43 -0
  91. package/lib/module/web/convert/utils.js.map +1 -0
  92. package/lib/module/web/create.js +84 -0
  93. package/lib/module/web/create.js.map +1 -0
  94. package/lib/module/web/index.js +24 -0
  95. package/lib/module/web/index.js.map +1 -0
  96. package/lib/module/web/listener/index.js +4 -0
  97. package/lib/module/web/listener/index.js.map +1 -0
  98. package/lib/module/web/listener/listener.js +31 -0
  99. package/lib/module/web/listener/listener.js.map +1 -0
  100. package/lib/module/web/mock.js +33 -0
  101. package/lib/module/web/mock.js.map +1 -0
  102. package/lib/module/web/mq.js +17 -0
  103. package/lib/module/web/mq.js.map +1 -0
  104. package/lib/module/web/pseudo.js +6 -0
  105. package/lib/module/web/pseudo.js.map +1 -0
  106. package/lib/module/web/registry.js +33 -0
  107. package/lib/module/web/registry.js.map +1 -0
  108. package/lib/module/web/runtime.js +160 -0
  109. package/lib/module/web/runtime.js.map +1 -0
  110. package/lib/module/web/state.js +117 -0
  111. package/lib/module/web/state.js.map +1 -0
  112. package/lib/module/web/utils.js +65 -0
  113. package/lib/module/web/utils.js.map +1 -0
  114. package/lib/module/web/variants/getVariants.js +34 -0
  115. package/lib/module/web/variants/getVariants.js.map +1 -0
  116. package/lib/module/web/variants/index.js +5 -0
  117. package/lib/module/web/variants/index.js.map +1 -0
  118. package/lib/module/web/variants/useVariants.js +70 -0
  119. package/lib/module/web/variants/useVariants.js.map +1 -0
  120. package/lib/typescript/example/App.d.ts.map +1 -1
  121. package/lib/typescript/example/Typography.d.ts +11 -0
  122. package/lib/typescript/example/Typography.d.ts.map +1 -0
  123. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts +2 -2
  124. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
  125. package/lib/typescript/src/specs/StyleSheet/index.d.ts +2 -1
  126. package/lib/typescript/src/specs/StyleSheet/index.d.ts.map +1 -1
  127. package/lib/typescript/src/specs/index.web.d.ts +1 -1
  128. package/lib/typescript/src/specs/index.web.d.ts.map +1 -1
  129. package/lib/typescript/src/types/stylesheet.d.ts +1 -1
  130. package/lib/typescript/src/types/stylesheet.d.ts.map +1 -1
  131. package/lib/typescript/src/web/convert/boxShadow.d.ts.map +1 -0
  132. package/lib/typescript/src/web/convert/breakpoint.d.ts.map +1 -0
  133. package/lib/typescript/{web → src/web}/convert/index.d.ts +1 -1
  134. package/lib/typescript/src/web/convert/index.d.ts.map +1 -0
  135. package/lib/typescript/src/web/convert/shadow.d.ts.map +1 -0
  136. package/lib/typescript/src/web/convert/style.d.ts.map +1 -0
  137. package/lib/typescript/src/web/convert/textShadow.d.ts.map +1 -0
  138. package/lib/typescript/src/web/convert/transform.d.ts.map +1 -0
  139. package/lib/typescript/{web → src/web}/convert/types.d.ts +1 -1
  140. package/lib/typescript/src/web/convert/types.d.ts.map +1 -0
  141. package/lib/typescript/src/web/convert/utils.d.ts.map +1 -0
  142. package/lib/typescript/{web → src/web}/create.d.ts +11 -11
  143. package/lib/typescript/{web → src/web}/create.d.ts.map +1 -1
  144. package/lib/typescript/{web → src/web}/index.d.ts +12 -12
  145. package/lib/typescript/{web → src/web}/index.d.ts.map +1 -1
  146. package/lib/typescript/src/web/listener/index.d.ts.map +1 -0
  147. package/lib/typescript/{web → src/web}/listener/listener.d.ts +1 -1
  148. package/lib/typescript/src/web/listener/listener.d.ts.map +1 -0
  149. package/lib/typescript/{web → src/web}/mock.d.ts +2 -2
  150. package/lib/typescript/src/web/mock.d.ts.map +1 -0
  151. package/lib/typescript/src/web/mq.d.ts.map +1 -0
  152. package/lib/typescript/src/web/pseudo.d.ts.map +1 -0
  153. package/lib/typescript/{web → src/web}/registry.d.ts +1 -1
  154. package/lib/typescript/src/web/registry.d.ts.map +1 -0
  155. package/lib/typescript/{web → src/web}/runtime.d.ts +6 -6
  156. package/lib/typescript/src/web/runtime.d.ts.map +1 -0
  157. package/lib/typescript/{web → src/web}/state.d.ts +4 -4
  158. package/lib/typescript/src/web/state.d.ts.map +1 -0
  159. package/lib/typescript/{web → src/web}/utils.d.ts +5 -3
  160. package/lib/typescript/src/web/utils.d.ts.map +1 -0
  161. package/lib/typescript/src/web/variants/getVariants.d.ts +3 -0
  162. package/lib/typescript/src/web/variants/getVariants.d.ts.map +1 -0
  163. package/lib/typescript/src/web/variants/index.d.ts +3 -0
  164. package/lib/typescript/src/web/variants/index.d.ts.map +1 -0
  165. package/lib/typescript/src/web/variants/useVariants.d.ts +3 -0
  166. package/lib/typescript/src/web/variants/useVariants.d.ts.map +1 -0
  167. package/package.json +1 -1
  168. package/plugin/__tests__/dependencies.spec.js +181 -103
  169. package/plugin/__tests__/ref.spec.js +346 -156
  170. package/plugin/__tests__/stylesheet.spec.js +148 -55
  171. package/plugin/index.js +31 -21
  172. package/plugin/ref.js +67 -15
  173. package/plugin/style.js +13 -24
  174. package/plugin/stylesheet.js +44 -1
  175. package/plugin/variants.js +33 -0
  176. package/src/specs/ShadowRegistry/index.ts +4 -4
  177. package/src/specs/StyleSheet/index.ts +3 -1
  178. package/src/specs/index.web.ts +1 -1
  179. package/src/types/stylesheet.ts +1 -1
  180. package/{web → src/web}/convert/breakpoint.ts +1 -1
  181. package/{web → src/web}/convert/index.ts +1 -1
  182. package/{web → src/web}/convert/types.ts +1 -1
  183. package/{web → src/web}/create.ts +36 -33
  184. package/{web → src/web}/listener/listener.ts +1 -1
  185. package/{web → src/web}/mock.ts +2 -2
  186. package/{web → src/web}/registry.ts +1 -1
  187. package/{web → src/web}/runtime.ts +4 -4
  188. package/{web → src/web}/state.ts +5 -5
  189. package/{web → src/web}/utils.ts +5 -5
  190. package/src/web/variants/getVariants.ts +42 -0
  191. package/src/web/variants/index.ts +2 -0
  192. package/{web → src/web/variants}/useVariants.ts +24 -44
  193. package/lib/typescript/web/convert/boxShadow.d.ts.map +0 -1
  194. package/lib/typescript/web/convert/breakpoint.d.ts.map +0 -1
  195. package/lib/typescript/web/convert/index.d.ts.map +0 -1
  196. package/lib/typescript/web/convert/shadow.d.ts.map +0 -1
  197. package/lib/typescript/web/convert/style.d.ts.map +0 -1
  198. package/lib/typescript/web/convert/textShadow.d.ts.map +0 -1
  199. package/lib/typescript/web/convert/transform.d.ts.map +0 -1
  200. package/lib/typescript/web/convert/types.d.ts.map +0 -1
  201. package/lib/typescript/web/convert/utils.d.ts.map +0 -1
  202. package/lib/typescript/web/listener/index.d.ts.map +0 -1
  203. package/lib/typescript/web/listener/listener.d.ts.map +0 -1
  204. package/lib/typescript/web/mock.d.ts.map +0 -1
  205. package/lib/typescript/web/mq.d.ts.map +0 -1
  206. package/lib/typescript/web/pseudo.d.ts.map +0 -1
  207. package/lib/typescript/web/registry.d.ts.map +0 -1
  208. package/lib/typescript/web/runtime.d.ts.map +0 -1
  209. package/lib/typescript/web/state.d.ts.map +0 -1
  210. package/lib/typescript/web/useVariants.d.ts +0 -3
  211. package/lib/typescript/web/useVariants.d.ts.map +0 -1
  212. package/lib/typescript/web/utils.d.ts.map +0 -1
  213. /package/lib/typescript/{web → src/web}/convert/boxShadow.d.ts +0 -0
  214. /package/lib/typescript/{web → src/web}/convert/breakpoint.d.ts +0 -0
  215. /package/lib/typescript/{web → src/web}/convert/shadow.d.ts +0 -0
  216. /package/lib/typescript/{web → src/web}/convert/style.d.ts +0 -0
  217. /package/lib/typescript/{web → src/web}/convert/textShadow.d.ts +0 -0
  218. /package/lib/typescript/{web → src/web}/convert/transform.d.ts +0 -0
  219. /package/lib/typescript/{web → src/web}/convert/utils.d.ts +0 -0
  220. /package/lib/typescript/{web → src/web}/listener/index.d.ts +0 -0
  221. /package/lib/typescript/{web → src/web}/mq.d.ts +0 -0
  222. /package/lib/typescript/{web → src/web}/pseudo.d.ts +0 -0
  223. /package/{web → src/web}/convert/boxShadow.ts +0 -0
  224. /package/{web → src/web}/convert/module.d.ts +0 -0
  225. /package/{web → src/web}/convert/shadow.ts +0 -0
  226. /package/{web → src/web}/convert/style.ts +0 -0
  227. /package/{web → src/web}/convert/textShadow.ts +0 -0
  228. /package/{web → src/web}/convert/transform.ts +0 -0
  229. /package/{web → src/web}/convert/utils.ts +0 -0
  230. /package/{web → src/web}/index.ts +0 -0
  231. /package/{web → src/web}/listener/index.ts +0 -0
  232. /package/{web → src/web}/mq.ts +0 -0
  233. /package/{web → src/web}/pseudo.ts +0 -0
@@ -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
  }
@@ -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 = parser.variantsToPairs(rt, arguments[0].asObject(rt));
45
+ auto pairs = helpers::variantsToPairs(rt, arguments[0].asObject(rt));
46
46
 
47
- if (pairs == this->_styleSheet->variants) {
47
+ if (pairs == this->_variants) {
48
48
  return jsi::Value::undefined();
49
49
  }
50
50
 
51
- this->_styleSheet->variants = pairs;
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
  });
@@ -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
  }
@@ -26,7 +26,6 @@ struct StyleSheet {
26
26
  StyleSheetType type;
27
27
  jsi::Object rawValue;
28
28
  std::unordered_map<std::string, Unistyle::Shared> unistyles{};
29
- std::vector<std::pair<std::string, std::string>> variants{};
30
29
  };
31
30
 
32
31
  }
@@ -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, ++tag, rawStyleSheet.asFunction(rt));
9
+ return this->addFromFunction(rt, unid, rawStyleSheet.asFunction(rt));
12
10
  }
13
11
 
14
- return this->addFromObject(rt, ++tag, std::move(rawStyleSheet));
12
+ return this->addFromObject(rt, unid, std::move(rawStyleSheet));
15
13
  }
16
14
 
17
- std::shared_ptr<StyleSheet> StyleSheetRegistry::addFromFunction(jsi::Runtime& rt, unsigned int tag, jsi::Function styleSheetFn) {
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(tag, core::StyleSheetType::Static, std::move(staticStyleSheet));
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(tag, core::StyleSheetType::Themable, std::move(styleSheetFn));
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(tag, core::StyleSheetType::ThemableWithMiniRuntime, std::move(styleSheetFn));
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
- void StyleSheetRegistry::removeStyleSheetByTag(unsigned int tag) {
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.removeStyleSheet(tag);
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, unsigned int tag, jsi::Function styleSheetFn);
24
- virtual std::shared_ptr<StyleSheet> addFromObject(jsi::Runtime& rt, unsigned int tag, jsi::Object rawStyleSheet);
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(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
+ ) {
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::find(unistylesVec.begin(), unistylesVec.end(), unistyle);
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 tag, core::StyleSheetType type, jsi::Object&& rawValue) {
97
- this->_styleSheetRegistry.emplace_back(std::make_shared<core::StyleSheet>(tag, type, std::move(rawValue)));
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.back();
100
+ return this->_styleSheetRegistry[&rt][unid];
100
101
  }
101
102
 
102
- void core::UnistylesRegistry::removeStyleSheet(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
- 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].push_back(shadowUnistyle);
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
- // make sure to use base Unistyle class
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<core::Unistyle::Shared>>
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
- void removeStyleSheet(int tag);
43
- DependencyMap buildDependencyMap(std::vector<UnistyleDependency>& deps);
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::vector<std::shared_ptr<core::StyleSheet>> _styleSheetRegistry{};
51
- std::unordered_map<const ShadowNodeFamily*, std::vector<core::Unistyle::Shared>> _shadowRegistry{};
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 == 2, "Unistyles: Invalid babel transform 'ShadowRegistry link' expected two arguments.");
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 two arguments.");
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 &rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) {
17
- helpers::assertThat(rt, count == 1, "StyleSheet.create expected to be called with one argument.");
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;