Moved all destruction calls of DictLoader to main thread.

This commit is contained in:
23rd 2020-02-26 12:38:24 +03:00 committed by John Preston
parent 7bd0598555
commit a0584ea7a1
2 changed files with 13 additions and 16 deletions

View File

@ -133,27 +133,22 @@ void DownloadDictionaryInBackground(
const auto id = langs[counter];
counter++;
const auto destroyer = [=] {
// This is a temporary workaround.
const auto copyId = id;
const auto copyLangs = langs;
const auto copySession = session;
const auto copyCounter = counter;
BackgroundLoader = nullptr;
BackgroundLoaderChanged.fire(0);
if (DictionaryExists(copyId)) {
auto dicts = copySession->settings().dictionariesEnabled();
if (!ranges::contains(dicts, copyId)) {
dicts.push_back(copyId);
copySession->settings().setDictionariesEnabled(std::move(dicts));
copySession->saveSettingsDelayed();
if (DictionaryExists(id)) {
auto dicts = session->settings().dictionariesEnabled();
if (!ranges::contains(dicts, id)) {
dicts.push_back(id);
session->settings().setDictionariesEnabled(std::move(dicts));
session->saveSettingsDelayed();
}
}
if (copyCounter >= copyLangs.size()) {
if (counter >= langs.size()) {
return;
}
DownloadDictionaryInBackground(copySession, copyCounter, copyLangs);
DownloadDictionaryInBackground(session, counter, langs);
};
if (DictionaryExists(id)) {
destroyer();
@ -194,20 +189,21 @@ DictLoader::DictLoader(
}
void DictLoader::unpack(const QString &path) {
Expects(_destroyCallback);
crl::async([=] {
const auto success = Spellchecker::UnpackDictionary(path, id());
if (success) {
QFile(path).remove();
destroy();
return;
}
crl::on_main(success ? _destroyCallback : [=] { fail(); });
crl::on_main([=] { fail(); });
});
}
void DictLoader::destroy() {
Expects(_destroyCallback);
_destroyCallback();
crl::on_main(_destroyCallback);
}
void DictLoader::fail() {

View File

@ -60,6 +60,7 @@ private:
void unpack(const QString &path) override;
void fail() override;
// Be sure to always call it in the main thread.
Fn<void()> _destroyCallback;
rpl::lifetime _lifetime;