Skip to content

Commit

Permalink
chore: update types
Browse files Browse the repository at this point in the history
  • Loading branch information
codejs-kr committed Feb 21, 2021
1 parent 79dd0c0 commit b8684d7
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 24 deletions.
11 changes: 9 additions & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ERROR_TYPES } from './types';
declare class STT {
private recognition;
private isRecognizing;
Expand All @@ -15,9 +16,15 @@ declare class STT {
abort: () => void;
onStart: () => void;
onEnd: () => void;
onResult: (event: any) => boolean;
onError: (event: any) => void;
onResult: (event: {
results: any[];
resultIndex: number;
}) => void;
onError: (event: {
error: string;
}) => void;
getIsRecognizing: () => boolean;
getRecognition: () => any;
}
export { ERROR_TYPES };
export default STT;
22 changes: 13 additions & 9 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 16 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mitt from 'mitt';
import { ERROR_TYPES } from './types';
import { isSupportedBrowser } from './env';

const speechRecognition = window.webkitSpeechRecognition;
Expand Down Expand Up @@ -34,7 +35,7 @@ class STT {

start = () => {
if (!isSupportedBrowser) {
emitter.emit('error', 'not-supported-browser');
emitter.emit('error', ERROR_TYPES.NOT_SUPPORTED_BROWSER);
return;
}

Expand Down Expand Up @@ -65,32 +66,34 @@ class STT {
emitter.emit('end');
};

onResult = (event) => {
onResult = (event: { results: any[]; resultIndex: number }) => {
const { results, resultIndex } = event;
let interimTranscript: string = '';
if (typeof event.results === 'undefined') {

if (typeof results === 'undefined') {
recognition.onend = null;
recognition.stop();
return false;
return;
}

for (let i = event.resultIndex; i < event.results.length; ++i) {
const transcript = event.results[i][0].transcript;
if (event.results[i].isFinal) {
for (let i = resultIndex; i < results.length; ++i) {
const transcript = results[i][0].transcript;
if (results[i].isFinal) {
this.finalTranscript += transcript;
console.log('isFinal :>> ', transcript, event.results);
console.log('isFinal :>> ', transcript, results);
} else {
interimTranscript += transcript;
}
}

emitter.emit('result', {
results: event.results,
finalTranscript: this.finalTranscript,
results,
interimTranscript,
finalTranscript: this.finalTranscript,
});
};

onError = (event) => {
onError = (event: { error: string }) => {
this.isRecognizing = false;
emitter.emit('error', event.error);
};
Expand All @@ -104,4 +107,6 @@ class STT {
};
}

export { ERROR_TYPES };

export default STT;
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum ERROR_TYPES {
NOT_SUPPORTED_BROWSER = 'not-supported-browser',
NOT_ALLOWED = 'not-allowed',
NO_SPEECH = 'no-speech',
AUDIO_CAPTURE = 'audio-capture',
}
2 changes: 1 addition & 1 deletion website/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ function bindSttEvents() {
fireCommand(interimTranscript);
});

// no-speech|audio-capture|not-allowed|not-supported-browser
stt.on('error', (error) => {
console.log('error :>> ', error);
// no-speech|audio-capture|not-allowed|not-supported-browser
$btnMic.classList.replace('on', 'off');

switch (error) {
Expand Down

0 comments on commit b8684d7

Please sign in to comment.