@@ -30,7 +30,7 @@ namespace margelo::nitro::cssnitro {
3030 // Initialize static members
3131 std::unique_ptr<ShadowTreeUpdateManager> HybridStyleRegistry::shadowUpdates_ =
3232 std::make_unique<ShadowTreeUpdateManager>();
33- std::unordered_map<std::string, std::shared_ptr<reactnativecss::Computed<Styled *>> > HybridStyleRegistry::computedMap_;
33+ std::unordered_map<std::string, HybridStyleRegistry::ComputedEntry > HybridStyleRegistry::computedMap_;
3434 std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<std::vector<HybridStyleRule>>>> HybridStyleRegistry::styleRuleMap_;
3535 std::atomic<uint64_t > HybridStyleRegistry::nextStyleRuleId_{1 };
3636
@@ -186,25 +186,51 @@ namespace margelo::nitro::cssnitro {
186186 const std::string &variableScope,
187187 const std::string &containerScope,
188188 const std::vector<std::string> &validAttributeQueries) {
189- if (auto existing = computedMap_.find (componentId); existing != computedMap_.end ()) {
190- if (existing->second ) {
191- existing->second ->dispose ();
189+ // Check if an entry exists for this component
190+ auto existing = computedMap_.find (componentId);
191+
192+ // Only recreate computed if parameters have changed or it doesn't exist
193+ bool shouldRecreate = false ;
194+ if (existing == computedMap_.end ()) {
195+ shouldRecreate = true ;
196+ } else {
197+ // Check if any of the parameters have changed
198+ const auto &entry = existing->second ;
199+ if (entry.classNames != classNames ||
200+ entry.variableScope != variableScope ||
201+ entry.containerScope != containerScope) {
202+ shouldRecreate = true ;
192203 }
193- computedMap_.erase (existing);
194204 }
195205
196- ( void ) rerender ;
206+ std::shared_ptr<reactnativecss::Computed<Styled *>> computed ;
197207
198- // Build computed Styled via factory
199- auto computed = ::margelo::nitro::cssnitro::makeStyledComputed (styleRuleMap_, classNames,
200- componentId,
201- rerender,
202- *shadowUpdates_,
203- variableScope,
204- containerScope,
205- validAttributeQueries);
208+ if (shouldRecreate) {
209+ // Dispose old computed if it exists
210+ if (existing != computedMap_.end () && existing->second .computed ) {
211+ existing->second .computed ->dispose ();
212+ }
206213
207- computedMap_[componentId] = computed;
214+ // Build new computed Styled via factory
215+ computed = ::margelo::nitro::cssnitro::makeStyledComputed (styleRuleMap_, classNames,
216+ componentId,
217+ rerender,
218+ *shadowUpdates_,
219+ variableScope,
220+ containerScope,
221+ validAttributeQueries);
222+
223+ // Store the new computed with its parameters
224+ computedMap_[componentId] = ComputedEntry{
225+ computed,
226+ classNames,
227+ variableScope,
228+ containerScope
229+ };
230+ } else {
231+ // Reuse existing computed
232+ computed = existing->second .computed ;
233+ }
208234
209235 // Get the value from computed - it's a Styled* that may be nullptr
210236 Styled *styledPtr = computed->get ();
@@ -221,8 +247,8 @@ namespace margelo::nitro::cssnitro {
221247 void HybridStyleRegistry::deregisterComponent (const std::string &componentId) {
222248 auto it = computedMap_.find (componentId);
223249 if (it != computedMap_.end ()) {
224- if (it->second ) {
225- it->second ->dispose ();
250+ if (it->second . computed ) {
251+ it->second . computed ->dispose ();
226252 }
227253 computedMap_.erase (it);
228254 }
0 commit comments