fix(mobile): harden recorder, queue, context, and screen async lifecycles

This commit is contained in:
Yun Chan 2026-11-14 15:10:18 +09:00
parent 3f9e412257
commit bd3c1a98d9
25 changed files with 2279 additions and 68 deletions

View file

@ -44,6 +44,7 @@ export function createPlatformRecorderBackend(): RecorderBackend {
export class AudioRecorder {
private selectedBackend: RecorderBackend | null;
private lease: RecorderOwner | null = null;
private pendingClaim: { owner: RecorderOwner; depth: number } | null = null;
constructor(backend?: RecorderBackend) {
this.selectedBackend = backend ?? null;
@ -102,6 +103,9 @@ export class AudioRecorder {
}
private async assertAvailable(owner: RecorderOwner): Promise<void> {
if (this.pendingClaim !== null && this.pendingClaim.owner !== owner) {
throw new RecorderBusyError(this.pendingClaim.owner);
}
const holder = await this.holder();
if (holder !== null && holder !== owner) throw new RecorderBusyError(holder);
// Another owner may have started a capture while the status was read.
@ -115,9 +119,28 @@ export class AudioRecorder {
claim: boolean,
operation: () => Promise<T>,
): Promise<T> {
await this.assertAvailable(owner);
if (claim) this.lease = owner;
return operation();
if (this.pendingClaim !== null && this.pendingClaim.owner !== owner) {
throw new RecorderBusyError(this.pendingClaim.owner);
}
if (claim) {
this.pendingClaim = this.pendingClaim?.owner === owner
? { owner, depth: this.pendingClaim.depth + 1 }
: { owner, depth: 1 };
}
try {
await this.assertAvailable(owner);
if (claim) this.lease = owner;
return await operation();
} finally {
if (claim && this.pendingClaim?.owner === owner) {
if (this.pendingClaim.depth <= 1) {
this.pendingClaim = null;
} else {
this.pendingClaim = { owner, depth: this.pendingClaim.depth - 1 };
}
this.releaseIfIdle(owner);
}
}
}
private async cleanupAsOwner(