Skip to content

Commit

Permalink
✨ feat: Handle exceptions and null returns in polyfills and hooks
Browse files Browse the repository at this point in the history
This update introduces proper error handling and null returns in the polyfill
functions for SpeechRecognition, SpeechSynthesis, and SpeechSynthesisUtterance
to prevent silent failures. Additionally, the useBlobUrl hook now logs an error
if audio playback fails, improving the debugging process. The splitTextIntoSegments
utility function has been updated to use String.fromCodePoint for better character
encoding handling and to fix a regular expression for sentence splitting.
  • Loading branch information
Gincioks committed Mar 24, 2024
1 parent aefb988 commit ded7e6a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/core/const/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const getSpeechRecognition = () => {
(window as any)?.SpeechRecognition ||
(window as any)?.webkitSpeechRecognition
);
} catch {}
} catch {
return null;
}
};

const getSpeechSynthesis = () => {
Expand All @@ -15,7 +17,9 @@ const getSpeechSynthesis = () => {
(window as any)?.speechSynthesis ||
(window as any)?.webkitSpeechSynthesis
);
} catch {}
} catch {
return null;
}
};

const getSpeechSynthesisUtterance = () => {
Expand All @@ -25,7 +29,9 @@ const getSpeechSynthesisUtterance = () => {
(window as any)?.SpeechSynthesisUtterance ||
(window as any)?.webkitSpeechSynthesisUtterance
);
} catch {}
} catch {
return null;
}
};
export const SpeechRecognition = getSpeechRecognition();
export const SpeechSynthesis = getSpeechSynthesis();
Expand Down
5 changes: 3 additions & 2 deletions src/core/utils/splitTextIntoSegments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const toHalfWidthAndCleanSpace = (str: string): string => {
// prettier-ignore
return str
.replaceAll(/[\uFF01-\uFF5E]/g, (ch) => String.fromCharCode(ch.charCodeAt(0) - 0xFE_E0))
.replaceAll(/[\uFF01-\uFF5E]/g, (ch) => String.fromCodePoint(ch.codePointAt(0)! - 0xFE_E0))
.replaceAll('\u3000', ' ')
.replaceAll('。', '.')
.replaceAll(',', ',')
Expand All @@ -25,7 +26,7 @@ const toHalfWidthAndCleanSpace = (str: string): string => {
export const splitTextIntoSegments = (text: string, maxChars: number = 100): string[] => {
text = toHalfWidthAndCleanSpace(text);

const sentences = text.match(/[^.!;?]+[.!;?]+/g) || [];
const sentences = text.match(/[^!.;?]+[!.;?]+/g) || [];
const segments: string[] = [];
let currentSegment = '';

Expand Down
4 changes: 3 additions & 1 deletion src/react/hooks/useBlobUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export const useBlobUrl = (src: string) => {
const newAudio = playAudioBlob(blob);
setUrl(newAudio.url);
setAudio(newAudio.audio);
} catch {}
} catch {
console.error('Failed to play audio');
}
setIsGlobalLoading(false);
},
},
Expand Down

0 comments on commit ded7e6a

Please sign in to comment.