From c90af329d4c0b930abf551d98d7deafc8fbe36a6 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 16 Apr 2021 23:59:25 +0200 Subject: Search all manga combined plugins in parallel --- src/plugins/MangaCombined.cpp | 48 ++++++++++++++++++++++++------------------- 1 file 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>> &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>> 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>> 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; } -- cgit v1.2.3