Skip to content

Commit 2fd85c1

Browse files
committed
fix: add fixes for VariableContext
1 parent ac60459 commit 2fd85c1

5 files changed

Lines changed: 48 additions & 25 deletions

File tree

cpp/HybridStyleRegistry.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,30 @@ namespace margelo::nitro::cssnitro {
7171
});
7272
}
7373

74-
void HybridStyleRegistry::setRootVariable(const std::string &name,
75-
const std::vector<HybridRootVariableRule> &value) {
76-
// Call setTopLevelVariable with key="root"
77-
VariableContext::setTopLevelVariable("root", name, value);
74+
void HybridStyleRegistry::setRootVariables(const std::shared_ptr<AnyMap> &variables) {
75+
VariableContext::createContext("root", "root");
76+
77+
// Loop over all entries in the AnyMap
78+
for (const auto &entry: variables->getMap()) {
79+
const std::string &key = entry.first;
80+
const AnyValue &value = entry.second;
81+
82+
// Call setTopLevelVariable with key="root"
83+
VariableContext::setTopLevelVariable("root", key, value);
84+
}
85+
}
86+
87+
void HybridStyleRegistry::setUniversalVariables(const std::shared_ptr<AnyMap> &variables) {
88+
VariableContext::createContext("universal", "root");
89+
90+
// Loop over all entries in the AnyMap
91+
for (const auto &entry: variables->getMap()) {
92+
const std::string &key = entry.first;
93+
const AnyValue &value = entry.second;
94+
95+
// Call setTopLevelVariable with key="universal"
96+
VariableContext::setTopLevelVariable("universal", key, value);
97+
}
7898
}
7999

80100
Declarations HybridStyleRegistry::getDeclarations(const std::string &componentId,

cpp/HybridStyleRegistry.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ namespace margelo::nitro::cssnitro {
4343

4444
void addStyleSheet(const HybridStyleSheet &stylesheet) override;
4545

46-
void setRootVariable(const std::string &name,
47-
const std::vector<HybridRootVariableRule> &value) override;
46+
void setRootVariables(const std::shared_ptr<AnyMap> &variables) override;
47+
48+
void setUniversalVariables(const std::shared_ptr<AnyMap> &variables) override;
4849

4950
Declarations getDeclarations(const std::string &componentId, const std::string &classNames,
5051
const std::string &variableScope,

cpp/VariableContext.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ namespace margelo::nitro::cssnitro {
88
std::unordered_map<std::string, VariableContext::Context> VariableContext::contexts;
99

1010
void VariableContext::createContext(const std::string &key, const std::string &parent) {
11+
// Check if context already exists
12+
if (contexts.find(key) != contexts.end()) {
13+
// Context already exists, don't overwrite it
14+
return;
15+
}
16+
1117
// Create a new context with the specified parent
1218
Context ctx;
1319
ctx.parent = parent;
@@ -45,6 +51,13 @@ namespace margelo::nitro::cssnitro {
4551
if (!std::holds_alternative<std::monostate>(result)) {
4652
return result;
4753
}
54+
} else {
55+
// Variable doesn't exist in this context, create a new Observable with nullptr
56+
auto observable = reactnativecss::Observable<AnyValue>::create(AnyValue());
57+
valueMap[name] = observable;
58+
59+
// Subscribe to the observable by calling get()
60+
get(*observable);
4861
}
4962
}
5063
return std::nullopt;
@@ -159,13 +172,13 @@ namespace margelo::nitro::cssnitro {
159172
}
160173

161174
void VariableContext::setTopLevelVariable(const std::string &key, const std::string &name,
162-
const std::vector<HybridRootVariableRule> &value) {
163-
// Create a new Computed that returns "yellow" for now
175+
const AnyValue &value) {
176+
// Create a new Computed that returns the provided value
164177
auto computed = reactnativecss::Computed<AnyValue>::create(
165-
[](const AnyValue &prev, reactnativecss::Effect::GetProxy &get) -> AnyValue {
178+
[value](const AnyValue &prev, reactnativecss::Effect::GetProxy &get) -> AnyValue {
166179
(void) prev;
167180
(void) get;
168-
return AnyValue("yellow");
181+
return value;
169182
},
170183
AnyValue() // Initial value
171184
);

cpp/VariableContext.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ namespace margelo::nitro::cssnitro {
1414

1515
using AnyValue = ::margelo::nitro::AnyValue;
1616

17-
// Forward declaration
18-
struct HybridRootVariableRule;
19-
2017
class VariableContext {
2118
public:
2219
// Value can be either Observable or Computed
@@ -52,9 +49,9 @@ namespace margelo::nitro::cssnitro {
5249
static void setVariable(const std::string &key, const std::string &name,
5350
std::shared_ptr<reactnativecss::Computed<AnyValue>> computed);
5451

55-
// Set a top-level variable (creates a Computed from HybridRootVariableRule)
52+
// Set a top-level variable (creates a Computed from AnyValue)
5653
static void setTopLevelVariable(const std::string &key, const std::string &name,
57-
const std::vector<HybridRootVariableRule> &value);
54+
const AnyValue &value);
5855

5956
private:
6057
VariableContext() = delete; // Static-only class

src/specs/StyleRegistry/HybridStyleRegistry.nitro.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export type HybridStyleRegistry = StyleRegistry & RawStyleRegistry;
77
export interface StyleRegistry
88
extends HybridObject<{ ios: "c++"; android: "c++" }> {
99
setClassname(classname: string, styleRule: HybridStyleRule[]): void;
10-
setRootVariable(name: string, value: HybridRootVariableRule[]): void;
10+
setRootVariables(variables: AnyMap): void;
11+
setUniversalVariables(variables: AnyMap): void;
1112
addStyleSheet(stylesheet: HybridStyleSheet): void;
1213
getDeclarations(
1314
componentId: string,
@@ -131,12 +132,3 @@ interface HybridStyleRule {
131132
}
132133

133134
type SpecificityArray = number[];
134-
135-
/****************************** Variables *******************************/
136-
137-
interface HybridRootVariableRule {
138-
d: AnyMap;
139-
140-
/** MediaQuery */
141-
m?: AnyMap;
142-
}

0 commit comments

Comments
 (0)