Skip to content

Commit

Permalink
Remove delay before recording starts (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus authored Sep 30, 2020
1 parent ec0df07 commit 10d447c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
8 changes: 6 additions & 2 deletions Sources/ApertureCLI/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ struct Options: Decodable {
}

func record() throws {
setbuf(__stdoutp, nil)

let options: Options = try CLI.arguments.first!.jsonDecoded()

let recorder = try Aperture(
Expand All @@ -28,7 +30,7 @@ func record() throws {
)

recorder.onStart = {
print("R")
print("FR")
}

recorder.onFinish = {
Expand All @@ -48,7 +50,9 @@ func record() throws {

recorder.start()

setbuf(__stdoutp, nil)
// Inform the Node.js code that the recording has started.
print("R")

RunLoop.main.run()
}

Expand Down
2 changes: 2 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ async function main() {
console.log('Preparing to record for 5 seconds');
await recorder.startRecording();
console.log('Recording started');
await recorder.isFileReady;
console.log('File is ready');
await delay(5000);
const fp = await recorder.stopRecording();
fs.renameSync(fp, 'recording.mp4');
Expand Down
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,25 @@ class Aperture {
reject(error);
});

this.isFileReady = new Promise(resolve => {
this._fileReadyResolve = resolve;
});

this.recorder.stdout.setEncoding('utf8');
this.recorder.stdout.on('data', data => {
debuglog(data);

if (data.trim() === 'R') {
// `R` is printed by Swift when the recording **actually** starts
const trimmed = data.trim();

if (trimmed === 'R') {
// `R` is printed by Swift about a second before the recording **actually** starts
clearTimeout(timeout);
resolve(this.tmpPath);
setTimeout(resolve, 1000);
} else if (trimmed === 'FR') {
// `FR` is printed by Swift when the the recording file is ready
if (this._fileReadyResolve) {
this._fileReadyResolve(this.tmpPath);
}
}
});
});
Expand All @@ -139,6 +150,8 @@ class Aperture {
this.recorder.kill();
await this.recorder;
delete this.recorder;
delete this._fileReadyResolve;
delete this.isFileReady;

return this.tmpPath;
}
Expand Down
10 changes: 8 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,15 @@ Map {

#### recorder.startRecording([[options]](#options))

Returns a `Promise` for the path to the screen recording file.
Returns a `Promise` that fullfills when the recording starts or rejects if the recording didn't start after 5 seconds.

#### recorder.isFileReady

`Promise` that fullfills with the path to the screen recording file when it's ready. This will never reject.

Only available while a recording is happening, `undefined` otherwise.

Fullfills when the recording starts or rejects if the recording didn't start after 5 seconds.
Usually, this resolves around 1 second before the recording starts, but that's not guaranteed.

#### recorder.stopRecording()

Expand Down
3 changes: 2 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ test('returns available video codecs', t => {

test('records screen', async t => {
const recorder = aperture();
t.true(fs.existsSync(await recorder.startRecording()));
await recorder.startRecording();
t.true(fs.existsSync(await recorder.isFileReady));
await delay(1000);
const videoPath = await recorder.stopRecording();
t.true(fs.existsSync(videoPath));
Expand Down

0 comments on commit 10d447c

Please sign in to comment.