From e6fa05f8c3275d710385061d91e901cd242209df Mon Sep 17 00:00:00 2001
From: Jarek Radosz <jradosz@gmail.com>
Date: Mon, 8 Aug 2022 11:46:09 +0200
Subject: [PATCH] DEV: Asyncify `findTopicList()` (#17816)

---
 .../discourse/app/routes/build-topic-route.js | 102 ++++++++++--------
 1 file changed, 55 insertions(+), 47 deletions(-)

diff --git a/app/assets/javascripts/discourse/app/routes/build-topic-route.js b/app/assets/javascripts/discourse/app/routes/build-topic-route.js
index dfc7128fef1..4717641724e 100644
--- a/app/assets/javascripts/discourse/app/routes/build-topic-route.js
+++ b/app/assets/javascripts/discourse/app/routes/build-topic-route.js
@@ -5,7 +5,6 @@ import {
 } from "discourse/controllers/discovery-sortable";
 import DiscourseRoute from "discourse/routes/discourse";
 import I18n from "I18n";
-import { Promise } from "rsvp";
 import Session from "discourse/models/session";
 import Site from "discourse/models/site";
 import { deepEqual } from "discourse-common/lib/object";
@@ -28,62 +27,71 @@ function filterQueryParams(params, defaultParams) {
   return findOpts;
 }
 
-function findTopicList(store, tracking, filter, filterParams, extras) {
-  extras = extras || {};
-  return new Promise(function (resolve) {
-    const session = Session.current();
+async function findTopicList(
+  store,
+  tracking,
+  filter,
+  filterParams,
+  extras = {}
+) {
+  let list;
+  const session = Session.current();
 
-    if (extras.cached) {
-      const cachedList = session.get("topicList");
+  if (extras.cached) {
+    const cachedList = session.get("topicList");
 
-      // Try to use the cached version if it exists and is greater than the topics per page
-      if (
-        cachedList &&
-        cachedList.get("filter") === filter &&
-        (cachedList.get("topics.length") || 0) > cachedList.get("per_page") &&
-        deepEqual(cachedList.get("listParams"), filterParams)
-      ) {
-        cachedList.set("loaded", true);
+    // Try to use the cached version if it exists and is greater than the topics per page
+    if (
+      cachedList &&
+      cachedList.get("filter") === filter &&
+      (cachedList.get("topics.length") || 0) > cachedList.get("per_page") &&
+      deepEqual(cachedList.get("listParams"), filterParams)
+    ) {
+      cachedList.set("loaded", true);
 
-        if (tracking) {
-          tracking.updateTopics(cachedList.get("topics"));
-        }
-        return resolve(cachedList);
-      }
-      session.set("topicList", null);
-    } else {
-      // Clear the cache
-      session.setProperties({ topicList: null, topicListScrollPosition: null });
+      tracking?.updateTopics(cachedList.get("topics"));
+      list = cachedList;
     }
 
+    session.set("topicList", null);
+  } else {
+    // Clear the cache
+    session.setProperties({ topicList: null, topicListScrollPosition: null });
+  }
+
+  if (!list) {
     // Clean up any string parameters that might slip through
-    filterParams = filterParams || {};
-    Object.keys(filterParams).forEach((k) => {
-      const val = filterParams[k];
+    filterParams ||= {};
+    for (const [key, val] of Object.entries(filterParams)) {
       if (val === "undefined" || val === "null") {
-        filterParams[k] = null;
+        filterParams[key] = null;
       }
-    });
+    }
 
-    return resolve(
-      store.findFiltered("topicList", { filter, params: filterParams || {} })
-    );
-  }).then(function (list) {
-    list.set("listParams", filterParams);
-    if (tracking) {
-      tracking.sync(list, list.filter, filterParams);
-      tracking.trackIncoming(list.filter);
+    list = await store.findFiltered("topicList", {
+      filter,
+      params: filterParams,
+    });
+  }
+
+  list.set("listParams", filterParams);
+
+  if (tracking) {
+    tracking.sync(list, list.filter, filterParams);
+    tracking.trackIncoming(list.filter);
+  }
+
+  Session.currentProp("topicList", list);
+
+  if (list.topic_list?.top_tags) {
+    if (list.filter.startsWith("c/") || list.filter.startsWith("tags/c/")) {
+      Site.currentProp("category_top_tags", list.topic_list.top_tags);
+    } else {
+      Site.currentProp("top_tags", list.topic_list.top_tags);
     }
-    Session.currentProp("topicList", list);
-    if (list.topic_list && list.topic_list.top_tags) {
-      if (list.filter.startsWith("c/") || list.filter.startsWith("tags/c/")) {
-        Site.currentProp("category_top_tags", list.topic_list.top_tags);
-      } else {
-        Site.currentProp("top_tags", list.topic_list.top_tags);
-      }
-    }
-    return list;
-  });
+  }
+
+  return list;
 }
 
 export default function (filter, extras) {