From df8d17fc4e720069931752ad0bc234294d69902b Mon Sep 17 00:00:00 2001 From: Alexandre Lissy Date: Thu, 15 Apr 2021 14:03:05 +0200 Subject: [PATCH] Ensure proper termination for ElectronJS and NodeJS --- native_client/javascript/client.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/native_client/javascript/client.ts b/native_client/javascript/client.ts index c396f4bf..3f708476 100644 --- a/native_client/javascript/client.ts +++ b/native_client/javascript/client.ts @@ -89,6 +89,15 @@ if (result.sampleRate < desired_sample_rate) { `Up-sampling might produce erratic speech recognition.`); } +function handleExit() { + if (process.versions.electron) { + const { app } = require("electron"); + app.quit(); + } else { + process.exit(0); + } +} + function bufferToStream(buffer: Buffer) { var stream = new Duplex(); stream.push(buffer); @@ -135,7 +144,14 @@ if (!args['stream']) { const inference_stop = process.hrtime(inference_start); console.error('Inference took %ds for %ds audio file.', totalTime(inference_stop), audioLength.toPrecision(4)); Ds.FreeModel(model); - process.exit(0); + // Allow some time for resources to exhaust and ensure we finish the + // process anyway + setTimeout(() => { + handleExit(); + }, 1*1000); + }); + audioStream.on('close', () => { + handleExit(); }); } else { let stream = model.createStream();