Skip to content

Commit

Permalink
perf: Sort view props alphabetically because fabric is implemented th…
Browse files Browse the repository at this point in the history
…at way
  • Loading branch information
mrousavy committed Jan 16, 2025
1 parent 8fa40b2 commit 53ec8b4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
12 changes: 11 additions & 1 deletion packages/nitrogen/src/views/ViewComponentShadowNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,18 @@ export function getViewComponentNames(
}

export function createViewComponentShadowNodeFiles(
spec: HybridObjectSpec
specOriginal: HybridObjectSpec
): SourceFile[] {
// Due to internal Fabric details, props must be alphabetically sorted.
const sortedProps = [...specOriginal.properties].sort((a, b) => {
if (a.name < b.name) return -1
else if (a.name > b.name) return 1
else return 0
})
const spec: HybridObjectSpec = {
...specOriginal,
properties: sortedProps,
}
if (!spec.isHybridView) {
throw new Error(
`Cannot create View Component ShadowNode code for ${spec.name} - it's not a HybridView!`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ namespace margelo::nitro::image::views {
const HybridTestViewProps& sourceProps,
const react::RawProps& rawProps):
react::ViewProps(context, sourceProps, rawProps, filterObjectKeys),
someProp([&]() -> CachedProp<bool> {
const react::RawValue* rawValue = rawProps.at("someProp", nullptr, nullptr);
if (rawValue == nullptr) return {};
const auto& [runtime, value] = (std::pair<jsi::Runtime*, const jsi::Value&>)*rawValue;
return CachedProp<bool>::fromRawValue(*runtime, value, sourceProps.someProp);
}()),
someCallback([&]() -> CachedProp<std::function<void(double /* someParam */)>> {
const react::RawValue* rawValue = rawProps.at("someCallback", nullptr, nullptr);
if (rawValue == nullptr) return {};
const auto& [runtime, value] = (std::pair<jsi::Runtime*, const jsi::Value&>)*rawValue;
return CachedProp<std::function<void(double /* someParam */)>>::fromRawValue(*runtime, value, sourceProps.someCallback);
}()),
someProp([&]() -> CachedProp<bool> {
const react::RawValue* rawValue = rawProps.at("someProp", nullptr, nullptr);
if (rawValue == nullptr) return {};
const auto& [runtime, value] = (std::pair<jsi::Runtime*, const jsi::Value&>)*rawValue;
return CachedProp<bool>::fromRawValue(*runtime, value, sourceProps.someProp);
}()) { }

HybridTestViewProps::HybridTestViewProps(const HybridTestViewProps& other):
react::ViewProps(),
someProp(other.someProp),
someCallback(other.someCallback) { }
someCallback(other.someCallback),
someProp(other.someProp) { }

bool HybridTestViewProps::filterObjectKeys(const std::string& propName) {
switch (hashString(propName)) {
case hashString("someProp"): return true;
case hashString("someCallback"): return true;
case hashString("someProp"): return true;
default: return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ namespace margelo::nitro::image::views {
const react::RawProps& rawProps);

public:
CachedProp<bool> someProp;
CachedProp<std::function<void(double /* someParam */)>> someCallback;
CachedProp<bool> someProp;

private:
static bool filterObjectKeys(const std::string& propName);
Expand Down

0 comments on commit 53ec8b4

Please sign in to comment.