Skip to content

Commit c3d803b

Browse files
committed
Lock is_dirty_lock shorter
1 parent 06cca8d commit c3d803b

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/openvic-simulation/utility/reactive/DependencyTracker.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ namespace OpenVic {
1515
return;
1616
}
1717

18-
const std::lock_guard<std::mutex> lock_guard { is_dirty_lock };
19-
if (is_dirty) {
20-
return;
18+
{
19+
const std::lock_guard<std::mutex> lock_guard { is_dirty_lock };
20+
if (is_dirty) {
21+
return;
22+
}
23+
24+
disconnect_all();
25+
is_dirty = true;
2126
}
2227

23-
disconnect_all();
24-
is_dirty = true;
2528
changed();
2629
}
2730
protected:

tests/src/utility/reactive/DerivedState.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "openvic-simulation/types/Signal.hpp"
12
#include "openvic-simulation/utility/reactive/DependencyTracker.hpp"
23
#include "openvic-simulation/utility/reactive/DerivedState.hpp"
34
#include "openvic-simulation/utility/reactive/MutableState.hpp"
@@ -53,3 +54,26 @@ TEST_CASE("DerivedState reactive", "[DerivedState-reactive]") {
5354
mutable_state_a.set(3);
5455
CHECK(times_marked_dirty == 2);
5556
}
57+
58+
TEST_CASE("DerivedState untracked in callback", "[DerivedState-untracked-in-callback]") {
59+
MutableState<int> mutable_state_a(0);
60+
MutableState<int> mutable_state_b(0);
61+
DerivedState<int> sum([
62+
&a=static_cast<ReadOnlyMutableState<int>&>(mutable_state_a),
63+
&b=static_cast<ReadOnlyMutableState<int>&>(mutable_state_b)
64+
](DependencyTracker& tracker)->int {
65+
return a.get(tracker) + b.get(tracker);
66+
});
67+
68+
connection conn;
69+
int sum_value = sum.get([&sum,&conn,&sum_value](OpenVic::signal<>& sum_changed) mutable -> void {
70+
conn = sum_changed.connect([&sum,&sum_value]() mutable -> void {
71+
sum_value = sum.get_untracked();
72+
});
73+
});
74+
CHECK(sum_value == 0);
75+
mutable_state_a.set(1);
76+
CHECK(sum_value == 1);
77+
mutable_state_a.set(2);
78+
CHECK(sum_value == 2);
79+
}

0 commit comments

Comments
 (0)