Skip to content

Commit

Permalink
Merge pull request #477 from cboard-org/fix-speech-provider
Browse files Browse the repository at this point in the history
Fix speech provider
  • Loading branch information
martinbedouret authored Sep 27, 2019
2 parents 134f046 + 701d6d8 commit fac8d66
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/providers/SpeechProvider/SpeechProvider.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function getVoices() {
name
}));
dispatch(receiveVoices(voices));
return voices;
});
};
}
Expand Down
10 changes: 5 additions & 5 deletions src/providers/SpeechProvider/SpeechProvider.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ export class SpeechProvider extends Component {

render() {
const { voices, children } = this.props;
if (!voices.length) {
return null;
}

return React.Children.only(children);
return !!voices.length ? React.Children.only(children) : null;
}
}

Expand All @@ -37,4 +34,7 @@ const mapDispatchToProps = {
getVoices
};

export default connect(mapStateToProps, mapDispatchToProps)(SpeechProvider);
export default connect(
mapStateToProps,
mapDispatchToProps
)(SpeechProvider);
7 changes: 4 additions & 3 deletions src/providers/SpeechProvider/SpeechProvider.reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function speechProviderReducer(state = initialState, action) {

return {
...state,
options,
options
};
case RECEIVE_VOICES:
return {
Expand All @@ -60,8 +60,9 @@ function speechProviderReducer(state = initialState, action) {
options: {
...state.options,
lang: action.lang,
voiceURI: state.voices.find(voice => voice.lang === action.lang)
.voiceURI
voiceURI: state.voices.length
? state.voices.find(voice => voice.lang === action.lang).voiceURI
: null
}
};
case CHANGE_PITCH:
Expand Down
23 changes: 17 additions & 6 deletions src/providers/SpeechProvider/tts.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ const tts = {

// Get voices depending on platform (browser/cordova)
_getPlatformVoices() {
const voices = synth.getVoices() || [];

const voices = synth.getVoices();
// On Cordova, voice results are under `._list`
return voices._list || voices;
},
Expand All @@ -59,10 +58,22 @@ const tts = {

// Android
if ('onvoiceschanged' in synth) {
speechSynthesis.onvoiceschanged = () => {
cachedVoices = this._getPlatformVoices();
resolve(this.normalizeVoices(cachedVoices));
};
synth.addEventListener('voiceschanged', function voiceslst() {
const voices = synth.getVoices();
if (!voices.length) {
return null;
} else {
synth.removeEventListener('voiceschanged', voiceslst);
// On Cordova, voice results are under `._list`
cachedVoices = voices._list || voices;
let nVoices = cachedVoices.map(({ voiceURI, name, lang }) => ({
voiceURI,
name,
lang: normalizeLanguageCode(lang)
}));
resolve(nVoices);
}
});
}
});
},
Expand Down

0 comments on commit fac8d66

Please sign in to comment.