Skip to content

Commit 329ff54

Browse files
committed
feat: add VariableContext
1 parent f25aaa3 commit 329ff54

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

cpp/VariableContext.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

cpp/VariableContext.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <unordered_map>
5+
#include <memory>
6+
#include <NitroModules/AnyValue.hpp>
7+
#include "Observable.hpp"
8+
#include "Effect.hpp"
9+
10+
namespace margelo::nitro::cssnitro {
11+
12+
using AnyValue = ::margelo::nitro::AnyValue;
13+
14+
class VariableContext {
15+
public:
16+
// Static map: context key -> (variable name -> Observable<AnyValue>)
17+
static std::unordered_map<std::string, std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<AnyValue>>>> contexts;
18+
19+
// Create a new context with the given key
20+
static void createContext(const std::string &key);
21+
22+
// Delete a context by key
23+
static void deleteContext(const std::string &key);
24+
25+
// Get a variable from a context, subscribing the effect to changes
26+
static const AnyValue &getVariable(const std::string &key, const std::string &name,
27+
reactnativecss::Effect::GetProxy &get);
28+
29+
// Set a variable in a context
30+
static void
31+
setVariable(const std::string &key, const std::string &name, const AnyValue &value);
32+
33+
// Set a variable in a context using an existing Observable
34+
static void setVariable(const std::string &key, const std::string &name,
35+
std::shared_ptr<reactnativecss::Observable<AnyValue>> observable);
36+
37+
private:
38+
VariableContext() = delete; // Static-only class
39+
};
40+
41+
} // namespace margelo::nitro::cssnitro

0 commit comments

Comments
 (0)