aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-04-16 23:59:25 +0200
committerdec05eba <dec05eba@protonmail.com>2021-04-16 23:59:25 +0200
commitc90af329d4c0b930abf551d98d7deafc8fbe36a6 (patch)
tree29e4e18e73cf94d15303b6397be4b96e8f8b4071
parent78f01f5d041ba291aca05f768bb71eba71ebb58e (diff)
Search all manga combined plugins in parallel
-rw-r--r--src/plugins/MangaCombined.cpp48
1 files changed, 27 insertions, 21 deletions
diff --git a/src/plugins/MangaCombined.cpp b/src/plugins/MangaCombined.cpp
index 8b989fb..994bb47 100644
--- a/src/plugins/MangaCombined.cpp
+++ b/src/plugins/MangaCombined.cpp
@@ -7,43 +7,49 @@ namespace QuickMedia {
}
- SearchResult MangaCombinedSearchPage::search(const std::string &str, BodyItems &result_items) {
- for(auto &search_page : search_pages) {
- BodyItems search_page_body_items;
- search_page.page->search(str, search_page_body_items);
+ static void result_items_add_thread_results(std::vector<std::pair<MangaPlugin*, std::future<BodyItems>>> &search_threads, BodyItems &result_items) {
+ for(auto &search_thread : search_threads) {
+ BodyItems search_page_body_items = search_thread.second.get();
if(search_page_body_items.empty())
continue;
auto title_item = BodyItem::create("");
- title_item->set_author("======================== " + search_page.title + " ========================");
- title_item->url = search_page.service_name;
+ title_item->set_author("======================== " + search_thread.first->title + " ========================");
+ title_item->url = search_thread.first->service_name;
result_items.push_back(std::move(title_item));
for(auto &new_body_item : search_page_body_items) {
- new_body_item->userdata = search_page.page.get();
+ new_body_item->userdata = search_thread.first->page.get();
}
result_items.insert(result_items.end(), std::move_iterator(search_page_body_items.begin()), std::move_iterator(search_page_body_items.end()));
}
- return SearchResult::OK;
}
- PluginResult MangaCombinedSearchPage::get_page(const std::string &str, int page, BodyItems &result_items) {
+ SearchResult MangaCombinedSearchPage::search(const std::string &str, BodyItems &result_items) {
+ std::vector<std::pair<MangaPlugin*, std::future<BodyItems>>> search_threads;
for(auto &search_page : search_pages) {
- BodyItems search_page_body_items;
- search_page.page->get_page(str, page, search_page_body_items);
- if(search_page_body_items.empty())
- continue;
+ search_threads.push_back(std::make_pair(&search_page, std::async(std::launch::async, [&str, &search_page]() {
+ BodyItems search_page_body_items;
+ search_page.page->search(str, search_page_body_items);
+ return search_page_body_items;
+ })));
+ }
- auto title_item = BodyItem::create("");
- title_item->set_author("======================== " + search_page.title + " ========================");
- title_item->url = search_page.service_name;
+ result_items_add_thread_results(search_threads, result_items);
+ return SearchResult::OK;
+ }
- result_items.push_back(std::move(title_item));
- for(auto &new_body_item : search_page_body_items) {
- new_body_item->userdata = search_page.page.get();
- }
- result_items.insert(result_items.end(), std::move_iterator(search_page_body_items.begin()), std::move_iterator(search_page_body_items.end()));
+ PluginResult MangaCombinedSearchPage::get_page(const std::string &str, int page, BodyItems &result_items) {
+ std::vector<std::pair<MangaPlugin*, std::future<BodyItems>>> search_threads;
+ for(auto &search_page : search_pages) {
+ search_threads.push_back(std::make_pair(&search_page, std::async(std::launch::async, [&str, page, &search_page]() {
+ BodyItems search_page_body_items;
+ search_page.page->get_page(str, page, search_page_body_items);
+ return search_page_body_items;
+ })));
}
+
+ result_items_add_thread_results(search_threads, result_items);
return PluginResult::OK;
}