|
| 1 | +#include "VariableContext.hpp" |
| 2 | + |
| 3 | +namespace margelo::nitro::cssnitro { |
| 4 | + |
| 5 | + // Initialize the static contexts map |
| 6 | + std::unordered_map<std::string, std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<AnyValue>>>> VariableContext::contexts; |
| 7 | + |
| 8 | + void VariableContext::createContext(const std::string &key) { |
| 9 | + // Create a new empty map for this context |
| 10 | + contexts[key] = std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<AnyValue>>>(); |
| 11 | + } |
| 12 | + |
| 13 | + void VariableContext::deleteContext(const std::string &key) { |
| 14 | + // Remove the context from the map |
| 15 | + contexts.erase(key); |
| 16 | + } |
| 17 | + |
| 18 | + const AnyValue &VariableContext::getVariable(const std::string &key, const std::string &name, |
| 19 | + reactnativecss::Effect::GetProxy &get) { |
| 20 | + // Find the context |
| 21 | + auto contextIt = contexts.find(key); |
| 22 | + if (contextIt == contexts.end()) { |
| 23 | + // Context doesn't exist, throw or return a default value |
| 24 | + static AnyValue defaultValue; |
| 25 | + return defaultValue; |
| 26 | + } |
| 27 | + |
| 28 | + // Find the variable in the context |
| 29 | + auto &variableMap = contextIt->second; |
| 30 | + auto varIt = variableMap.find(name); |
| 31 | + if (varIt == variableMap.end()) { |
| 32 | + // Variable doesn't exist, return a default value |
| 33 | + static AnyValue defaultValue; |
| 34 | + return defaultValue; |
| 35 | + } |
| 36 | + |
| 37 | + // Get the observable and subscribe the effect to it |
| 38 | + auto &observable = varIt->second; |
| 39 | + return get(*observable); |
| 40 | + } |
| 41 | + |
| 42 | + void VariableContext::setVariable(const std::string &key, const std::string &name, |
| 43 | + const AnyValue &value) { |
| 44 | + // Find or create the context |
| 45 | + auto contextIt = contexts.find(key); |
| 46 | + if (contextIt == contexts.end()) { |
| 47 | + // Context doesn't exist, create it |
| 48 | + createContext(key); |
| 49 | + contextIt = contexts.find(key); |
| 50 | + } |
| 51 | + |
| 52 | + auto &variableMap = contextIt->second; |
| 53 | + |
| 54 | + // Find or create the observable for this variable |
| 55 | + auto varIt = variableMap.find(name); |
| 56 | + if (varIt == variableMap.end()) { |
| 57 | + // Variable doesn't exist, create a new Observable with the value |
| 58 | + auto observable = reactnativecss::Observable<AnyValue>::create(value); |
| 59 | + variableMap[name] = observable; |
| 60 | + } else { |
| 61 | + // Variable exists, update its value |
| 62 | + varIt->second->set(value); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + void VariableContext::setVariable(const std::string &key, const std::string &name, |
| 67 | + std::shared_ptr<reactnativecss::Observable<AnyValue>> observable) { |
| 68 | + // Find or create the context |
| 69 | + auto contextIt = contexts.find(key); |
| 70 | + if (contextIt == contexts.end()) { |
| 71 | + // Context doesn't exist, create it |
| 72 | + createContext(key); |
| 73 | + contextIt = contexts.find(key); |
| 74 | + } |
| 75 | + |
| 76 | + auto &variableMap = contextIt->second; |
| 77 | + |
| 78 | + // Set the variable to use the provided Observable directly |
| 79 | + variableMap[name] = observable; |
| 80 | + } |
| 81 | + |
| 82 | +} // namespace margelo::nitro::cssnitro |
0 commit comments