From d29f0fc31c8ff86fef12bab6a0522313bf069ba1 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Sat, 7 Oct 2023 18:38:45 +0800 Subject: [PATCH] don't overwrite status of others with concurrent requests --- src/App.svelte | 112 ++++++++++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 52 deletions(-) diff --git a/src/App.svelte b/src/App.svelte index c745b5b..4c2ee73 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -104,64 +104,72 @@ } } + let concurrency = 0; async function do_search(more?: any) { - abort.abort(); - abort = new AbortController(); - if (!group && !islocal) { - error = "请选择要搜索的群组"; - return; - } - if (!query && !islocal) { - error = "请输入搜索关键字"; - return; - } - error = ""; - our_hash_change = true; - console.log( - `searching ${query} for group ${group}, older than ${more}, from ${sender}` - ); - const q = new URLSearchParams(); - if (group) { - q.append("g", group); - } - if (query) { - q.append("q", query); - } - if (sender) { - q.append("sender", sender); - } - let url: RequestInfo | URL; - const qstr = q.toString(); - if (!more) { - location.hash = `#${qstr}`; - need_update_title = true; - if (result) { - result.messages = []; - } - url = `${LUOXU_URL}/search?${qstr}`; - } else { - url = `${LUOXU_URL}/search?${q}&end=${more}`; - } - - now = new Date(); - loading = true; + concurrency += 1; try { - const res = await fetch(url, { signal: abort.signal }); - const r = await res.json(); - loading = false; - if (abort.signal.aborted) { - return []; + abort.abort(); + abort = new AbortController(); + if (!group && !islocal) { + error = "请选择要搜索的群组"; + return; } - if (more) { - return r; + if (!query && !islocal) { + error = "请输入搜索关键字"; + return; + } + error = ""; + our_hash_change = true; + console.log( + `searching ${query} for group ${group}, older than ${more}, from ${sender}`, + ); + const q = new URLSearchParams(); + if (group) { + q.append("g", group); + } + if (query) { + q.append("q", query); + } + if (sender) { + q.append("sender", sender); + } + let url: RequestInfo | URL; + const qstr = q.toString(); + if (!more) { + location.hash = `#${qstr}`; + need_update_title = true; + if (result) { + result.messages = []; + } + url = `${LUOXU_URL}/search?${qstr}`; } else { - result = r; + url = `${LUOXU_URL}/search?${q}&end=${more}`; } - } catch (e) { - error = e; - loading = false; + + now = new Date(); + loading = true; + try { + const res = await fetch(url, { signal: abort.signal }); + const r = await res.json(); + loading = false; + if (abort.signal.aborted) { + return []; + } + if (more) { + return r; + } else { + result = r; + } + } catch (e) { + if (concurrency <= 1) { + error = e; + loading = false; + } + } + our_hash_change = false; + } finally { + concurrency -= 1; } - our_hash_change = false; } async function on_group_change() {