Ejemplo n.º 1
0
void SuggestionsSidebarBlock::QueryProvider(SuggestionsBackend& backend, CatalogItem *item, uint64_t queryId)
{
    m_pendingQueries++;

    // we need something to talk to GUI thread through that is guaranteed
    // to exist, and the app object is a good choice:
    wxApp *app = wxTheApp;
    std::weak_ptr<SuggestionsSidebarBlock> weakSelf = std::dynamic_pointer_cast<SuggestionsSidebarBlock>(shared_from_this());

    m_provider->SuggestTranslation
    (
        backend,
        m_parent->GetCurrentLanguage(),
        item->GetString().ToStdWstring(),
        SUGGESTIONS_REQUEST_COUNT,

        // when receiving data
        [=](const SuggestionsList& hits){
            app->CallAfter([weakSelf,queryId,hits]{
                auto self = weakSelf.lock();
                // maybe this call is already out of date:
                if (!self || self->m_latestQueryId != queryId)
                    return;
                self->UpdateSuggestions(hits);
                if (--self->m_pendingQueries == 0)
                    self->OnQueriesFinished();
            });
        },

        // on error:
        [=](std::exception_ptr e){
            app->CallAfter([weakSelf,queryId,e]{
                auto self = weakSelf.lock();
                // maybe this call is already out of date:
                if (!self || self->m_latestQueryId != queryId)
                    return;
                self->SetMessage("SuggestionError", DescribeException(e));
                if (--self->m_pendingQueries == 0)
                    self->OnQueriesFinished();
            });
        }
    );
}
Ejemplo n.º 2
0
void SuggestionsSidebarBlock::ReportError(SuggestionsBackend*, std::exception_ptr e)
{
    SetMessage("SuggestionError", DescribeException(e));
}