Skip to content

Commit 2b37b6b

Browse files
committed
feat: shadowtree updates
1 parent 179515c commit 2b37b6b

11 files changed

Lines changed: 249 additions & 125 deletions

cpp/AnyToDynamic.cpp

Lines changed: 0 additions & 71 deletions
This file was deleted.

cpp/AnyToDynamic.hpp

Lines changed: 0 additions & 19 deletions
This file was deleted.

cpp/Helpers.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <jsi/jsi.h>
4+
#include <jsi/JSIDynamic.h>
5+
#include <folly/dynamic.h>
6+
#include <unordered_set>
7+
8+
using namespace facebook;
9+
10+
namespace margelo::nitro::cssnitro::helpers {
11+
12+
using Variants = std::vector<std::pair<std::string, std::string>>;
13+
14+
inline void assertThat(jsi::Runtime &rt, bool condition, const std::string &message) {
15+
if (!condition) {
16+
throw jsi::JSError(rt, message);
17+
}
18+
}
19+
}

cpp/HybridStyleRegistry.cpp

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "HybridStyleRegistry.hpp"
22
#include "Computed.hpp"
33
#include "Observable.hpp"
4+
#include "Helpers.hpp"
45
#include "ShadowTreeUpdateManager.hpp"
56
#include "StyledComputedFactory.hpp"
67

@@ -55,6 +56,10 @@ namespace margelo::nitro::cssnitro {
5556

5657
void unlinkComponent(const std::string &componentId);
5758

59+
jsi::Value registerExternalMethods(jsi::Runtime &runtime,
60+
const jsi::Value &thisValue,
61+
const jsi::Value *args, size_t count);
62+
5863
private:
5964
// Per-component link info and update source
6065
struct ComponentLink {
@@ -71,18 +76,13 @@ namespace margelo::nitro::cssnitro {
7176
// Per-runtime effect (kept alive)
7277
std::unordered_map<jsi::Runtime *, std::shared_ptr<reactnativecss::Computed<bool>>> runtime_effects_;
7378

74-
void ensureRuntimeEffect(jsi::Runtime &runtime);
75-
76-
void applyUpdates(jsi::Runtime &runtime, const UpdatesMap &updates);
77-
78-
void addUpdates(const std::string &componentId, const folly::dynamic &payload);
79-
8079
public:
8180
static std::once_flag flag_;
8281
static std::shared_ptr<Impl> inst_;
8382

8483
static std::shared_ptr<Impl> get();
8584

85+
private:
8686
// Manager that owns per-runtime effects and updates batching
8787
std::unique_ptr<ShadowTreeUpdateManager> shadowUpdates_;
8888

@@ -145,13 +145,24 @@ namespace margelo::nitro::cssnitro {
145145
return impl_->linkComponent(runtime, thisValue, args, count);
146146
}
147147

148+
jsi::Value
149+
HybridStyleRegistry::registerExternalMethods(jsi::Runtime &runtime, const jsi::Value &thisValue,
150+
const jsi::Value *args, size_t count) {
151+
return impl_->registerExternalMethods(runtime, thisValue, args, count);
152+
}
153+
148154
void HybridStyleRegistry::loadHybridMethods() {
149155
HybridStyleRegistrySpec::loadHybridMethods();
150156
registerHybrids(this, [](Prototype &prototype) {
151157
prototype.registerRawHybridMethod(
152158
"linkComponent",
153159
2,
154160
&HybridStyleRegistry::linkComponent);
161+
162+
prototype.registerRawHybridMethod(
163+
"registerExternalMethods",
164+
1,
165+
&HybridStyleRegistry::registerExternalMethods);
155166
});
156167
}
157168

@@ -268,7 +279,25 @@ namespace margelo::nitro::cssnitro {
268279
shadowUpdates_->unlinkComponent(componentId);
269280
}
270281

271-
// Update application is handled by ShadowTreeUpdateManager.
282+
jsi::Value HybridStyleRegistry::Impl::registerExternalMethods(
283+
jsi::Runtime &runtime, const jsi::Value &thisValue, const jsi::Value *args,
284+
size_t count) {
285+
(void) thisValue;
286+
287+
if (!args[0].isObject()) {
288+
return jsi::Value::undefined();
289+
}
290+
291+
/** processColor **/
292+
auto maybeProcessColorFn = args[0].asObject(runtime).getProperty(runtime, "processColor");
293+
helpers::assertThat(runtime, maybeProcessColorFn.isObject(),
294+
"react-native-css: Can't load processColor function from JS.");
295+
jsi::Function processColorFn = maybeProcessColorFn.asObject(runtime).asFunction(runtime);
296+
297+
shadowUpdates_->registerProcessColorFunction(std::move(processColorFn));
298+
299+
return jsi::Value::undefined();
300+
}
272301

273302
void HybridStyleRegistry::updateComponentInlineStyleKeys(
274303
const std::string &componentId,

cpp/HybridStyleRegistry.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ namespace margelo::nitro::cssnitro {
3434
using PropValue = std::variant<std::string, double, bool>;
3535
using PropPath = std::vector<std::string>;
3636
using SelectorAndArgs = std::tuple<std::string, std::vector<std::string>>;
37-
using ConfigTuple = std::tuple<std::string, std::vector<std::string>, std::vector<SelectorAndArgs>>;
38-
using TestPropFn = std::function<bool(const PropPath &, const PropValue &)>;
3937

4038
// ----- public API forwarded to Impl -----
4139
void set(const std::string &className,
@@ -70,6 +68,10 @@ namespace margelo::nitro::cssnitro {
7068
jsi::Value linkComponent(jsi::Runtime &runtime,
7169
const jsi::Value &thisValue,
7270
const jsi::Value *args, size_t count);
71+
72+
jsi::Value registerExternalMethods(jsi::Runtime &runtime,
73+
const jsi::Value &thisValue,
74+
const jsi::Value *args, size_t count);
7375
};
7476

7577
} // namespace margelo::nitro::cssnitro

0 commit comments

Comments
 (0)