From 7d34c86f781eb14912bf902215b80983af8b351c Mon Sep 17 00:00:00 2001 From: Lawrence Lomax Date: Tue, 1 Feb 2022 04:55:51 -0800 Subject: [PATCH] Remove recording invariants Summary: The state of the destination and the process are coupled together. As a result it makes sense to move these to an object type. This prevents destination and process being independently settable. No change in behaviour, just a removal of invariants that need to be checked Reviewed By: passy Differential Revision: D33913213 fbshipit-source-id: ed18650cac4f72d40e82d20254674f7fa12cbb48 --- .../src/devices/ios/IOSDevice.tsx | 68 +++++++++---------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/desktop/flipper-server-core/src/devices/ios/IOSDevice.tsx b/desktop/flipper-server-core/src/devices/ios/IOSDevice.tsx index 6bba150c2..5a2a52a8a 100644 --- a/desktop/flipper-server-core/src/devices/ios/IOSDevice.tsx +++ b/desktop/flipper-server-core/src/devices/ios/IOSDevice.tsx @@ -18,8 +18,7 @@ import {iOSLogListener} from './iOSLogListener'; export default class IOSDevice extends ServerDevice { log?: child_process.ChildProcessWithoutNullStreams; buffer: string; - private recordingProcess?: ChildProcess; - private recordingLocation?: string; + private recording?: {process: ChildProcess; destination: string}; private iOSBridge: IOSBridge; readonly logListener: iOSLogListener; readonly crashWatcher: iOSCrashWatcher; @@ -82,47 +81,44 @@ export default class IOSDevice extends ServerDevice { } async startScreenCapture(destination: string) { - this.recordingProcess = this.iOSBridge.recordVideo( - this.serial, - destination, - ); - this.recordingLocation = destination; + const process = this.iOSBridge.recordVideo(this.serial, destination); + this.recording = {process, destination}; } async stopScreenCapture(): Promise { - if (this.recordingProcess && this.recordingLocation) { - const prom = new Promise((resolve, _reject) => { - this.recordingProcess!.on( - 'exit', - async (_code: number | null, _signal: NodeJS.Signals | null) => { - resolve(); - }, - ); - this.recordingProcess!.kill('SIGINT'); - }); - - const output: string = await timeout( - 5000, - prom, - 'Timed out to stop a screen capture.', - ) - .then(() => { - const {recordingLocation} = this; - this.recordingLocation = undefined; - return recordingLocation!; - }) - .catch((e) => { - this.recordingLocation = undefined; - console.warn('Failed to terminate iOS screen recording:', e); - throw e; - }); - return output; + const recording = this.recording; + if (!recording) { + throw new Error('No recording in progress'); } - throw new Error('No recording in progress'); + const prom = new Promise((resolve, _reject) => { + recording.process.on( + 'exit', + async (_code: number | null, _signal: NodeJS.Signals | null) => { + resolve(); + }, + ); + recording.process.kill('SIGINT'); + }); + + const output: string = await timeout( + 5000, + prom, + 'Timed out to stop a screen capture.', + ) + .then(() => { + this.recording = undefined; + return recording.destination; + }) + .catch((e) => { + this.recording = undefined; + console.warn('Failed to terminate iOS screen recording:', e); + throw e; + }); + return output; } disconnect() { - if (this.recordingProcess && this.recordingLocation) { + if (this.recording) { this.stopScreenCapture(); } super.disconnect();