Files
reverse-engineering-anything/src/application/apiContract/ApiContractTraversal.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

45 lines
1.6 KiB
TypeScript

import { setImmediate } from "node:timers/promises";
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
import { cancelledBy, carriedReason } from "../../domain/analysisErrorCore.js";
import { err, type Result } from "../../domain/result.js";
import { API_CONTRACT_OPERATION as operation } from "../../domain/apiContract/apiContract.js";
/** Let a producer retain already interpreted facts before releasing its provisional state. */
const cancelSteps = <Value>(
steps: Generator<void, Result<Value, AnalysisError>>,
reason: unknown,
): Result<Value, AnalysisError> => {
const cancellation = cancelledBy(operation, carriedReason(reason));
try {
const stopped = steps.throw(cancellation);
if (stopped.done && !stopped.value.ok) return stopped.value;
} catch (cause) {
if (cause !== cancellation) throw cause;
}
return err(cancellation);
};
/** Cooperative traversals release their provisional state on cancellation. */
export const completeApiContractSteps = async <Value>(
steps: Generator<void, Result<Value, AnalysisError>>,
signal?: AbortSignal,
): Promise<Result<Value, AnalysisError>> => {
let completed = false;
try {
for (;;) {
if (signal?.aborted) return cancelSteps(steps, signal.reason);
const next = steps.next();
if (next.done) {
completed = true;
if (!next.value.ok) return next.value;
// The Evidence owner classifies queued late cancellation with completed facts.
await setImmediate();
return next.value;
}
await setImmediate();
}
} finally {
if (!completed) steps.return(undefined as never);
}
};