@@ -6,6 +6,8 @@ namespace margelo::nitro::cssnitro {
66
77 // Initialize the static contexts map
88 std::unordered_map<std::string, VariableContext::Context> VariableContext::contexts;
9+ std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<AnyValue>>> VariableContext::root_values;
10+ std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<AnyValue>>> VariableContext::universal_values;
911
1012 void VariableContext::createContext (const std::string &key, const std::string &parent) {
1113 // Check if context already exists
@@ -173,18 +175,46 @@ namespace margelo::nitro::cssnitro {
173175
174176 void VariableContext::setTopLevelVariable (const std::string &key, const std::string &name,
175177 const AnyValue &value) {
176- // Create a new Computed that returns the provided value
177- auto computed = reactnativecss::Computed<AnyValue>::create (
178- [value](const AnyValue &prev, reactnativecss::Effect::GetProxy &get) -> AnyValue {
179- (void ) prev;
180- (void ) get;
181- return value;
182- },
183- AnyValue () // Initial value
184- );
185-
186- // Use the Computed overload to set the variable
187- setVariable (key, name, computed);
178+ // Determine which map to use based on the key
179+ auto &targetMap = (key == " root" ) ? root_values : universal_values;
180+
181+ // Find or create the observable in the target map
182+ auto observableIt = targetMap.find (name);
183+ if (observableIt != targetMap.end ()) {
184+ // Observable already exists, just update its value
185+ observableIt->second ->set (value);
186+ } else {
187+ // Create a new Observable with the value
188+ auto observable = reactnativecss::Observable<AnyValue>::create (value);
189+ targetMap[name] = observable;
190+ }
191+
192+ // Check if the name has been set for the key context
193+ auto contextIt = contexts.find (key);
194+ if (contextIt != contexts.end ()) {
195+ auto &valueMap = contextIt->second .values ;
196+ auto varIt = valueMap.find (name);
197+
198+ if (varIt == valueMap.end ()) {
199+ // Variable doesn't exist in context, create a Computed that reads from the target map
200+ auto computed = reactnativecss::Computed<AnyValue>::create (
201+ [&targetMap, name](const AnyValue &prev,
202+ reactnativecss::Effect::GetProxy &get) -> AnyValue {
203+ (void ) prev;
204+
205+ // Read from the target map
206+ auto it = targetMap.find (name);
207+ if (it != targetMap.end ()) {
208+ return get (*it->second );
209+ }
210+ return AnyValue ();
211+ },
212+ AnyValue () // Initial value
213+ );
214+
215+ valueMap[name] = computed;
216+ }
217+ }
188218 }
189219
190220} // namespace margelo::nitro::cssnitro
0 commit comments