Skip to content

Commit f847a27

Browse files
committed
[IMPL] useSpeechRecognition hook
1 parent 4363a4e commit f847a27

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

packages/react-tools/src/hooks/useSpeechRecognition.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const isSupported = !!window && ("SpeechRecognition" in window || "webkitSpeechR
99
/**
1010
* **`useSpeechRecognition`**: Hook to use _SpeechRecognition API_. Refer to [Web Speech API](https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition).
1111
* @param {Object} opts - options.
12+
* @param {boolean} [opts.alreadyStarted=false] - istant start SpeechRecognition if it is available.
1213
* @param {Object} [opts.defaultConfig] - config parameters for current SpeechRecognition.
1314
* @param {SpeechGrammarList} [opts.defaultConfig.grammars] - a _SpeechGrammarList_ containing the SpeechGrammar objects that represent your grammar for your app.
1415
* @param {LanguageBCP47Tags} [opts.defaultConfig.lang] - a string representing the BCP 47 language tag for the current SpeechRecognition.
@@ -35,10 +36,11 @@ const isSupported = !!window && ("SpeechRecognition" in window || "webkitSpeechR
3536
* - 3. __stop__: function to stop SpeechRecognition.
3637
* - 4. __reset__: function to reset SpeechRecognition with optional parameter to reset results also.
3738
*/
38-
export const useSpeechRecognition = ({ defaultConfig, onAudioStart, onAudioEnd, onEnd, onError, onNoMatch, onResult, onSoundStart, onSoundEnd, onSpeechStart, onSpeechEnd, onStart }: { defaultConfig?: SpeechRecognitionConfig, onAudioStart?: SpeechRecognition["onaudiostart"], onAudioEnd?: SpeechRecognition["onaudioend"], onEnd?: SpeechRecognition["onend"], onError?: SpeechRecognition["onerror"], onNoMatch?: SpeechRecognition["onnomatch"], onResult?: SpeechRecognition["onresult"], onSoundStart?: SpeechRecognition["onsoundstart"], onSoundEnd?: SpeechRecognition["onsoundend"], onSpeechStart?: SpeechRecognition["onspeechstart"], onSpeechEnd?: SpeechRecognition["onspeechend"], onStart?: SpeechRecognition["onstart"] }): [SpeechRecognitionState, (config?: SpeechRecognitionConfig) => void, () => void, (resultAlso?: boolean) => void] => {
39+
export const useSpeechRecognition = ({ alreadyStarted, defaultConfig, onAudioStart, onAudioEnd, onEnd, onError, onNoMatch, onResult, onSoundStart, onSoundEnd, onSpeechStart, onSpeechEnd, onStart }: { alreadyStarted?: boolean, defaultConfig?: SpeechRecognitionConfig, onAudioStart?: SpeechRecognition["onaudiostart"], onAudioEnd?: SpeechRecognition["onaudioend"], onEnd?: SpeechRecognition["onend"], onError?: SpeechRecognition["onerror"], onNoMatch?: SpeechRecognition["onnomatch"], onResult?: SpeechRecognition["onresult"], onSoundStart?: SpeechRecognition["onsoundstart"], onSoundEnd?: SpeechRecognition["onsoundend"], onSpeechStart?: SpeechRecognition["onspeechstart"], onSpeechEnd?: SpeechRecognition["onspeechend"], onStart?: SpeechRecognition["onstart"] }): [SpeechRecognitionState, (config?: SpeechRecognitionConfig) => void, () => void, (resultAlso?: boolean) => void] => {
3940
const recognition = useRef<SpeechRecognition>();
4041
const notifRef = useRef<() => void>();
4142
const isListening = useRef(false);
43+
const firstExecution = useRef(true);
4244
const result = useRef<{ results: SpeechRecognitionEvent["results"]|null, resultIndex: SpeechRecognitionEvent["resultIndex"]|null }>({resultIndex: null, results: null});
4345

4446
const start = useCallback((config?: typeof defaultConfig) => {
@@ -143,5 +145,11 @@ export const useSpeechRecognition = ({ defaultConfig, onAudioStart, onAudioEnd,
143145
}, [])
144146
);
145147

148+
if (firstExecution.current && isSupported && alreadyStarted) {
149+
isListening.current = true;
150+
firstExecution.current = false;
151+
start();
152+
}
153+
146154
return [state, start, stop.current, reset.current];
147155
}

0 commit comments

Comments
 (0)