Skip to content

Commit 08cc974

Browse files
committed
Added some more examples.
1 parent e32118d commit 08cc974

2 files changed

Lines changed: 89 additions & 19 deletions

File tree

include/react/ReactiveObject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ class ReactiveObject
9191
////////////////////////////////////////////////////////////////////////////////////////
9292
#define DYNAMIC_REF(obj, name) \
9393
Flatten( \
94-
MakeSignal([] ( \
95-
Identity<decltype(obj)>::Type::ValueT r) { return r->name; }, obj))
94+
MakeSignal( \
95+
[] (Identity<decltype(obj)>::Type::ValueT r) { return r->name; }, obj))
9696
};
9797

9898
} //~namespace react

src/sandbox/Main.cpp

Lines changed: 87 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ void SignalExample1()
2727
auto area = width * height;
2828
auto volume = area * depth;
2929

30-
Observe(volume, [] (int v) {
31-
cout << "Volume changed to: " << v << endl;
32-
});
33-
3430
cout << "t0" << endl;
3531
cout << "\tArea: " << area() << endl;
3632
cout << "\tVolume: " << volume() << endl;
@@ -53,8 +49,6 @@ void SignalExample2()
5349
auto height = D::MakeVar(70);
5450
auto depth = D::MakeVar(8);
5551

56-
auto src = D::MakeVar(0);
57-
5852
auto volume = (width,height,depth) >>= [] (int w, int h, int d) {
5953
return w * h * d;
6054
};
@@ -63,29 +57,38 @@ void SignalExample2()
6357
// This observer handle holds a shared_ptr to the subject, so as long as it exists,
6458
// the subject will not be destroyed.
6559
// The lifetime of the observer itself is tied to the subject.
66-
Observe(src, [&] (int v) {
67-
cout << "v: " << v << endl;
68-
if (v < 10)
69-
src <<= v+1;
60+
Observe(volume, [] (int v) {
61+
cout << "Volume changed to: " << v << endl;
7062
});
7163

7264
D::DoTransaction([&] {
7365
width <<= 90;
7466
depth <<= 80;
7567
});
7668

77-
src <<= 1;
78-
7969
cout << endl;
70+
}
8071

72+
void SignalExample3()
73+
{
74+
cout << "Signal Example 3" << endl;
8175

82-
//auto h = D::MakeVar(width);
83-
static_assert(IsSignalT<D,decltype(width)>::value, "Not a signal.");
84-
static_assert(! IsSignalT<D,decltype(60)>::value, "A signal.");
76+
auto src = D::MakeVar(0);
8577

86-
auto h = D::MakeVar(width);
87-
}
78+
// Input values can be manipulated imperatively in observers.
79+
// Inputs are implicitly thread-safe, buffered and executed in a continuation turn.
80+
// This continuation turn is queued just like a regular turn.
81+
// If other turns are already queued, they are executed before the continuation.
82+
Observe(src, [&] (int v) {
83+
cout << "V: " << v << endl;
84+
if (v < 10)
85+
src <<= v+1;
86+
});
87+
88+
src <<= 1;
8889

90+
cout << endl;
91+
}
8992

9093
void EventExample1()
9194
{
@@ -106,13 +109,39 @@ void EventExample1()
106109
cout << endl;
107110
}
108111

112+
void EventExample2()
113+
{
114+
cout << "Event Example 2" << endl;
115+
116+
// The event type can be omitted if not required, in which case the event
117+
// stream just indicates that it has fired, i.e. it behaves like a token stream.
118+
auto emitter = D::MakeEventSource();
119+
120+
auto counter = Iterate(0, emitter, Incrementer<int>());
121+
122+
// In this case, the observer func must not declare a parameter for token streams.
123+
Observe(emitter, [] {
124+
cout << "Emitter fired!" << endl;
125+
});
126+
127+
// Using .Emit() to fire rather than "<< value"
128+
for (int i=0; i<5; i++)
129+
emitter.Emit();
130+
131+
cout << "Counted " << counter() << " events" << endl;
132+
133+
cout << endl;
134+
}
135+
109136
class Person : public ReactiveObject<D>
110137
{
111138
public:
112139
VarSignal<int> Age = MakeVar(1);
113140
Signal<int> Health = 100 - Age;
114141
Signal<int> Wisdom = Age * Age / 100;
115142

143+
Signal<int> Test;
144+
116145
// Note: Initializing them directly uses the same lambda for both signals...
117146
// compiler bug?
118147
Observer wisdomObs;
@@ -150,6 +179,43 @@ void ObjectExample1()
150179
cout << endl;
151180
}
152181

182+
class PersonManager : public ReactiveObject<D>
183+
{
184+
public:
185+
VarSignal<Person*> CurrentPerson;
186+
187+
Observer healthObs;
188+
189+
PersonManager(Person& p) :
190+
CurrentPerson{ MakeVar(&p) }
191+
{
192+
// Todo: Safety, doesn't work for VarSignal etc.
193+
healthObs = DYNAMIC_REF(CurrentPerson, Health).Observe([] (int v) {
194+
cout << "Manager: Health changed to " << v << endl;
195+
});
196+
}
197+
};
198+
199+
void ObjectExample2()
200+
{
201+
cout << "Object Example 2" << endl;
202+
203+
Person person1;
204+
Person person2;
205+
206+
PersonManager mgmt{ person1 };
207+
208+
person1.Age <<= 30;
209+
person2.Age <<= 15;
210+
211+
mgmt.CurrentPerson <<= &person2;
212+
213+
person1.Age <<= 40;
214+
person2.Age <<= 25;
215+
216+
cout << endl;
217+
}
218+
153219
void FoldExample1()
154220
{
155221
cout << "Fold Example 1" << endl;
@@ -220,9 +286,13 @@ int main()
220286
{
221287
SignalExample1();
222288
SignalExample2();
289+
SignalExample3();
290+
223291
EventExample1();
292+
EventExample2();
224293

225294
ObjectExample1();
295+
ObjectExample2();
226296

227297
FoldExample1();
228298

0 commit comments

Comments
 (0)