Skip to content

Commit b476204

Browse files
committed
Removed DetachAllObservers as it's too dangerous. Having observer groups would be a safer approach.
1 parent 7024616 commit b476204

3 files changed

Lines changed: 9 additions & 74 deletions

File tree

‎examples/src/BasicObservers.cpp‎

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -96,73 +96,21 @@ namespace example1
9696
}
9797

9898
///////////////////////////////////////////////////////////////////////////////////////////////////
99-
/// Example 2 - Detaching observers manually (1)
99+
/// Example 2 - Detaching observers manually
100100
///////////////////////////////////////////////////////////////////////////////////////////////////
101101
namespace example2
102102
{
103103
using namespace std;
104104
using namespace react;
105105

106-
REACTIVE_DOMAIN(D)
107-
108-
class DelayedExecutor
109-
{
110-
public:
111-
USING_REACTIVE_DOMAIN(D)
112-
113-
template <typename F>
114-
void ScheduleForExecution(const F& func)
115-
{
116-
Observe(trigger_, [=] (Token) { func(); });
117-
}
118-
119-
void Execute()
120-
{
121-
trigger_.Emit();
122-
DetachAllObservers(trigger_);
123-
}
124-
125-
private:
126-
EventSourceT<> trigger_ = MakeEventSource<D>();
127-
};
128-
129-
void Run()
130-
{
131-
cout << "Example 2 - Detaching observers manually (1)" << endl;
132-
133-
DelayedExecutor exec;
134-
135-
exec.ScheduleForExecution([] {
136-
cout << "one" << endl;
137-
});
138-
exec.ScheduleForExecution([] {
139-
cout << "two" << endl;
140-
});
141-
142-
exec.Execute(); // output: one, two
143-
144-
exec.Execute(); // no output
145-
146-
cout << endl;
147-
}
148-
}
149-
150-
///////////////////////////////////////////////////////////////////////////////////////////////////
151-
/// Example 3 - Detaching observers manually (2)
152-
///////////////////////////////////////////////////////////////////////////////////////////////////
153-
namespace example3
154-
{
155-
using namespace std;
156-
using namespace react;
157-
158106
REACTIVE_DOMAIN(D)
159107
USING_REACTIVE_DOMAIN(D)
160108

161109
EventSourceT<> trigger = MakeEventSource<D>();
162110

163111
void Run()
164112
{
165-
cout << "Example 3 - Detaching observers manually (2)" << endl;
113+
cout << "Example 2 - Detaching observers manually" << endl;
166114

167115
ObserverT obs = Observe(trigger, [] (Token) {
168116
cout << "Triggered!" << endl;
@@ -179,9 +127,9 @@ namespace example3
179127
}
180128

181129
///////////////////////////////////////////////////////////////////////////////////////////////////
182-
/// Example 4 - Using scoped observers
130+
/// Example 3 - Using scoped observers
183131
///////////////////////////////////////////////////////////////////////////////////////////////////
184-
namespace example4
132+
namespace example3
185133
{
186134
using namespace std;
187135
using namespace react;
@@ -193,7 +141,7 @@ namespace example4
193141

194142
void Run()
195143
{
196-
cout << "Example 4 - Using scoped observers" << endl;
144+
cout << "Example 3 - Using scoped observers" << endl;
197145

198146
// Inner scope
199147
{
@@ -236,7 +184,5 @@ int main()
236184

237185
example3::Run();
238186

239-
example4::Run();
240-
241187
return 0;
242188
}

‎include/react/Observer.h‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,6 @@ auto Observe(const Events<D,E>& subject,
231231
return Observer<D>(rawObsPtr, subject.NodePtr());
232232
}
233233

234-
///////////////////////////////////////////////////////////////////////////////////////////////////
235-
/// DetachAllObservers
236-
///////////////////////////////////////////////////////////////////////////////////////////////////
237-
template <typename TSubject>
238-
void DetachAllObservers(const TSubject& subject)
239-
{
240-
using D = typename TSubject::DomainT;
241-
242-
REACT_IMPL::DomainSpecificObserverRegistry<D>::Instance().UnregisterFrom(
243-
subject.NodePtr().get());
244-
}
245-
246234
///////////////////////////////////////////////////////////////////////////////////////////////////
247235
/// DetachThisObserver
248236
///////////////////////////////////////////////////////////////////////////////////////////////////

‎tests/src/ObserverTest.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ TYPED_TEST_P(ObserverTest, Detach)
6262
ASSERT_TRUE(false);
6363
});
6464

65-
Observe(result, [&] (int v)
65+
auto obs2 = Observe(result, [&] (int v)
6666
{
6767
observeCount2++;
6868

@@ -74,7 +74,7 @@ TYPED_TEST_P(ObserverTest, Detach)
7474
ASSERT_TRUE(false);
7575
});
7676

77-
Observe(result, [&] (int v)
77+
auto obs3 = Observe(result, [&] (int v)
7878
{
7979
observeCount3++;
8080

@@ -100,7 +100,8 @@ TYPED_TEST_P(ObserverTest, Detach)
100100
ASSERT_EQ(observeCount3,2);
101101

102102
phase = 2;
103-
DetachAllObservers(result);
103+
obs2.Detach();
104+
obs3.Detach();
104105
a1 <<= 4;
105106
ASSERT_EQ(observeCount1,1);
106107
ASSERT_EQ(observeCount2,2);

0 commit comments

Comments
 (0)