Last active
October 8, 2015 02:28
-
-
Save danbst/30ef063cf7a6ea894b92 to your computer and use it in GitHub Desktop.
Attempt to solve https://github.com/schlangster/cpp.react/issues/12 question 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <utility> | |
#include <conio.h> | |
#include <chrono> | |
#include <thread> | |
#include "react/Domain.h" | |
#include "react/Event.h" | |
#include "react/Observer.h" | |
#include "react/Algorithm.h" | |
#include "react/Signal.h" | |
namespace example6 | |
{ | |
using namespace std; | |
using namespace react; | |
using namespace chrono; | |
REACTIVE_DOMAIN(D, sequential) | |
USING_REACTIVE_DOMAIN(D) | |
EventSourceT<int> tick = MakeEventSource<D, int>(); | |
SignalT<long long> Sum(EventSourceT<int> ticks) { | |
return Iterate(tick, 0ll, [](int t, long long sum){ return sum + t; }); | |
} | |
SignalT<long long /*milliseconds*/> time = Sum(tick); | |
EventSourceT<> leftClick = MakeEventSource<D, Token>(); | |
EventSourceT<> rightClick = MakeEventSource<D, Token>(); | |
SignalT<bool> Delay(EventSourceT<> clicks, long long delay) { | |
return Iterate( | |
Monitor(time), | |
false, | |
With(Snapshot(clicks, time)), | |
[delay](long long t1, bool result, long long startT) { | |
long long diff = t1 - startT; | |
return startT != 0 && diff > 0 && diff <= delay; | |
} | |
); | |
} | |
EventsT<> bothClick = ChangedTo(Delay(leftClick, 20) && Delay(rightClick, 20), true); | |
void Run() | |
{ | |
cout << "Example 6 - Left and right clicks" << endl; | |
Observe(bothClick, [](Token t) { | |
cout << endl << "-->both clicks" << endl; | |
}); | |
cout << endl; | |
while (true) { | |
if (_kbhit()) { | |
char ch = _getch(); | |
switch (ch) { | |
case 'q': | |
return; | |
case 'l': | |
leftClick(); | |
break; | |
case 'r': | |
rightClick(); | |
break; | |
} | |
cout << ch; | |
} | |
this_thread::sleep_for(milliseconds(1)); | |
tick(1); | |
} | |
} | |
} | |
int main() | |
{ | |
example6::Run(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment