Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

fix: Register error event handler immediately. #156

Merged
merged 4 commits into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,26 @@ module.exports = () => {
options = {};
}

var requestStream = this._innerApiCalls.streamingRecognize(options);

// Format the audio content as input request for pipeline
var recognizeStream = streamEvents(pumpify.obj());

var requestStream = this._innerApiCalls
.streamingRecognize(options)
.on('error', err => {
recognizeStream.destroy(err);
})
.on('response', response => {
recognizeStream.emit('response', response);
});

// Attach the events to the request stream, but only do so
// when the first write (of data) comes in.
//
// This also means that the sending of the initial request (with the
// config) is delayed until we get the first burst of data.
recognizeStream.once('writing', () => {
requestStream.on('error', err => {
recognizeStream.destroy(err);
});

// Responses must be explicitly forwarded.
requestStream.on('response', response => {
recognizeStream.emit('response', response);
});

// The first message should contain the streaming config.
let first_message = true;
let firstMessage = true;

// Set up appropriate piping between the stream returned by
// the underlying API method and the one that we return.
Expand All @@ -98,7 +96,7 @@ module.exports = () => {
// the appropriate request structure.
through.obj((obj, _, next) => {
let payload = {};
if (first_message && config !== undefined) {
if (firstMessage && config !== undefined) {
// Write the initial configuration to the stream.
payload.streamingConfig = config;
}
Expand Down
2 changes: 0 additions & 2 deletions test/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ describe('Speech helper methods', () => {
done();
});

userStream.emit('writing');

requestStream.emit('error', error);
});

Expand Down