Skip to content

Commit 53e8cf2

Browse files
committed
fix
1 parent d7fb36c commit 53e8cf2

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

src/lib/languageModeRecommendations.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ function hasPlainTextFallback(modeInfo, filename) {
5858

5959
class LanguageModeRecommendations {
6060
notifiedKeywords = new Set();
61+
pendingKeywords = new Set();
6162
availabilityCache = new Map();
6263

6364
async getPluginAvailability(keyword) {
@@ -73,9 +74,9 @@ class LanguageModeRecommendations {
7374
),
7475
),
7576
)
76-
.then((response) => response.json())
77+
.then((response) => (response.ok ? response.json() : []))
7778
.then((plugins) => Array.isArray(plugins) && plugins.length > 0)
78-
.catch(() => true);
79+
.catch(() => false);
7980

8081
this.availabilityCache.set(keyword, availability);
8182
return availability;
@@ -88,11 +89,25 @@ class LanguageModeRecommendations {
8889
if (!hasPlainTextFallback(modeInfo, filename)) return;
8990

9091
const keyword = getSearchKeyword(filename);
91-
if (!keyword || this.notifiedKeywords.has(keyword)) return;
92-
93-
this.notifiedKeywords.add(keyword);
92+
if (
93+
!keyword ||
94+
this.notifiedKeywords.has(keyword) ||
95+
this.pendingKeywords.has(keyword)
96+
) {
97+
return;
98+
}
9499

95-
void this.showRecommendation(keyword);
100+
this.pendingKeywords.add(keyword);
101+
void this.showRecommendation(keyword)
102+
.then(() => {
103+
this.notifiedKeywords.add(keyword);
104+
})
105+
.catch((error) => {
106+
console.warn("Failed to show extension recommendation.", error);
107+
})
108+
.finally(() => {
109+
this.pendingKeywords.delete(keyword);
110+
});
96111
}
97112

98113
async showRecommendation(keyword) {

src/sidebarApps/extensions/index.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ let container = null;
2727
let $searchResult = null;
2828

2929
const LIMIT = 50;
30+
const SEARCH_INPUT_WAIT_TIMEOUT = 1000;
3031
let currentPage = 1;
3132
let hasMore = true;
3233
let isLoading = false;
@@ -260,15 +261,35 @@ async function runSearch(query) {
260261
}
261262
}
262263

263-
export function openWithSearch(query) {
264+
function getSearchInput() {
265+
return container?.querySelector('input[name="search-ext"]');
266+
}
267+
268+
function waitForSearchInput() {
269+
const startTime = Date.now();
270+
271+
return new Promise((resolve) => {
272+
const check = () => {
273+
const searchInput = getSearchInput();
274+
if (searchInput || Date.now() - startTime >= SEARCH_INPUT_WAIT_TIMEOUT) {
275+
resolve(searchInput);
276+
return;
277+
}
278+
279+
requestAnimationFrame(check);
280+
};
281+
282+
check();
283+
});
284+
}
285+
286+
export async function openWithSearch(query) {
264287
Sidebar.show();
265288
document
266289
.querySelector('[data-action="sidebar-app"][data-id="extensions"]')
267290
?.click();
268291

269-
if (!container) return;
270-
271-
const searchInput = container.querySelector('input[name="search-ext"]');
292+
const searchInput = await waitForSearchInput();
272293
if (!searchInput) return;
273294

274295
searchInput.value = query;

0 commit comments

Comments
 (0)