Speech Voices Store (utils/voices.ts)

librechat · client/src/utils/voices.ts · typescript · MIT
voice, fw:react, tag:store

Module-level store subscribing to SpeechSynthesis voice changes, exposing a snapshot for React's useSyncExternalStore to avoid redundant re-renders across TTS buttons.

// Source: https://github.com/danny-avila/librechat | License: MIT | Copyright 2026 LibreChat
// Provided via justclaw.it.com; keep this notice with the code.
export type SpeechVoicesSnapshot = {
  voices: SpeechSynthesisVoice[];
  supported: boolean;
};

let snapshot: SpeechVoicesSnapshot = { voices: [], supported: true };
const listeners = new Set<() => void>();
let initialized = false;

const notify = () => {
  listeners.forEach((listener) => listener());
};

const readVoices = (synth: SpeechSynthesis) => {
  try {
    const voices = synth.getVoices();
    if (!Array.isArray(voices)) {
      console.error('getVoices() did not return an array');
      return;
    }
    snapshot = { voices, supported: true };
    notify();
  } catch (error) {
    console.error('Error updating voices:', er

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key