Last active
November 23, 2020 15:25
-
-
Save madmongo1/268bcf770c1f05f8c3a4e69b715a6882 to your computer and use it in GitHub Desktop.
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
net::awaitable< void > | |
main_window::monitor_deribit_indecies(QMenu *menu) | |
{ | |
struct index_entry | |
{ | |
QAction *menu_action = nullptr; | |
bool | |
valid() const noexcept | |
{ | |
return bool(menu_action); | |
} | |
}; | |
std::unordered_map< std::string, index_entry > index_map; | |
auto indecies_feed = trade::deribit_index_price_names(entity_service_); | |
auto indecies_connection = co_await indecies_feed.subscribe(); | |
auto stopped = false; | |
auto close_connection = close_signal_.connect([&] { | |
stopped = true; | |
indecies_connection.disconnect(); | |
}); | |
BOOST_SCOPE_EXIT_ALL(&) { close_connection.disconnect(); }; | |
while (!stopped) | |
{ | |
auto snap = co_await indecies_connection.consume(); | |
if (snap.status() == trade::snapshot_status::good) | |
{ | |
auto to_remove = keys_of(index_map); | |
for (auto &deribit_index_name : snap.names()) | |
{ | |
auto [base, term] = [&] { | |
auto term = std::string(); | |
auto und = deribit_index_name.find('_'); | |
auto base = | |
boost::to_upper_copy(deribit_index_name.substr(0, und)); | |
if (und != std::string::npos) | |
term = boost::to_upper_copy( | |
deribit_index_name.substr(und + 1)); | |
return std::make_tuple(std::move(base), std::move(term)); | |
}(); | |
auto index_name = fmt::format("{}/{}", base, term); | |
auto key = trade::ccy_pair { | |
.base = trade::currency(base), | |
.term = trade::currency(term) | |
}; | |
to_remove.erase(index_name); | |
auto &entry = index_map[index_name]; | |
if (!entry.valid()) | |
{ | |
entry.menu_action = | |
menu->addAction(QString::fromStdString(index_name)); | |
QObject::connect( | |
entry.menu_action, | |
&QAction::triggered, | |
this, | |
[this, key] { | |
auto pred = [&](deribit::index_rate_widget *p) { | |
return p->key() == key; | |
}; | |
auto gen = [&] { | |
return new deribit::index_rate_widget( | |
key, entity_service_); | |
}; | |
activate_mdi_view(mdi_area_, pred, gen); | |
}); | |
} | |
else | |
{ | |
entry.menu_action->setEnabled(true); | |
} | |
} | |
for (auto &index_name : to_remove) | |
if (auto imap = index_map.find(index_name); | |
imap != index_map.end()) | |
{ | |
menu->removeAction(imap->second.menu_action); | |
index_map.erase(imap); | |
} | |
} | |
else | |
{ | |
for (auto &entry : index_map) | |
{ | |
entry.second.menu_action->setEnabled(false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment