|
| 1 | +#include "Animations.hpp" |
| 2 | +#include "StyleResolver.hpp" |
| 3 | +#include <unordered_map> |
| 4 | +#include <mutex> |
| 5 | + |
| 6 | +namespace reactnativecss::animations { |
| 7 | + |
| 8 | + using AnyMap = ::margelo::nitro::AnyMap; |
| 9 | + using AnyValue = ::margelo::nitro::AnyValue; |
| 10 | + using AnyObject = ::margelo::nitro::AnyObject; |
| 11 | + |
| 12 | + // Map to store the shared keyframes observables (one per keyframe name) |
| 13 | + static std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<std::shared_ptr<AnyMap>>>> keyframesObservables; |
| 14 | + |
| 15 | + // Map to store scope-specific computeds: Map<variableScope, Map<name, Computed<AnyMap>>> |
| 16 | + static std::unordered_map<std::string, std::unordered_map<std::string, std::shared_ptr<reactnativecss::Computed<std::shared_ptr<AnyMap>>>>> scopedComputeds; |
| 17 | + |
| 18 | + static std::mutex keyframesMutex; |
| 19 | + |
| 20 | + void setKeyframes(const std::string &name, const std::shared_ptr<AnyMap> &keyframes) { |
| 21 | + std::lock_guard<std::mutex> lock(keyframesMutex); |
| 22 | + |
| 23 | + // Find or create the observable for this keyframe name |
| 24 | + auto it = keyframesObservables.find(name); |
| 25 | + |
| 26 | + if (it != keyframesObservables.end()) { |
| 27 | + // Update existing observable |
| 28 | + it->second->set(keyframes); |
| 29 | + } else { |
| 30 | + // Create new observable |
| 31 | + keyframesObservables[name] = reactnativecss::Observable<std::shared_ptr<AnyMap>>::create( |
| 32 | + keyframes); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + std::shared_ptr<AnyMap> getKeyframes(const std::string &name, const std::string &variableScope, |
| 37 | + reactnativecss::Effect::GetProxy &get) { |
| 38 | + std::lock_guard<std::mutex> lock(keyframesMutex); |
| 39 | + |
| 40 | + // First, ensure the Observable exists for this keyframe name |
| 41 | + auto obsIt = keyframesObservables.find(name); |
| 42 | + if (obsIt == keyframesObservables.end()) { |
| 43 | + // Create a new observable with an empty AnyMap |
| 44 | + keyframesObservables[name] = reactnativecss::Observable<std::shared_ptr<AnyMap>>::create( |
| 45 | + AnyMap::make()); |
| 46 | + obsIt = keyframesObservables.find(name); |
| 47 | + } |
| 48 | + |
| 49 | + // Get the observable for this keyframe name |
| 50 | + auto observable = obsIt->second; |
| 51 | + |
| 52 | + // Now check if a Computed exists within the variableScope for this name |
| 53 | + auto scopeIt = scopedComputeds.find(variableScope); |
| 54 | + if (scopeIt == scopedComputeds.end()) { |
| 55 | + // Create the scope map if it doesn't exist |
| 56 | + scopedComputeds[variableScope] = std::unordered_map<std::string, std::shared_ptr<reactnativecss::Computed<std::shared_ptr<AnyMap>>>>(); |
| 57 | + scopeIt = scopedComputeds.find(variableScope); |
| 58 | + } |
| 59 | + |
| 60 | + auto &scopeMap = scopeIt->second; |
| 61 | + auto computedIt = scopeMap.find(name); |
| 62 | + |
| 63 | + if (computedIt == scopeMap.end()) { |
| 64 | + // Create a new Computed that gets the AnyMap from the observable and processes it |
| 65 | + auto computed = reactnativecss::Computed<std::shared_ptr<AnyMap>>::create( |
| 66 | + [observable, variableScope](const std::shared_ptr<AnyMap> &prev, |
| 67 | + reactnativecss::Effect::GetProxy &get) { |
| 68 | + // Get the raw keyframes from the observable |
| 69 | + auto rawKeyframes = get(*observable); |
| 70 | + |
| 71 | + // Create a new AnyMap to hold the resolved keyframes |
| 72 | + auto resolvedKeyframes = AnyMap::make(rawKeyframes->getMap().size()); |
| 73 | + |
| 74 | + // Loop over the entries of the rawKeyframes |
| 75 | + for (const auto &entry: rawKeyframes->getMap()) { |
| 76 | + const std::string &key = entry.first; |
| 77 | + const AnyValue &value = entry.second; |
| 78 | + |
| 79 | + // Each value should be an AnyObject, if not skip that entry |
| 80 | + if (!std::holds_alternative<AnyObject>(value)) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + const auto &frameMap = std::get<AnyObject>(value); |
| 85 | + |
| 86 | + // Create a temporary map to hold resolved frame values |
| 87 | + std::unordered_map<std::string, AnyValue> resolvedFrameMap; |
| 88 | + resolvedFrameMap.reserve(frameMap.size()); |
| 89 | + |
| 90 | + // Loop over each entry of the frame and resolve values |
| 91 | + for (const auto &frameEntry: frameMap) { |
| 92 | + const std::string &frameKey = frameEntry.first; |
| 93 | + const AnyValue &frameValue = frameEntry.second; |
| 94 | + |
| 95 | + // Resolve the value using StyleResolver |
| 96 | + AnyValue resolvedValue = margelo::nitro::cssnitro::StyleResolver::resolveStyle( |
| 97 | + frameValue, variableScope, get |
| 98 | + ); |
| 99 | + |
| 100 | + resolvedFrameMap[frameKey] = resolvedValue; |
| 101 | + } |
| 102 | + |
| 103 | + // Apply style mapping to the resolved frame |
| 104 | + auto transformedFrame = margelo::nitro::cssnitro::StyleResolver::applyStyleMapping( |
| 105 | + resolvedFrameMap, variableScope, get |
| 106 | + ); |
| 107 | + |
| 108 | + // Convert the transformed frame back to an AnyObject |
| 109 | + AnyObject finalFrame; |
| 110 | + for (const auto &transformedEntry: transformedFrame->getMap()) { |
| 111 | + finalFrame[transformedEntry.first] = transformedEntry.second; |
| 112 | + } |
| 113 | + |
| 114 | + // Set the resolved and transformed frame in the result |
| 115 | + resolvedKeyframes->setObject(key, finalFrame); |
| 116 | + } |
| 117 | + |
| 118 | + return resolvedKeyframes; |
| 119 | + }, |
| 120 | + AnyMap::make() |
| 121 | + ); |
| 122 | + |
| 123 | + scopeMap[name] = computed; |
| 124 | + computedIt = scopeMap.find(name); |
| 125 | + } |
| 126 | + |
| 127 | + // Return get(computed) to subscribe to it |
| 128 | + return get(*computedIt->second); |
| 129 | + } |
| 130 | + |
| 131 | + void deleteScope(const std::string &name) { |
| 132 | + std::lock_guard<std::mutex> lock(keyframesMutex); |
| 133 | + scopedComputeds.erase(name); |
| 134 | + } |
| 135 | + |
| 136 | +} // namespace reactnativecss::animations |
0 commit comments