perf(browser-files): wait on picker aborts through the kernel operation (R3-2)
The native-input wait and the system-picker wait no longer add their own abort listeners; both use createAbortableOperation, and dispose closes the operation. Results stay ABORTED for caller abort and for dispose. Adds a characterization test for dispose while the enhanced picker is pending. Hand-rolled abort ratchet 16 -> 15. file-transfer gzip: 54500 -> 54499 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
ce03745b66
commit
a58197844b
@@ -23,6 +23,7 @@ import {
|
||||
} from "./file-observer.ts";
|
||||
import type { RegisteredFileSelectionPolicy } from "./file-policy.ts";
|
||||
import { BrowserFilePolicyRegistry } from "./browser-file-policy-registry.ts";
|
||||
import { createAbortableOperation } from "../platform/abortable-operation.ts";
|
||||
|
||||
type UserActivationState = Readonly<{ isActive: boolean }>;
|
||||
|
||||
@@ -260,6 +261,8 @@ export class NativeInputFilePicker implements DisposableFilePicker {
|
||||
let settled = false;
|
||||
let focusTimer: unknown;
|
||||
const signal = request.signal;
|
||||
// The kernel operation owns the caller signal listener.
|
||||
const operation = createAbortableOperation({ signal });
|
||||
const finish = (
|
||||
outcome: BrowserDataResult<FileSelectionOutcome>,
|
||||
): void => {
|
||||
@@ -267,7 +270,7 @@ export class NativeInputFilePicker implements DisposableFilePicker {
|
||||
settled = true;
|
||||
this.#inputEvents.remove("change", onChange);
|
||||
this.#inputEvents.remove("cancel", onCancel);
|
||||
signal?.removeEventListener("abort", onAbort);
|
||||
operation.close();
|
||||
this.#windowEvents?.remove("focus", onWindowFocus);
|
||||
if (focusTimer !== undefined) {
|
||||
this.#scheduler.clearTimeout(focusTimer);
|
||||
@@ -309,8 +312,6 @@ export class NativeInputFilePicker implements DisposableFilePicker {
|
||||
this.#nativeCancelObserved = true;
|
||||
dismiss();
|
||||
};
|
||||
const onAbort = (): void =>
|
||||
finish(browserDataFailure("ABORTED", "FILE_SELECT"));
|
||||
const abortPending = (): void =>
|
||||
finish(browserDataFailure("ABORTED", "FILE_SELECT"));
|
||||
const onWindowFocus = (): void => {
|
||||
@@ -333,7 +334,8 @@ export class NativeInputFilePicker implements DisposableFilePicker {
|
||||
|
||||
this.#inputEvents.add("change", onChange, { once: true });
|
||||
this.#inputEvents.add("cancel", onCancel, { once: true });
|
||||
signal?.addEventListener("abort", onAbort, { once: true });
|
||||
// Settles only when the operation ends; after `finish` this is a no-op.
|
||||
void operation.race(new Promise<never>(() => {})).then(abortPending);
|
||||
if (!this.#nativeCancelObserved) {
|
||||
this.#windowEvents?.add("focus", onWindowFocus);
|
||||
}
|
||||
@@ -566,29 +568,20 @@ export class EnhancedFilePicker implements DisposableFilePicker {
|
||||
new DOMException("Picker aborted", "AbortError"),
|
||||
);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
let settled = false;
|
||||
const finish = (callback: () => void): void => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
signal?.removeEventListener("abort", onAbort);
|
||||
if (this.#abortPending === abortPending) {
|
||||
this.#abortPending = undefined;
|
||||
}
|
||||
callback();
|
||||
};
|
||||
const abortPending = (): void =>
|
||||
finish(() => reject(new PickerDisposedError()));
|
||||
const onAbort = (): void =>
|
||||
finish(() =>
|
||||
reject(new DOMException("Picker aborted", "AbortError")),
|
||||
);
|
||||
this.#abortPending = abortPending;
|
||||
signal?.addEventListener("abort", onAbort, { once: true });
|
||||
picker.then(
|
||||
(handles) => finish(() => resolve(handles)),
|
||||
(error: unknown) => finish(() => reject(error)),
|
||||
);
|
||||
const operation = createAbortableOperation({ signal });
|
||||
const abortPending = (): void => operation.close();
|
||||
this.#abortPending = abortPending;
|
||||
return operation.race(picker).then((outcome) => {
|
||||
if (this.#abortPending === abortPending) {
|
||||
this.#abortPending = undefined;
|
||||
}
|
||||
operation.close();
|
||||
if (outcome.kind === "VALUE") return outcome.value;
|
||||
throw outcome.kind === "REJECTED"
|
||||
? outcome.reason
|
||||
: outcome.terminal === "CALLER_ABORT"
|
||||
? new DOMException("Picker aborted", "AbortError")
|
||||
: new PickerDisposedError();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user