Files
reverse-engineering-anything/src/application/desktop/DesktopBuildTurns.ts
T
morluto 2826592759 refactor(application): build application cancellations with a required reason (#10574)
* refactor(application): build application cancellations with a required reason

Migrate every AnalysisCancelledError construction under src/application
to cancelledBy and lower the guard baseline from 671 to 442.

Shutdown refusals in BinarySession report OwnerCancellation.ownerClosed
when the caller did not abort, instead of an undefined reason.
ReverseEngineeringService and DesktopBuildOwnedWork close their work
with the typed owner reason. SharedStartupAttempt returns the departing
caller's reason with a cancelled wait. One shared isAborted predicate
replaces eleven local copies and narrows a checked signal for
cancelledBy.

* refactor(desktop): drop an unused cancellation import from desktop capture state
2026-11-15 18:38:24 +08:00

71 lines
2.4 KiB
TypeScript

import type { AnalysisError } from "../../domain/analysisErrorBase.js";
import { cancelledBy } from "../../domain/analysisErrorCore.js";
import { err, type Result } from "../../domain/result.js";
import {
AbortedWait,
waitForAbortable,
} from "../../process/AbortablePromise.js";
/**
* One build computer's calls, each run alone in the order it arrived, so a
* witness stage is admitted, read and filed with no action or other step in
* between. Closing cancels the call running and refuses those waiting.
*/
export class DesktopBuildTurns {
#tail: Promise<void> = Promise.resolve();
readonly #closing = new AbortController();
/**
* Run `work` once every call before it has ended; its signal also aborts
* when the computer closes. What `work` returns is the call's result, even
* if its turn aborted after the work committed what that result reports; a
* step that commits nothing checks its signal before succeeding.
*/
async run<Value>(
operation: string,
signal: AbortSignal,
work: (signal: AbortSignal) => Promise<Result<Value, AnalysisError>>,
): Promise<Result<Value, AnalysisError>> {
const turn = AbortSignal.any([signal, this.#closing.signal]);
const { previous, release } = this.#enqueue();
if (
(await waitForAbortable(previous, turn)) instanceof AbortedWait ||
turn.aborted
) {
// A call that never ran keeps its place until the one before it ends, so the next still waits for that one.
void previous.then(release);
return err(cancelledBy(operation, turn));
}
try {
return await work(turn);
} finally {
release();
}
}
/** Cancel the call running and those waiting, then run `work` once the running one has ended. */
async close<Value>(work: () => Promise<Value>): Promise<Value> {
this.#closing.abort(new Error("The build computer was closed."));
const { previous, release } = this.#enqueue();
await previous;
try {
return await work();
} finally {
release();
}
}
/** Take the next place: the end of the call before it, and the release of this one. */
#enqueue(): {
readonly previous: Promise<void>;
readonly release: () => void;
} {
const previous = this.#tail;
let release = (): void => undefined;
this.#tail = new Promise<void>((resolve) => {
release = resolve;
});
return { previous, release };
}
}