DEV: Asyncify `findTopicList()` (#17816)

This commit is contained in:
Jarek Radosz 2022-08-08 11:46:09 +02:00 committed by GitHub
parent 6f3be0c25a
commit e6fa05f8c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 55 additions and 47 deletions

View File

@ -5,7 +5,6 @@ import {
} from "discourse/controllers/discovery-sortable"; } from "discourse/controllers/discovery-sortable";
import DiscourseRoute from "discourse/routes/discourse"; import DiscourseRoute from "discourse/routes/discourse";
import I18n from "I18n"; import I18n from "I18n";
import { Promise } from "rsvp";
import Session from "discourse/models/session"; import Session from "discourse/models/session";
import Site from "discourse/models/site"; import Site from "discourse/models/site";
import { deepEqual } from "discourse-common/lib/object"; import { deepEqual } from "discourse-common/lib/object";
@ -28,9 +27,14 @@ function filterQueryParams(params, defaultParams) {
return findOpts; return findOpts;
} }
function findTopicList(store, tracking, filter, filterParams, extras) { async function findTopicList(
extras = extras || {}; store,
return new Promise(function (resolve) { tracking,
filter,
filterParams,
extras = {}
) {
let list;
const session = Session.current(); const session = Session.current();
if (extras.cached) { if (extras.cached) {
@ -45,45 +49,49 @@ function findTopicList(store, tracking, filter, filterParams, extras) {
) { ) {
cachedList.set("loaded", true); cachedList.set("loaded", true);
if (tracking) { tracking?.updateTopics(cachedList.get("topics"));
tracking.updateTopics(cachedList.get("topics")); list = cachedList;
}
return resolve(cachedList);
} }
session.set("topicList", null); session.set("topicList", null);
} else { } else {
// Clear the cache // Clear the cache
session.setProperties({ topicList: null, topicListScrollPosition: null }); session.setProperties({ topicList: null, topicListScrollPosition: null });
} }
if (!list) {
// Clean up any string parameters that might slip through // Clean up any string parameters that might slip through
filterParams = filterParams || {}; filterParams ||= {};
Object.keys(filterParams).forEach((k) => { for (const [key, val] of Object.entries(filterParams)) {
const val = filterParams[k];
if (val === "undefined" || val === "null") { if (val === "undefined" || val === "null") {
filterParams[k] = null; filterParams[key] = null;
}
}
list = await store.findFiltered("topicList", {
filter,
params: filterParams,
});
} }
});
return resolve(
store.findFiltered("topicList", { filter, params: filterParams || {} })
);
}).then(function (list) {
list.set("listParams", filterParams); list.set("listParams", filterParams);
if (tracking) { if (tracking) {
tracking.sync(list, list.filter, filterParams); tracking.sync(list, list.filter, filterParams);
tracking.trackIncoming(list.filter); tracking.trackIncoming(list.filter);
} }
Session.currentProp("topicList", list); Session.currentProp("topicList", list);
if (list.topic_list && list.topic_list.top_tags) {
if (list.topic_list?.top_tags) {
if (list.filter.startsWith("c/") || list.filter.startsWith("tags/c/")) { if (list.filter.startsWith("c/") || list.filter.startsWith("tags/c/")) {
Site.currentProp("category_top_tags", list.topic_list.top_tags); Site.currentProp("category_top_tags", list.topic_list.top_tags);
} else { } else {
Site.currentProp("top_tags", list.topic_list.top_tags); Site.currentProp("top_tags", list.topic_list.top_tags);
} }
} }
return list; return list;
});
} }
export default function (filter, extras) { export default function (filter, extras) {