Skip to content

Commit 7856f92

Browse files
Alexandre Lissyreuben
Alexandre Lissy
authored andcommitted
Ensure proper termination for ElectronJS and NodeJS
1 parent a74e48b commit 7856f92

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

native_client/javascript/client.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ if (result.sampleRate < desired_sample_rate) {
8989
`Up-sampling might produce erratic speech recognition.`);
9090
}
9191

92+
function handleExit() {
93+
if (process.versions.electron) {
94+
const { app } = require("electron");
95+
app.quit();
96+
} else {
97+
process.exit(0);
98+
}
99+
}
100+
92101
function bufferToStream(buffer: Buffer) {
93102
var stream = new Duplex();
94103
stream.push(buffer);
@@ -135,7 +144,14 @@ if (!args['stream']) {
135144
const inference_stop = process.hrtime(inference_start);
136145
console.error('Inference took %ds for %ds audio file.', totalTime(inference_stop), audioLength.toPrecision(4));
137146
Ds.FreeModel(model);
138-
process.exit(0);
147+
// Allow some time for resources to exhaust and ensure we finish the
148+
// process anyway
149+
setTimeout(() => {
150+
handleExit();
151+
}, 1*1000);
152+
});
153+
audioStream.on('close', () => {
154+
handleExit();
139155
});
140156
} else {
141157
let stream = model.createStream();

0 commit comments

Comments
 (0)