这与 Rx 中常见的 UX 拖动示例有关。在这种情况下,压力机是加密狗连接,而释放是加密狗移除。
此解决方案要求一次只能连接一个加密狗(预计在下一次连接之前移除)。事件中必须有更多信息才能允许多个连接重叠。
这是解决方案的核心。整个程序如下。
auto DatasFromConnectedDongle = DongleConnects. // when connected
map([=](DongleEvent const & event){
assert(event == DongleEvent::Connected);
cout << "Connected - " << flush;
return DongleDatas. // return all the datas
take_until(DongleRemoves). // stop when removed
finally([](){
cout << "- Removed" << endl;
});
}).
switch_on_next(). // only listen to datas from the most recent connected dongle
take_until(Exits); // stop everything when key is pressed
我确实最终使用了repeat()
,但只是为了生成测试事件数据。
#include <rxcpp/rx.hpp>
namespace Rx {
using namespace rxcpp;
using namespace rxcpp::sources;
using namespace rxcpp::operators;
using namespace rxcpp::util;
}
using namespace Rx;
#include <cassert>
using namespace std;
using namespace std::chrono;
int main()
{
//
// test code
//
auto keyboardPressed = [](){
return false;
};
enum class DongleEvent {
Connected,
Removed,
Data,
Other
};
auto events = from(
DongleEvent::Data, DongleEvent::Other,
DongleEvent::Connected, DongleEvent::Data,
DongleEvent::Other, DongleEvent::Other,
DongleEvent::Data, DongleEvent::Removed,
DongleEvent::Other, DongleEvent::Data).
repeat(5). // send the above events five times over
zip(take_at<0>(), interval(milliseconds(200))). // pace our test data
publish().
ref_count(); // publish and ref_count make the events sharable
//
// the solution
//
// fires when connected
auto DongleConnects = events.
filter([](DongleEvent const & event) {
return event == DongleEvent::Connected;
});
// fires when data arrives
auto DongleDatas = events.
filter([](DongleEvent const & event) {
return event == DongleEvent::Data;
});
// fires when removed
auto DongleRemoves = events.
filter([](DongleEvent const & event) {
return event == DongleEvent::Removed;
});
// fires when key pressed
auto Exits = interval(milliseconds(200)).
filter([=](long){
return keyboardPressed();
});
auto DatasFromConnectedDongle = DongleConnects.
map([=](DongleEvent const & event){
assert(event == DongleEvent::Connected);
cout << "Connected - " << flush;
return DongleDatas. // return all the datas
take_until(DongleRemoves). // stop when removed
finally([](){
cout << "- Removed" << endl;
});
}).
switch_on_next(). // only listen to datas from the most recent connected dongle
take_until(Exits); // stop everything when key is pressed
DatasFromConnectedDongle.subscribe([](DongleEvent const & event){
assert(event == DongleEvent::Data);
cout << "Data " << flush;
});
return 0;
}
生产
~/source/rxcpp/Rx/v2/examples/dongle (master)$ cmake .
...
~/source/rxcpp/Rx/v2/examples/dongle (master)$ make
Scanning dependencies of target dongle
[ 50%] Building CXX object CMakeFiles/dongle.dir/main.cpp.o
[100%] Linking CXX executable dongle
[100%] Built target dongle
~/source/rxcpp/Rx/v2/examples/dongle (master)$ ./dongle
Connected - Data Data - Removed
Connected - Data Data - Removed
Connected - Data Data - Removed
Connected - Data Data - Removed
Connected - Data Data - Removed
~/source/rxcpp/Rx/v2/examples/dongle (master)$