-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathobserver.cpp
270 lines (205 loc) · 7.5 KB
/
observer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//
// Copyright (C) 2020-2023 Krylov Yaroslav.
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include "catch2_extra.hpp"
#include "identity.hpp"
#include "ureact/adaptor/hold.hpp"
#include "ureact/adaptor/lift.hpp"
#include "ureact/adaptor/observe.hpp"
#include "ureact/events.hpp"
#include "ureact/transaction.hpp"
// default constructive
static_assert( std::is_default_constructible_v<ureact::observer> );
// nothrow movable only
static_assert( std::is_copy_constructible_v<ureact::observer> );
static_assert( std::is_copy_assignable_v<ureact::observer> );
static_assert( std::is_move_constructible_v<ureact::observer> );
static_assert( std::is_move_assignable_v<ureact::observer> );
static_assert( std::is_nothrow_move_constructible_v<ureact::observer> );
static_assert( std::is_nothrow_move_assignable_v<ureact::observer> );
// TODO: consider separation of observer.cpp into observer.cpp and observe.cpp
// TODO: check construction and moving
// TODO: test .detach() .is_valid()
// TODO: test observe with temporary events. Synced and not
// TODO: order tests (less specific to more specific)
// TODO: refactor NoObserveOnNoChanged
// Functor used for observer can optionally return value
// Using this value observer can be optionally self detached
TEST_CASE( "SelfObserverDetach" )
{
ureact::context ctx;
std::vector<int> results;
auto observe_handler = [&]( int v ) {
if( v < 0 )
{
return ureact::observer_action::stop_and_detach;
}
else
{
results.push_back( v );
return ureact::observer_action::next;
}
};
auto src = ureact::make_source<int>( ctx );
ureact::observer obs;
std::vector input_values{ 1, 2, 3, -1, 4 };
SECTION( "signal" )
{
obs = ureact::observe( src | ureact::hold( -1 ), observe_handler );
}
SECTION( "source" )
{
obs = ureact::observe( src, observe_handler );
}
for( int i : { 1, 2, 3, -1, 4 } )
src << i;
CHECK( results == std::vector{ 1, 2, 3 } );
}
TEST_CASE( "NoObserveOnNoChanged" )
{
ureact::context ctx;
auto a = make_var( ctx, 1 );
auto b = make_var( ctx, 1 );
auto product = a * b;
auto expressionString = ureact::lift(
with( a, b, product ), []( const int a_, const int b_, const int product_ ) {
return std::to_string( a_ ) + " * " + std::to_string( b_ ) + " = "
+ std::to_string( product_ );
} );
int aObserveCount = 0;
int bObserveCount = 0;
int productObserveCount = 0;
ureact::observer obs1 = ureact::observe( a, [&]( int /*v*/ ) { ++aObserveCount; } );
ureact::observer obs2 = ureact::observe( b, [&]( int /*v*/ ) { ++bObserveCount; } );
ureact::observer obs3 = ureact::observe( product, [&]( int /*v*/ ) { ++productObserveCount; } );
CHECK( aObserveCount == 0 );
CHECK( bObserveCount == 0 );
CHECK( productObserveCount == 0 );
CHECK( expressionString.get() == "1 * 1 = 1" );
b <<= 2;
CHECK( aObserveCount == 0 );
CHECK( bObserveCount == 1 );
CHECK( productObserveCount == 1 );
CHECK( expressionString.get() == "1 * 2 = 2" );
b <<= 2; // Shouldn't change
CHECK( aObserveCount == 0 );
CHECK( bObserveCount == 1 );
CHECK( productObserveCount == 1 );
CHECK( expressionString.get() == "1 * 2 = 2" );
{
ureact::transaction _{ ctx };
b <<= 1;
b <<= 2; // Shouldn't change
}
CHECK( aObserveCount == 0 );
CHECK( bObserveCount == 1 );
CHECK( productObserveCount == 1 );
CHECK( expressionString.get() == "1 * 2 = 2" );
a <<= 0;
CHECK( aObserveCount == 1 );
CHECK( bObserveCount == 1 );
CHECK( productObserveCount == 2 );
CHECK( expressionString.get() == "0 * 2 = 0" );
b <<= 3;
CHECK( aObserveCount == 1 );
CHECK( bObserveCount == 2 );
CHECK( productObserveCount == 2 ); // Product shouldn't change
CHECK( expressionString.get() == "0 * 3 = 0" );
}
TEST_CASE( "SyncedObserveTest" )
{
ureact::context ctx;
auto a = make_var( ctx, 1 );
auto b = make_var( ctx, 1 );
const auto sum = a + b;
const auto prod = a * b;
const auto diff = a - b;
SECTION( "unit source" )
{
const auto src = ureact::make_source( ctx );
using result_t = std::vector<std::tuple<int, int, int>>;
result_t result;
ureact::observer _ = ureact::observe(
src, with( sum, prod, diff ), [&]( ureact::unit, int sum, int prod, int diff ) {
result.emplace_back( sum, prod, diff );
} );
a <<= 22;
b <<= 11;
CHECK( result.empty() );
src.emit();
const result_t expected = { { 33, 242, 11 } };
CHECK( result == expected );
}
SECTION( "string source" )
{
const auto src = ureact::make_source<std::string>( ctx );
using result_t = std::vector<std::tuple<std::string, int, int, int>>;
result_t result;
ureact::observer _ = ureact::observe(
src, with( sum, prod, diff ), [&]( const std::string& e, int sum, int prod, int diff ) {
result.emplace_back( e, sum, prod, diff );
} );
a <<= 22;
b <<= 11;
CHECK( result.empty() );
src.emit( "Wtf?" );
const result_t expected = { { "Wtf?", 33, 242, 11 } };
CHECK( result == expected );
}
}
TEST_CASE( "Observers" )
{
ureact::context ctx;
auto x = make_var( ctx, 0 );
std::vector<int> x_values;
auto on_x_value_change = [&]( const int new_value ) { x_values.push_back( new_value ); };
CHECK( x_values == std::vector<int>{} );
SECTION( "Subject-bound observers v1" )
{
// Inner scope
{
// Create a signal in the function scope
auto my_signal = ureact::lift( x, identity{} );
// [Obsolete] The lifetime of the observer is bound to my_signal.
// [Obsolete] After scope my_signal is destroyed, and so is the observer
ureact::observer _ = ureact::observe( my_signal, on_x_value_change );
x <<= 1;
CHECK( x_values == std::vector<int>{ 1 } );
}
// ~Inner scope
x <<= 2; // no output
CHECK( x_values == std::vector<int>{ 1 } );
}
SECTION( "Subject-bound observers v2" )
{
// Outer scope
{
// Unbound observer
ureact::observer obs;
// Inner scope
{
auto my_signal = ureact::lift( x, identity{} );
// Move-assign to obs
obs = ureact::observe( my_signal, on_x_value_change );
// [Obsolete] The node linked to my_signal is now also owned by obs
x <<= 1;
CHECK( x_values == std::vector<int>{ 1 } );
}
// ~Inner scope
// [Obsolete] my_signal was destroyed, but as long as obs exists and is still
// [Obsolete] attached to the signal node, this signal node won't be destroyed
x <<= 2;
CHECK( x_values == std::vector<int>{ 1, 2 } );
}
// ~Outer scope
// [Obsolete] obs was destroyed
// [Obsolete] -> the signal node is no longer owned by anything and is destroyed
// [Obsolete] -> the observer node is destroyed as it was bound to the subject
x <<= 3; // no output
CHECK( x_values == std::vector<int>{ 1, 2 } );
}
}