diff --git a/web_src/js/components/ActionRunJobView.vue b/web_src/js/components/ActionRunJobView.vue index 2092d9ce20b..96635e27d37 100644 --- a/web_src/js/components/ActionRunJobView.vue +++ b/web_src/js/components/ActionRunJobView.vue @@ -4,7 +4,7 @@ import SvgIcon from './SvgIcon.vue'; import ActionStatusIcon from './ActionStatusIcon.vue'; import {addDelegatedEventListener, createElementFromAttrs} from '../utils/dom.ts'; import {formatDatetime, formatDatetimeISO} from '../utils/time.ts'; -import {POST} from '../modules/fetch.ts'; +import {ExclusiveFetch, request} from '../modules/fetch.ts'; import {createTippy} from '../modules/tippy.ts'; import {copyToClipboardWithFeedback} from '../modules/clipboard.ts'; import type {Instance} from 'tippy.js'; @@ -16,6 +16,7 @@ import {AnsiLineRenderer} from '../render/ansi.ts'; import {createLogLineMessage, parseLogLineCommand} from './ActionRunView.ts'; import type {ActionRunViewStore, LogLine, LogLineCommand} from './ActionRunView.ts'; import {registerAsyncHashChangeListener, removeAsyncHashChangeListener} from '../modules/hashchange.ts'; +import {showErrorToast} from '../modules/toast.ts'; function isLogElementInViewport(el: Element, {extraViewPortHeight} = {extraViewPortHeight: 0}): boolean { const rect = el.getBoundingClientRect(); @@ -113,7 +114,7 @@ const { } = localUserSettings.getJsonObject('actions-view-options', defaultViewOptions); // internal state -let loadingAbortController: AbortController | null = null; +const exclusiveFetch = new ExclusiveFetch(); let intervalID: IntervalId | null = null; const currentJobStepsStates = ref>([]); @@ -162,6 +163,7 @@ onMounted(async () => { // load job data and then auto-reload periodically // need to await first loadJob so this.currentJobStepsStates is initialized and can be used in hashChangeListener + intervalID = setInterval(() => loadJob(), 1000); await loadJob(); // auto-scroll to the bottom of the log group when it is opened @@ -176,19 +178,19 @@ onMounted(async () => { }, 0); }); - intervalID = setInterval(() => loadJob(), 1000); registerAsyncHashChangeListener(hashChangeListener); }); +function stopPollingCurrentRun() { + if (!intervalID) return; + clearInterval(intervalID); + intervalID = null; +} + onBeforeUnmount(() => { menuTippy.destroy(); removeAsyncHashChangeListener(hashChangeListener); - // clear the interval timer when the component is unmounted - // even our page is rendered once, not spa style - if (intervalID) { - clearInterval(intervalID); - intervalID = null; - } + stopPollingCurrentRun(); }); function saveLocaleStorageOptions() { @@ -255,7 +257,7 @@ async function copyStepOutput(event: MouseEvent, stepIndex: number) { function toggleStepLogs(idx: number) { currentJobStepsStates.value[idx].expanded = !currentJobStepsStates.value[idx].expanded; if (currentJobStepsStates.value[idx].expanded) { - loadJobForce(); // try to load the data immediately instead of waiting for next timer interval + loadJob(true); // try to load the data immediately instead of waiting for next timer interval } else if (currentJob.value.steps[idx].status === 'running') { currentJobStepsStates.value[idx].manuallyCollapsed = true; } @@ -309,26 +311,21 @@ function appendLogs(stepIndex: number, startTime: number, logLines: LogLine[]) { // For example: backend can make cursor=null means the first time to fetch logs, cursor=1234 for a position, cursor=eof for no more logs, etc. type LogCursor = {step: number, cursor: any, expanded: boolean}; -async function fetchJobData(logCursors: LogCursor[], signal?: AbortSignal): Promise { - const resp = await POST(props.actionsViewUrl, {signal, data: {logCursors}}); +async function fetchJobData(logCursors: LogCursor[]): Promise { + const resp = await request(props.actionsViewUrl, {method: 'POST', data: {logCursors}}); return await resp.json(); } -async function loadJobForce() { - loadingAbortController?.abort(); - loadingAbortController = null; - await loadJob(); -} - -async function loadJob() { - if (loadingAbortController) return; - const abortController = new AbortController(); - loadingAbortController = abortController; - try { - const logCursors = currentJobStepsStates.value.map((it, idx) => ({step: idx, cursor: it.cursor, expanded: it.expanded})); - const runJobResp = await fetchJobData(logCursors, abortController.signal); - if (loadingAbortController !== abortController) return; +async function loadJob(force = false) { + if (exclusiveFetch.isInFlight() && !force) return; + const logCursors = currentJobStepsStates.value.map((it, idx) => ({step: idx, cursor: it.cursor, expanded: it.expanded})); + const resp = exclusiveFetch.request(props.actionsViewUrl, {method: 'POST', data: {logCursors}}); + resp.onResponseError((details) => { + if (details.response?.status === 404) stopPollingCurrentRun(); + showErrorToast(`Failed to load job data: ${details.message}`, {duration: 2500}); + }); + await resp.fetchJson(async (runJobResp: JobData) => { // FIXME: this logic is quite hacky and dirty, it should be refactored in a better way in the future // Use consistent "store" operations to load/update the view data store.viewData.runArtifacts = runJobResp.artifacts || []; @@ -385,18 +382,9 @@ async function loadJob() { lastLogElem.scrollIntoView({behavior: 'smooth', block: 'end'}); } - // clear the interval timer if the job is done, the logs of changed steps still need to be loaded - if (run.value.done && intervalID && !stepsChanged) { - clearInterval(intervalID); - intervalID = null; - } - } catch (e) { - // avoid network error while unloading page, and ignore "abort" error - if (e instanceof TypeError || abortController.signal.aborted) return; - throw e; - } finally { - if (loadingAbortController === abortController) loadingAbortController = null; - } + // even if the run is done, we still need to load more data if the steps changed + if (run.value.done && !stepsChanged) stopPollingCurrentRun(); + }); } function isSameSteps(a: Array, b: Array) { diff --git a/web_src/js/components/ActionRunView.ts b/web_src/js/components/ActionRunView.ts index f9b3aebd564..6832142b7cd 100644 --- a/web_src/js/components/ActionRunView.ts +++ b/web_src/js/components/ActionRunView.ts @@ -3,7 +3,8 @@ import type {AnsiLineRenderer} from '../render/ansi.ts'; import {reactive} from 'vue'; import type {ActionsArtifact, ActionsJob, ActionsRun, ActionsStatus} from '../modules/gitea-actions.ts'; import type {IntervalId} from '../types.ts'; -import {POST} from '../modules/fetch.ts'; +import {ExclusiveFetch} from '../modules/fetch.ts'; +import {showErrorToast} from '../modules/toast.ts'; // How GitHub Actions logs work: // * Workflow command outputs log commands like "::group::the-title", "::add-matcher::...." @@ -161,54 +162,44 @@ export function createEmptyActionsRun(): ActionsRun { } export function createActionRunViewStore(viewUrl: string) { - let loadingAbortController: AbortController | null = null; let intervalID: IntervalId | null = null; + const exclusiveFetch = new ExclusiveFetch(); const viewData = reactive({ currentRun: createEmptyActionsRun(), runArtifacts: [] as Array, }); - const loadCurrentRun = async () => { - if (loadingAbortController) return; - const abortController = new AbortController(); - loadingAbortController = abortController; - try { - const resp = await POST(viewUrl, {signal: abortController.signal, data: {}}); - const runResp = await resp.json(); - if (loadingAbortController !== abortController) return; + const stopPollingCurrentRun = () => { + if (!intervalID) return; + clearInterval(intervalID); + intervalID = null; + }; + + const loadCurrentRun = async (force = false) => { + if (exclusiveFetch.isInFlight() && !force) return; + const resp = exclusiveFetch.request(viewUrl, {method: 'POST', data: {}}); + resp.onResponseError((details) => { + if (details.response?.status === 404) stopPollingCurrentRun(); + showErrorToast(`Failed to load current job run data: ${details.message}`, {duration: 2500}); + }); + await resp.fetchJson((runResp: any) => { viewData.runArtifacts = runResp.artifacts || []; viewData.currentRun = runResp.state.run; - // clear the interval timer if the job is done - if (viewData.currentRun.done && intervalID) { - clearInterval(intervalID); - intervalID = null; - } - } catch (e) { - // avoid network error while unloading page, and ignore "abort" error - if (e instanceof TypeError || abortController.signal.aborted) return; - throw e; - } finally { - if (loadingAbortController === abortController) loadingAbortController = null; - } + if (viewData.currentRun.done) stopPollingCurrentRun(); + }); }; return { viewData, async startPollingCurrentRun() { - await loadCurrentRun(); intervalID = setInterval(() => loadCurrentRun(), 1000); + await loadCurrentRun(); }, async forceReloadCurrentRun() { - loadingAbortController?.abort(); - loadingAbortController = null; - await loadCurrentRun(); - }, - stopPollingCurrentRun() { - if (!intervalID) return; - clearInterval(intervalID); - intervalID = null; + await loadCurrentRun(true); }, + stopPollingCurrentRun, }; } diff --git a/web_src/js/features/comp/ComboMarkdownEditor.ts b/web_src/js/features/comp/ComboMarkdownEditor.ts index 328a5f9ed87..5cbf707359b 100644 --- a/web_src/js/features/comp/ComboMarkdownEditor.ts +++ b/web_src/js/features/comp/ComboMarkdownEditor.ts @@ -269,7 +269,7 @@ export class ComboMarkdownEditor { formData.append('context', this.previewContext); formData.append('text', this.value()); const resp = this.previewFetch.request(this.previewUrl, {method: 'POST', data: formData}); - resp.onResponseError((resp) => showErrorToast(`Failed to render preview: ${resp.status} ${resp.statusText}`)); + resp.onResponseError((details) => showErrorToast(`Failed to render preview: ${details.message}`, {duration: 2500})); await resp.fetchText(renderPreview); }, {capture: true}); } diff --git a/web_src/js/features/comp/TextExpander.ts b/web_src/js/features/comp/TextExpander.ts index 7b5a95450dd..89a614ef757 100644 --- a/web_src/js/features/comp/TextExpander.ts +++ b/web_src/js/features/comp/TextExpander.ts @@ -1,23 +1,28 @@ -import {matchEmoji, matchMention, matchIssue} from '../../utils/match.ts'; +import {matchEmoji, matchMention} from '../../utils/match.ts'; import {emojiString} from '../emoji.ts'; import {svg} from '../../svg.ts'; import {parseIssueHref, parseRepoOwnerPathInfo} from '../../utils.ts'; import {createElementFromAttrs, createElementFromHTML} from '../../utils/dom.ts'; import {getIssueColorClass, getIssueIcon} from '../issue.ts'; -import {errorName} from '../../modules/errors.ts'; import {debounce} from '../../utils/func.ts'; import type TextExpanderElement from '@github/text-expander-element'; import type {TextExpanderChangeEvent, TextExpanderResult} from '@github/text-expander-element'; +import {ExclusiveFetch} from '../../modules/fetch.ts'; +import type {Issue} from '../../types.ts'; -async function fetchIssueSuggestions(key: string, text: string, signal: AbortSignal): Promise { +function makeFetchIssueSuggestionsUrl(query: string): {issueIndexStr: string, suggestionsUrl: string} | null { const hrefPathInfo = parseIssueHref(window.location.href); // the fallback has no indexString, it is only used to exclude the current issue when "matchIssue" const pathInfo = hrefPathInfo ?? parseRepoOwnerPathInfo(window.location.pathname); - if (!pathInfo) return {matched: false}; - - const matches = await matchIssue(pathInfo.ownerName, pathInfo.repoName, hrefPathInfo?.indexString, text, signal); - if (!matches.length) return {matched: false}; + if (!pathInfo) return null; + return { + issueIndexStr: hrefPathInfo?.indexString ?? '', + suggestionsUrl: `${window.config.appSubUrl}/${pathInfo.ownerName}/${pathInfo.repoName}/issues/suggestions?q=${encodeURIComponent(query)}`, + }; +} +function buildIssueSuggestions(key: string, matches: Issue[]): TextExpanderResult { + if (matches.length === 0) return {matched: false}; const ul = createElementFromAttrs('ul', {class: 'suggestions'}); for (const issue of matches) { const li = createElementFromAttrs( @@ -45,28 +50,23 @@ export function initTextExpander(expander: TextExpanderElement) { return keyStart > lineStart; }; - let suggestionsController = new AbortController(); + const exclusiveFetch = new ExclusiveFetch(); const debouncedIssueSuggestions = debounce(async (key: string, text: string): Promise => { - // https://github.com/github/text-expander-element/issues/71 - // Upstream bug: when using "multiword+promise", TextExpander will get wrong "key" position. - // To reproduce, comment out the "shouldShowIssueSuggestions" check, use the "await sleep" below, - // then use content "close #20\nclose #20\nclose #20" (3 lines), keep changing the last line `#20` part from the end (including removing the `#`) - // There will be a JS error: Uncaught (in promise) IndexSizeError: Failed to execute 'setStart' on 'Range': The offset 28 is larger than the node's length (27). - - // check the input before the request, to avoid emitting empty query to backend (still related to the upstream bug) + // check the input before the request, to avoid emitting empty query to backend if (!shouldShowIssueSuggestions()) return {matched: false}; - // await sleep(Math.random() * 1000); // help to reproduce the text-expander bug - suggestionsController.abort(); // only the newest request may answer - suggestionsController = new AbortController(); - try { - const ret = await fetchIssueSuggestions(key, text, suggestionsController.signal); - // check the input again to avoid text-expander using incorrect position (upstream bug) + + const result = makeFetchIssueSuggestionsUrl(text); + if (!result) return {matched: false}; + const {issueIndexStr, suggestionsUrl} = result; + + const resp = exclusiveFetch.request(suggestionsUrl); + return await resp.fetchJson((raw: Issue[]) => { if (!shouldShowIssueSuggestions()) return {matched: false}; - return ret; - } catch (err) { - if (errorName(err) !== 'AbortError') throw err; - return {matched: false}; - } + + const issueNumber = parseInt(issueIndexStr || ''); + const issues = raw.filter((i) => i.number !== issueNumber); + return buildIssueSuggestions(key, issues); + }) || {matched: false}; }, 300); // to match onInputDebounce delay expander.addEventListener('text-expander-change', (e: TextExpanderChangeEvent) => { diff --git a/web_src/js/features/repo-editor.ts b/web_src/js/features/repo-editor.ts index 49ce7ed2290..d843362a515 100644 --- a/web_src/js/features/repo-editor.ts +++ b/web_src/js/features/repo-editor.ts @@ -38,7 +38,7 @@ function initEditPreviewTab(elForm: HTMLFormElement) { formData.append('text', elTextarea.value); formData.append('file_path', elTreePath.value); const resp = previewFetch.request(`${repoLink}/markup`, {method: 'POST', data: formData}); - resp.onResponseError((resp) => showErrorToast(`Failed to render preview: ${resp.status} ${resp.statusText}`)); + resp.onResponseError((details) => showErrorToast(`Failed to render preview: ${details.message}`, {duration: 2500})); await resp.fetchText((data) => renderPreviewPanelContent(elPreviewPanel, data)); }); @@ -50,7 +50,7 @@ function initEditPreviewTab(elForm: HTMLFormElement) { const diffUrl = `${repoLink}/_preview/${pathEscapeSegments(branchName)}/${pathEscapeSegments(elTreePath.value)}`; // don't use FormData, because FormData sends "\r\n" line endings, backend assumes "\n" line endings const resp = diffFetch.request(diffUrl, {method: 'POST', data: new URLSearchParams({content: elTextarea.value})}); - resp.onResponseError((resp) => showErrorToast(`Failed to render diff: ${resp.status} ${resp.statusText}`)); + resp.onResponseError((details) => showErrorToast(`Failed to render diff: ${details.message}`, {duration: 2500})); await resp.fetchText((data) => elDiffPanel.innerHTML = data); }); } diff --git a/web_src/js/markup/tasklist.ts b/web_src/js/markup/tasklist.ts index 1cc5bd1ce64..935f838fec5 100644 --- a/web_src/js/markup/tasklist.ts +++ b/web_src/js/markup/tasklist.ts @@ -83,7 +83,7 @@ export function initMarkupTasklist(elMarkup: HTMLElement): void { rawContent.textContent = newContent; } catch (err) { checkbox.checked = !checkbox.checked; - console.error(err); + console.error('exception occurred when updating markup task list', err); } // Enable input on checkboxes again diff --git a/web_src/js/modules/fetch.test.ts b/web_src/js/modules/fetch.test.ts index d8fe7841c5d..2122dbf0bfb 100644 --- a/web_src/js/modules/fetch.test.ts +++ b/web_src/js/modules/fetch.test.ts @@ -25,8 +25,10 @@ test('ExclusiveFetch discards a superseded response while reading its body', asy const exclusiveFetch = new ExclusiveFetch(); const render = vi.fn(); const oldRequest = exclusiveFetch.request('/old').fetchText(render); + expect(exclusiveFetch.isInFlight()).toBe(true); await bodyStarted.promise; await exclusiveFetch.request('/new').fetchText(render); + expect(exclusiveFetch.isInFlight()).toBe(false); oldBody.resolve('old'); await oldRequest; diff --git a/web_src/js/modules/fetch.ts b/web_src/js/modules/fetch.ts index 5caca16e58e..e14b6ec98b6 100644 --- a/web_src/js/modules/fetch.ts +++ b/web_src/js/modules/fetch.ts @@ -1,5 +1,6 @@ import {isObject} from '../utils.ts'; import type {RequestOpts} from '../types.ts'; +import isNetworkError from 'is-network-error'; // fetch wrapper, use below method name functions and the `data` option to pass in data // which will automatically set an appropriate headers. For JSON content, only object @@ -36,7 +37,7 @@ class ExclusiveResponse { private readonly promiseResp: Promise; private readonly exclusiveFetch: ExclusiveFetch; private readonly abortController: AbortController; - private onResponseErrorFn: ((resp: Response) => void) | null = null; + private onResponseErrorFn: ((details: {message: string, response?: Response}) => void) | null = null; constructor(exclusiveFetch: ExclusiveFetch, abortController: AbortController, promiseResp: Promise) { this.exclusiveFetch = exclusiveFetch; @@ -48,36 +49,44 @@ class ExclusiveResponse { return this.abortController.signal.aborted || this.exclusiveFetch.currentController !== this.abortController; } - // fetchGeneric is designed for future usages like "resp.json()", "resp.blob()", etc, they share the same "exclusive" logic - private async fetchGeneric(readResp: (resp: Response) => Promise, userFn: (ret: T) => void): Promise { + private async fetchGeneric(readResp: (resp: Response) => Promise, callback: (ret: T) => R | Promise) : Promise { try { const resp = await this.promiseResp; - if (this.isAborted()) return false; + if (this.isAborted()) return undefined; if (!resp.ok) { - this.onResponseErrorFn?.(resp); - return false; + this.onResponseErrorFn?.({message: `http: ${resp.status} ${resp.statusText}`, response: resp}); + return undefined; } const ret = await readResp(resp); - if (this.isAborted()) return false; - userFn(ret); - return true; + if (this.isAborted()) return undefined; + return await callback(ret); } catch (err) { - if (this.isAborted()) return false; + // avoid network error while unloading page, and ignore "abort" error + if (isNetworkError(err) || this.isAborted()) return undefined; + // let caller handle callback function's exceptions throw err; + } finally { + if (this.exclusiveFetch.currentController === this.abortController) { + this.exclusiveFetch.currentController = null; + } } } - // onResponseError allows the user to set a callback function that will be called when the response is not ok (e.g., 4xx or 5xx status codes). + // onResponseError allows the caller to set a callback function that will be called when the response is not ok (e.g., 4xx or 5xx status codes). // The callback is only called when the request is not-ok and not aborted (the request is the last one). - onResponseError(fn: (resp: Response) => void) { + onResponseError(fn: (details: {message: string, response?: Response}) => void) { this.onResponseErrorFn = fn; } // fetchText reads the response as text and passes it to the provided callback function. // The callback is only called when the request is successful and not aborted (the request is the last one). - async fetchText(fn: (text: string) => void): Promise { + async fetchText(fn: (text: string) => R | Promise): Promise { return this.fetchGeneric((resp) => resp.text(), fn); } + + async fetchJson(fn: (text: T) => R | Promise): Promise { + return this.fetchGeneric((resp) => resp.json(), fn); + } } // ExclusiveFetch is used for fetching data where only the latest request is relevant. @@ -96,4 +105,8 @@ export class ExclusiveFetch { this.currentController = controller; return new ExclusiveResponse(this, controller, request(url, {method, data, headers, ...other, signal: controller.signal})); } + + isInFlight(): boolean { + return this.currentController !== null; + } } diff --git a/web_src/js/utils/match.ts b/web_src/js/utils/match.ts index 7fdd85eec85..63b2f6478cf 100644 --- a/web_src/js/utils/match.ts +++ b/web_src/js/utils/match.ts @@ -3,7 +3,7 @@ import {GET} from '../modules/fetch.ts'; import {showErrorToast} from '../modules/toast.ts'; import {parseIssuePageInfo} from '../utils.ts'; import {errorMessage} from '../modules/errors.ts'; -import type {Issue, Mention} from '../types.ts'; +import type {Mention} from '../types.ts'; const maxMatches = 6; @@ -70,13 +70,3 @@ export async function matchMention(mentionsUrl: string, queryText: string): Prom return sortAndReduce(results); } - -export async function matchIssue(owner: string, repo: string, issueIndexStr: string | undefined, query: string, signal: AbortSignal): Promise { - const res = await GET(`${window.config.appSubUrl}/${owner}/${repo}/issues/suggestions?q=${encodeURIComponent(query)}`, {signal}); - - const issues: Issue[] = await res.json(); - const issueNumber = parseInt(issueIndexStr || ''); - - // filter out issue with same id - return issues.filter((i) => i.number !== issueNumber); -}