mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 03:33:25 +00:00
* refactor(errors): read cleanup completion from dependencies instead of pushing it to dependents AnalysisError.cleanupIncomplete becomes a getter over the error's own unresolved state and its bound dependencies. The WeakRef dependents graph and resolveCleanup's push propagation are removed, and subclasses mark their initial state through markCleanupIncomplete instead of overriding a public mutable field. * fix(errors): settle bound dependencies before binding more A composite whose dependencies had released but whose status was never read stayed incomplete when a later dependency was bound. * refactor(errors): let errors read a lease group's release instead of resolving them (#10759) AnalysisCleanupLeaseGroup no longer calls resolveCleanup() on its error. The error holds the group as a release claim and reads cleanupReleased when asked, so completion follows the lease owner without a push. * chore(verify): count resolveCleanup and bindCleanupDependencies calls so they only shrink (#10763) * refactor(errors): let errors read a lease group's release instead of resolving them AnalysisCleanupLeaseGroup no longer calls resolveCleanup() on its error. The error holds the group as a release claim and reads cleanupReleased when asked, so completion follows the lease owner without a push. * chore(verify): count resolveCleanup and bindCleanupDependencies calls so they only shrink
145 lines
4.4 KiB
TypeScript
145 lines
4.4 KiB
TypeScript
import type {
|
|
AnalysisError,
|
|
AnalysisCleanupReleaseClaim,
|
|
AnalysisCleanupResourceOwner,
|
|
} from "../domain/analysisErrorBase.js";
|
|
|
|
/** Link one resource's confirmed release to its originating analysis error. */
|
|
export class AnalysisCleanupLease {
|
|
#released = false;
|
|
#releasePromise: Promise<void> | undefined;
|
|
readonly #groups = new Set<AnalysisCleanupLeaseGroup>();
|
|
|
|
/** Whether this exact owner has confirmed release. */
|
|
get released(): boolean {
|
|
return this.#released;
|
|
}
|
|
|
|
readonly #resourceOwner: AnalysisCleanupResourceOwner | undefined;
|
|
readonly #resources: readonly string[] | undefined;
|
|
|
|
get cleanupResources(): readonly string[] | undefined {
|
|
return this.#resourceOwner === undefined
|
|
? this.#resources
|
|
: this.#resourceOwner.cleanupResources;
|
|
}
|
|
|
|
/** Adopt exact close authority while retaining its live labels across retries. */
|
|
static forCloseOwner(
|
|
owner: {
|
|
close(): Promise<void>;
|
|
readonly cleanupResources?: readonly string[] | undefined;
|
|
},
|
|
resource: string,
|
|
): AnalysisCleanupLease {
|
|
return new AnalysisCleanupLease(
|
|
() => owner.close(),
|
|
!("cleanupResources" in owner)
|
|
? [resource]
|
|
: {
|
|
get cleanupResources() {
|
|
return owner.cleanupResources;
|
|
},
|
|
},
|
|
);
|
|
}
|
|
|
|
constructor(
|
|
private readonly cleanup: () => Promise<void>,
|
|
resources?: readonly string[] | AnalysisCleanupResourceOwner,
|
|
) {
|
|
if (resources !== undefined && "cleanupResources" in resources) {
|
|
this.#resourceOwner = resources;
|
|
this.#resources = undefined;
|
|
} else {
|
|
this.#resourceOwner = undefined;
|
|
this.#resources = resources === undefined ? undefined : [...resources];
|
|
}
|
|
}
|
|
|
|
release(): Promise<void> {
|
|
if (this.#released) return Promise.resolve();
|
|
if (this.#releasePromise !== undefined) return this.#releasePromise;
|
|
const attempt = Promise.resolve()
|
|
.then(async () => {
|
|
await this.cleanup();
|
|
this.#released = true;
|
|
for (const group of this.#groups) group.ownerReleased(this);
|
|
this.#groups.clear();
|
|
})
|
|
.finally(() => {
|
|
if (this.#releasePromise === attempt) this.#releasePromise = undefined;
|
|
});
|
|
this.#releasePromise = attempt;
|
|
return attempt;
|
|
}
|
|
|
|
/** Binding can follow a concurrent retry that already confirmed release. */
|
|
bindCleanupError(error: AnalysisError): void {
|
|
new AnalysisCleanupLeaseGroup(error, [this], {
|
|
preserveResourceAttribution: true,
|
|
});
|
|
}
|
|
|
|
/** Bind one aggregate error to every exact lease in a failed retry set. */
|
|
bindCleanupGroup(group: AnalysisCleanupLeaseGroup): void {
|
|
if (this.#released) {
|
|
group.ownerReleased(this);
|
|
return;
|
|
}
|
|
for (const retained of this.#groups)
|
|
if (!retained.hasLiveError) this.#groups.delete(retained);
|
|
this.#groups.add(group);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Conjunctive cleanup binding for an error covering multiple exact owners.
|
|
* The error reads `cleanupReleased` when asked; release is never pushed to it.
|
|
*/
|
|
export class AnalysisCleanupLeaseGroup implements AnalysisCleanupReleaseClaim {
|
|
readonly #error: WeakRef<AnalysisError>;
|
|
readonly #pending: Set<AnalysisCleanupLease>;
|
|
|
|
constructor(
|
|
error: AnalysisError,
|
|
owners: readonly AnalysisCleanupLease[],
|
|
options: { readonly preserveResourceAttribution?: boolean } = {},
|
|
) {
|
|
this.#error = new WeakRef(error);
|
|
this.#pending = new Set(owners);
|
|
if (this.#pending.size === 0) return;
|
|
error.bindCleanupReleaseClaim(this);
|
|
if (options.preserveResourceAttribution === true)
|
|
error.bindCleanupCompletionOwner(this);
|
|
else error.bindCleanupResourceOwner(this);
|
|
for (const owner of this.#pending) owner.bindCleanupGroup(this);
|
|
}
|
|
|
|
/** Unlabelled owners keep attribution unknown; never match resources by text. */
|
|
get cleanupResources(): readonly string[] | undefined {
|
|
if (
|
|
[...this.#pending].some((owner) => owner.cleanupResources === undefined)
|
|
)
|
|
return undefined;
|
|
return [
|
|
...new Set(
|
|
[...this.#pending].flatMap((owner) => owner.cleanupResources ?? []),
|
|
),
|
|
];
|
|
}
|
|
|
|
get hasLiveError(): boolean {
|
|
return this.#error.deref() !== undefined;
|
|
}
|
|
|
|
/** Every owner in the set confirmed release. */
|
|
get cleanupReleased(): boolean {
|
|
return this.#pending.size === 0;
|
|
}
|
|
|
|
ownerReleased(owner: AnalysisCleanupLease): void {
|
|
this.#pending.delete(owner);
|
|
}
|
|
}
|