Skip to content

Commit

Permalink
feat: resolve unistyle not bound
Browse files Browse the repository at this point in the history
  • Loading branch information
jpudysz committed Dec 21, 2024
1 parent fcf2354 commit f8a137e
Show file tree
Hide file tree
Showing 33 changed files with 264 additions and 517 deletions.
3 changes: 2 additions & 1 deletion components/native/ActivityIndicator/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/ActivityIndicator.js",
"module": "../../../lib/module/components/native/ActivityIndicator.js"
"module": "../../../lib/module/components/native/ActivityIndicator.js",
"react-native": "../../../src/components/native/ActivityIndicator.tsx"
}
3 changes: 2 additions & 1 deletion components/native/FlatList/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/FlatList.js",
"module": "../../../lib/module/components/native/FlatList.js"
"module": "../../../lib/module/components/native/FlatList.js",
"react-native": "../../../src/components/native/FlatList.tsx"
}
3 changes: 2 additions & 1 deletion components/native/Image/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/Image.js",
"module": "../../../lib/module/components/native/Image.js"
"module": "../../../lib/module/components/native/Image.js",
"react-native": "../../../src/components/native/Image.tsx"
}
3 changes: 2 additions & 1 deletion components/native/ImageBackground/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/ImageBackground.js",
"module": "../../../lib/module/components/native/ImageBackground.js"
"module": "../../../lib/module/components/native/ImageBackground.js",
"react-native": "../../../src/components/native/ImageBackground.native.tsx"
}
3 changes: 2 additions & 1 deletion components/native/KeyboardAvoidingView/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/KeyboardAvoidingView.js",
"module": "../../../lib/module/components/native/KeyboardAvoidingView.js"
"module": "../../../lib/module/components/native/KeyboardAvoidingView.js",
"react-native": "../../../src/components/native/KeyboardAvoidingView.tsx"
}
2 changes: 1 addition & 1 deletion components/native/Pressable/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/Pressable.js",
"module": "../../../lib/module/components/native/Pressable.js",
"react-native": "../../../lib/commonjs/components/native/Pressable.native.js"
"react-native": "../../../src/components/native/Pressable.native.tsx"
}
3 changes: 2 additions & 1 deletion components/native/RefreshControl/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/RefreshControl.js",
"module": "../../../lib/module/components/native/RefreshControl.js"
"module": "../../../lib/module/components/native/RefreshControl.js",
"react-native": "../../../src/components/native/RefreshControl.tsx"
}
3 changes: 2 additions & 1 deletion components/native/ScrollView/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/ScrollView.js",
"module": "../../../lib/module/components/native/ScrollView.js"
"module": "../../../lib/module/components/native/ScrollView.js",
"react-native": "../../../src/components/native/ScrollView.tsx"
}
3 changes: 2 additions & 1 deletion components/native/SectionList/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/SectionList.js",
"module": "../../../lib/module/components/native/SectionList.js"
"module": "../../../lib/module/components/native/SectionList.js",
"react-native": "../../../src/components/native/SectionList.tsx"
}
3 changes: 2 additions & 1 deletion components/native/Switch/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/Switch.js",
"module": "../../../lib/module/components/native/Switch.js"
"module": "../../../lib/module/components/native/Switch.js",
"react-native": "../../../src/components/native/Switch.tsx"
}
3 changes: 2 additions & 1 deletion components/native/Text/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/Text.js",
"module": "../../../lib/module/components/native/Text.js"
"module": "../../../lib/module/components/native/Text.js",
"react-native": "../../../src/components/native/Text.tsx"
}
3 changes: 2 additions & 1 deletion components/native/TextInput/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/TextInput.js",
"module": "../../../lib/module/components/native/TextInput.js"
"module": "../../../lib/module/components/native/TextInput.js",
"react-native": "../../../src/components/native/TextInput.tsx"
}
3 changes: 2 additions & 1 deletion components/native/TouchableHighlight/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/TouchableHighlight.js",
"module": "../../../lib/module/components/native/TouchableHighlight.js"
"module": "../../../lib/module/components/native/TouchableHighlight.js",
"react-native": "../../../src/components/native/TouchableHighlight.tsx"
}
3 changes: 2 additions & 1 deletion components/native/TouchableOpacity/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/TouchableOpacity.js",
"module": "../../../lib/module/components/native/TouchableOpacity.js"
"module": "../../../lib/module/components/native/TouchableOpacity.js",
"react-native": "../../../src/components/native/TouchableOpacity.tsx"
}
3 changes: 2 additions & 1 deletion components/native/View/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/View.js",
"module": "../../../lib/module/components/native/View.js"
"module": "../../../lib/module/components/native/View.js",
"react-native": "../../../src/components/native/View.tsx"
}
3 changes: 2 additions & 1 deletion components/native/VirtualizedList/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../../../lib/commonjs/components/native/VirtualizedList.js",
"module": "../../../lib/module/components/native/VirtualizedList.js"
"module": "../../../lib/module/components/native/VirtualizedList.js",
"react-native": "../../../src/components/native/VirtualizedList.tsx"
}
8 changes: 5 additions & 3 deletions cxx/common/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace margelo::nitro::unistyles::helpers {

static const std::string UNISTYLES_ID = "__unid";
static const std::string STYLESHEET_ID = "__stylesheetID";
static const std::string UNISTYLE_ID = "__unistyleID";
static const std::string ADD_VARIANTS_FN = "useVariants";
static const std::string STYLE_DEPENDENCIES = "uni__dependencies";
static const std::string STYLE_VARIANTS = "uni__variants";
static const std::string STYLESHEET_VARIANTS = "__stylesheetVariants";
static const std::string WEB_STYLE_KEY = "_web";
static const std::string EXOTIC_STYLE_KEY = "_exotic";
static const std::string NAME_STYLE_KEY = "__unistyles_name";

// todo verify it
static const std::string SECRETS = "__uni__secrets";
static const std::string ARGUMENTS = "__uni__args";

Expand Down
16 changes: 16 additions & 0 deletions cxx/core/IDGenerator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <atomic>

namespace margelo::nitro::unistyles::helpers {

struct IDGenerator {
IDGenerator(unsigned int start = 1): _currentID(start) {}

int next() {
return _currentID.fetch_add(1, std::memory_order_relaxed);
}

private:
std::atomic<unsigned int> _currentID;
};

}
50 changes: 40 additions & 10 deletions cxx/core/RNStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,68 @@

#include <jsi/jsi.h>
#include "Helpers.h"
#include "Parser.h"
#include "UnistyleWrapper.h"

namespace margelo::nitro::unistyles::core {

jsi::Object toRNStyle(jsi::Runtime& rt, std::shared_ptr<StyleSheet> styleSheet, std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime, Variants&& variants);

jsi::Function createAddVariantsProxyFunction(jsi::Runtime& rt, std::shared_ptr<StyleSheet> stylesheet, std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime) {
auto useVariantsFnName = jsi::PropNameID::forUtf8(rt, helpers::ADD_VARIANTS_FN);
auto parser = parser::Parser(unistylesRuntime);

return jsi::Function::createFromHostFunction(rt, useVariantsFnName, 1, [stylesheet, unistylesRuntime](jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count){
return jsi::Function::createFromHostFunction(rt, useVariantsFnName, 1, [stylesheet, unistylesRuntime, &parser](jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count){
helpers::assertThat(rt, count == 1, "Unistyles: useVariants expected to be called with one argument.");
helpers::assertThat(rt, arguments[0].isObject(), "Unistyles: useVariants expected to be called with object.");

// using variants == cloning stylesheet
return toRNStyle(rt, stylesheet, unistylesRuntime, helpers::variantsToPairs(rt, arguments[0].asObject(rt)));
jsi::Object rnStyle = jsi::Object(rt);
Variants variants = helpers::variantsToPairs(rt, arguments[0].asObject(rt));

// copy hidden properties as they are not enumerable
helpers::defineHiddenProperty(rt, rnStyle, helpers::STYLESHEET_ID.c_str(), jsi::Value(stylesheet->tag));
helpers::defineHiddenProperty(rt, rnStyle, helpers::STYLESHEET_VARIANTS.c_str(), arguments[0].asObject(rt));

// copy unaffected styles, or compute new
helpers::enumerateJSIObject(rt, thisVal.asObject(rt), [&, stylesheet](const std::string& name, jsi::Value& value){
if (name == helpers::ADD_VARIANTS_FN) {
rnStyle.setProperty(rt, helpers::ADD_VARIANTS_FN.c_str(), std::move(value));

return;
}

if (!stylesheet->unistyles.contains(name)) {
return;
}

auto unistyle = stylesheet->unistyles[name];

if (!unistyle->dependsOn(UnistyleDependency::VARIANTS)) {
rnStyle.setProperty(rt, name.c_str(), std::move(value));

return;
}

// compute new value
parser.rebuildUnistyle(rt, stylesheet, unistyle, variants, std::nullopt);
rnStyle.setProperty(rt, name.c_str(), valueFromUnistyle(rt, unistylesRuntime, unistyle, variants));
});

return rnStyle;
});
}

jsi::Object toRNStyle(jsi::Runtime& rt, std::shared_ptr<StyleSheet> stylesheet, std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime, Variants&& variants) {
jsi::Object rnStyle = jsi::Object(rt);

helpers::defineHiddenProperty(rt, rnStyle, helpers::UNISTYLES_ID.c_str(), jsi::Value(stylesheet->tag));
helpers::defineHiddenProperty(rt, rnStyle, helpers::STYLE_VARIANTS.c_str(), helpers::variantsToValue(rt, variants));


helpers::defineHiddenProperty(rt, rnStyle, helpers::STYLESHEET_ID.c_str(), jsi::Value(stylesheet->tag));
helpers::defineHiddenProperty(rt, rnStyle, helpers::STYLESHEET_VARIANTS.c_str(), helpers::variantsToValue(rt, variants));
rnStyle.setProperty(rt, helpers::ADD_VARIANTS_FN.c_str(), createAddVariantsProxyFunction(rt, stylesheet, unistylesRuntime));

for (auto& pair: stylesheet->unistyles) {
auto [propertyName, unistyle] = pair;

rnStyle.setProperty(rt, propertyName.c_str(), valueFromUnistyle(rt, unistylesRuntime, unistyle, variants));
}

return rnStyle;
}

Expand Down
9 changes: 5 additions & 4 deletions cxx/core/Unistyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ enum class UnistyleType {
struct Unistyle {
using Shared = std::shared_ptr<Unistyle>;

Unistyle(UnistyleType type, std::string styleKey, jsi::Object& rawObject, std::shared_ptr<StyleSheet> styleSheet)
: styleKey{styleKey}, type{type}, rawValue{std::move(rawObject)}, parent{styleSheet} {}
Unistyle(unsigned int unid, UnistyleType type, std::string styleKey, jsi::Object& rawObject, std::shared_ptr<StyleSheet> styleSheet)
: unid{unid}, styleKey{styleKey}, type{type}, rawValue{std::move(rawObject)}, parent{styleSheet} {}
virtual ~Unistyle() = default;

Unistyle(const Unistyle&) = delete;
Unistyle(Unistyle&& other) = delete;

UnistyleType type;
std::string styleKey;
unsigned int unid;
jsi::Object rawValue;
std::optional<jsi::Object> parsedStyle;
std::vector<UnistyleDependency> dependencies{};
Expand Down Expand Up @@ -65,8 +66,8 @@ struct UnistyleDynamicFunction: public Unistyle {
// unprocessedValue <- object generated after calling proxy and user's original function
// parsedStyle <- parsed with Unistyle's parser

UnistyleDynamicFunction(UnistyleType type, std::string styleKey, jsi::Object& rawObject, std::shared_ptr<StyleSheet> styleSheet)
: Unistyle(type, styleKey, rawObject, styleSheet) {}
UnistyleDynamicFunction(unsigned int unid, UnistyleType type, std::string styleKey, jsi::Object& rawObject, std::shared_ptr<StyleSheet> styleSheet)
: Unistyle(unid, type, styleKey, rawObject, styleSheet) {}

UnistyleDynamicFunction(const UnistyleDynamicFunction&) = delete;
UnistyleDynamicFunction(UnistyleDynamicFunction&& other) = delete;
Expand Down
Loading

0 comments on commit f8a137e

Please sign in to comment.