Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const EVENT_NAMES = ['onresult', 'onerror', 'onend'];
export class SpeechRecognitionAdapter {
private _speechRecognition?: SpeechRecognition | null;

private _isListening = false;

constructor(config: SpeechRecognitionConfig, events: SpeechRecognitionEvents) {
const window = getWindow();
// @ts-expect-error SpeechRecognition API is not supported in TS
Expand All @@ -42,7 +44,15 @@ export class SpeechRecognitionAdapter {
}

// eslint-disable-next-line spellcheck/spell-checker
this._speechRecognition.onend = events.onEnd;
this._speechRecognition.onstart = (): void => {
this._isListening = true;
};
// eslint-disable-next-line spellcheck/spell-checker
this._speechRecognition.onend = (event: Event): void => {
this._isListening = false;

events.onEnd(event);
};
// eslint-disable-next-line spellcheck/spell-checker
this._speechRecognition.onresult = events.onResult;
this._speechRecognition.onerror = events.onError;
Expand All @@ -57,10 +67,18 @@ export class SpeechRecognitionAdapter {
}

start(): void {
if (this._isListening) {
return;
}

this._speechRecognition?.start();
}

stop(): void {
if (!this._isListening) {
return;
}

this._speechRecognition?.stop();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ QUnit.module('SpeechRecognitionAdapter', {
constructor() {
this.start = sinon.spy();
this.stop = sinon.spy();
this.onstart = null;
this.onresult = null;
this.onerror = null;
this.onend = null;
Expand Down Expand Up @@ -118,6 +119,7 @@ QUnit.module('SpeechRecognitionAdapter', {
const speechRecognition = adapter._speechRecognition;

adapter.start();
speechRecognition.onstart();
adapter.stop();

assert.ok(speechRecognition.start.calledOnce, 'start called');
Expand All @@ -131,5 +133,37 @@ QUnit.module('SpeechRecognitionAdapter', {
adapter.dispose();
assert.strictEqual(adapter._speechRecognition, null, 'cleared after dispose');
});

QUnit.test('should not call start when listening', function(assert) {
const adapter = this.createAdapter();
const speechRecognition = adapter._speechRecognition;

speechRecognition.onstart();
adapter.start();

assert.ok(speechRecognition.start.notCalled, 'start not called');
});

QUnit.test('should not call stop if not listening', function(assert) {
const adapter = this.createAdapter();
const speechRecognition = adapter._speechRecognition;

adapter.stop();

assert.ok(speechRecognition.stop.notCalled, 'stop not called');
});

QUnit.test('should update _isListening on onstart/onend', function(assert) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion(minor)

We don't usually check private APIs.

const adapter = this.createAdapter();
const speechRecognition = adapter._speechRecognition;

assert.strictEqual(adapter._isListening, false, 'initially not listening');

speechRecognition.onstart();
assert.strictEqual(adapter._isListening, true, 'set to listening after onstart');

speechRecognition.onend();
assert.strictEqual(adapter._isListening, false, 'reset to false after onend');
});
});

Loading