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

@ -104,6 +104,7 @@ export class NitroRecorderBackend implements RecorderBackend {
private state: NitroRecorderState = 'idle';
private currentPath: string | null = null;
private progress: RecordingProgress = { durationMs: 0, meteringDb: null };
private startGeneration = 0;
private pendingStart: Promise<void> | null = null;
private pendingTransition: Promise<void> | null = null;
private pendingStop: Promise<RecordedAudio> | null = null;
@ -144,14 +145,21 @@ export class NitroRecorderBackend implements RecorderBackend {
throw new Error(`Cannot start recorder while it is ${this.state}`);
}
const generation = ++this.startGeneration;
this.state = 'starting';
try {
await deleteStaleTemporaryFiles();
} catch (error) {
this.state = 'idle';
if (generation === this.startGeneration) {
this.state = 'idle';
}
throw error;
}
if (generation !== this.startGeneration) {
throw new Error('Recording start was cancelled');
}
const outputPath = createTemporaryPath();
this.currentPath = outputPath;
this.progress = { durationMs: 0, meteringDb: null };
@ -296,10 +304,15 @@ export class NitroRecorderBackend implements RecorderBackend {
}
async cancel(): Promise<void> {
this.startGeneration += 1;
if (this.state === 'starting' && this.pendingStart !== null) {
try {
await this.pendingStart;
} catch {
this.progressHandler = null;
this.currentPath = null;
this.state = 'idle';
this.progress = { durationMs: 0, meteringDb: null };
return;
}
}