Add promise to RecentDialogList::get_dialogs.

This commit is contained in:
levlam 2021-09-13 20:52:17 +03:00
parent 5bec536803
commit 77c8eb9617
3 changed files with 16 additions and 14 deletions

View File

@ -16248,13 +16248,7 @@ std::pair<int32, vector<DialogId>> MessagesManager::search_dialogs(const string
return {};
}
if (query.empty()) {
if (!recently_found_dialogs_.load_dialogs(promise)) {
return {};
}
promise.set_value(Unit());
return recently_found_dialogs_.get_dialogs(limit);
return recently_found_dialogs_.get_dialogs(limit, std::move(promise));
}
auto result = dialogs_hints_.search(query, limit);

View File

@ -27,7 +27,7 @@ string RecentDialogList::get_binlog_key() const {
return PSTRING() << name_ << "_dialog_usernames_and_ids";
}
void RecentDialogList::save_dialogs() {
void RecentDialogList::save_dialogs() const {
if (dialogs_loaded_ < 2) {
return;
}
@ -66,8 +66,9 @@ void RecentDialogList::save_dialogs() {
G()->td_db()->get_binlog_pmc()->set(get_binlog_key(), value);
}
bool RecentDialogList::load_dialogs(Promise<Unit> &promise) {
bool RecentDialogList::load_dialogs(Promise<Unit> &&promise) {
if (dialogs_loaded_ >= 2) {
promise.set_value(Unit());
return true;
}
@ -77,6 +78,7 @@ bool RecentDialogList::load_dialogs(Promise<Unit> &promise) {
if (!dialog_ids_.empty()) {
save_dialogs();
}
promise.set_value(Unit());
return true;
}
@ -109,6 +111,7 @@ bool RecentDialogList::load_dialogs(Promise<Unit> &promise) {
if (!newly_found_dialogs.empty()) {
save_dialogs();
}
promise.set_value(Unit());
return true;
}
@ -221,8 +224,13 @@ void RecentDialogList::update_dialogs() {
}
}
std::pair<int32, vector<DialogId>> RecentDialogList::get_dialogs(int32 limit) {
std::pair<int32, vector<DialogId>> RecentDialogList::get_dialogs(int32 limit, Promise<Unit> &&promise) {
if (!load_dialogs(std::move(promise))) {
return {};
}
CHECK(dialogs_loaded_ == 2);
update_dialogs();
size_t result_size = min(static_cast<size_t>(limit), dialog_ids_.size());

View File

@ -27,9 +27,7 @@ class RecentDialogList : public Actor {
void remove_dialog(DialogId dialog_id);
bool load_dialogs(Promise<Unit> &promise);
std::pair<int32, vector<DialogId>> get_dialogs(int32 limit);
std::pair<int32, vector<DialogId>> get_dialogs(int32 limit, Promise<Unit> &&promise);
void clear_dialogs();
@ -42,13 +40,15 @@ class RecentDialogList : public Actor {
int32 dialogs_loaded_ = 0; // 0 - not loaded, 1 - load request was sent, 2 - loaded
MultiPromiseActor resolve_dialogs_multipromise_{"ResolveDialogsMultiPromiseActor"};
bool load_dialogs(Promise<Unit> &&promise);
bool do_add_dialog(DialogId dialog_id);
string get_binlog_key() const;
void update_dialogs();
void save_dialogs();
void save_dialogs() const;
};
} // namespace td