Skip to content

Commit f5cadc1

Browse files
committed
feat: add get() to Effect callback parameters
1 parent 847f42f commit f5cadc1

2 files changed

Lines changed: 34 additions & 10 deletions

File tree

cpp/Effect.hpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,37 @@
99

1010
namespace reactnativecss {
1111

12+
// Forward declarations to allow Effect::GetProxy operators without including headers here
13+
template<class T>
14+
class Observable;
15+
16+
template<class T>
17+
class Computed;
18+
1219
class Effect {
1320
public:
14-
using Callback = std::function<void()>;
21+
struct GetProxy {
22+
Effect *self;
23+
24+
template<class U>
25+
inline const U &operator()(Observable<U> &obs) const noexcept {
26+
return obs.get(*self);
27+
}
28+
29+
template<class U>
30+
inline const U &operator()(Computed<U> &comp) const noexcept {
31+
return comp.get(*self);
32+
}
33+
};
34+
35+
using Callback = std::function<void(GetProxy &)>;
1536

1637
explicit Effect(Callback cb) : callback_(std::move(cb)) {}
1738

39+
// Backward-compat: allow constructing with a no-arg callback
40+
explicit Effect(std::function<void()> cb)
41+
: callback_([fn = std::move(cb)](GetProxy &) { if (fn) fn(); }) {}
42+
1843
Effect(const Effect &) = delete;
1944

2045
Effect &operator=(const Effect &) = delete;
@@ -86,8 +111,10 @@ namespace reactnativecss {
86111
// Immediate execution helper
87112
void runImmediate() {
88113
dispose();
89-
if (callback_)
90-
callback_();
114+
if (callback_) {
115+
GetProxy g{this};
116+
callback_(g);
117+
}
91118
}
92119

93120
// Per-thread batching state
@@ -107,4 +134,4 @@ namespace reactnativecss {
107134
}
108135
};
109136

110-
} // namespace nitro
137+
} // namespace reactnativecss

cpp/ShadowTreeUpdateManager.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <cassert>
1717

1818
namespace margelo::nitro::cssnitro {
19+
1920
using jsi::Runtime;
2021
using reactnativecss::Observable;
2122
namespace nitro_ns = ::margelo::nitro;
@@ -151,17 +152,13 @@ namespace margelo::nitro::cssnitro {
151152
}
152153
if (runtime_effects_.find(rt) == runtime_effects_.end()) {
153154
auto obs = rtObsIt->second;
154-
auto weakBox = std::make_shared<std::weak_ptr<reactnativecss::Effect>>();
155155
auto effect = std::make_shared<reactnativecss::Effect>(
156-
[obs, rt, weakBox]() {
157-
auto eff = weakBox->lock();
158-
if (!eff) return;
159-
const ShadowTreeUpdateManager::UpdatesMap &updates = obs->get(*eff);
156+
[obs, rt](reactnativecss::Effect::GetProxy &get) {
157+
const ShadowTreeUpdateManager::UpdatesMap &updates = get(*obs);
160158
if (updates.empty()) return;
161159
ShadowTreeUpdateManager::applyUpdates(*rt, updates);
162160
obs->set(ShadowTreeUpdateManager::UpdatesMap{});
163161
});
164-
*weakBox = effect;
165162
effect->run();
166163
runtime_effects_.emplace(rt, std::move(effect));
167164
}

0 commit comments

Comments
 (0)