ssovee-os-web-ui 0.1.41 → 0.1.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -3265,11 +3265,77 @@ var soundMap = {
3265
3265
  swipe: swipe_tone_default
3266
3266
  };
3267
3267
  var DEFAULT_SOUND_TYPE = "click";
3268
+ var audioBufferCache = /* @__PURE__ */ new Map();
3269
+ var sharedAudioContext = null;
3268
3270
  var isSoundType = (type) => {
3269
3271
  return type in soundMap;
3270
3272
  };
3271
3273
  var useSound = () => {
3272
- const audioCache = React22.useRef({});
3274
+ const getAudioContext = () => {
3275
+ if (typeof window === "undefined") {
3276
+ return null;
3277
+ }
3278
+ const AudioContextCtor = window.AudioContext ?? window.webkitAudioContext;
3279
+ if (!AudioContextCtor) {
3280
+ return null;
3281
+ }
3282
+ if (!sharedAudioContext || sharedAudioContext.state === "closed") {
3283
+ sharedAudioContext = new AudioContextCtor();
3284
+ }
3285
+ return sharedAudioContext;
3286
+ };
3287
+ const loadAudioBuffer = async (source) => {
3288
+ const cached = audioBufferCache.get(source);
3289
+ if (cached) {
3290
+ return cached;
3291
+ }
3292
+ const promise = (async () => {
3293
+ if (typeof fetch !== "function") {
3294
+ throw new Error("Fetch is not available");
3295
+ }
3296
+ const response = await fetch(source);
3297
+ if (!response.ok) {
3298
+ throw new Error(`Failed to load sound source: ${response.status} ${response.statusText}`);
3299
+ }
3300
+ const context = getAudioContext();
3301
+ if (!context) {
3302
+ throw new Error("Web Audio API is not available");
3303
+ }
3304
+ const arrayBuffer = await response.arrayBuffer();
3305
+ return await context.decodeAudioData(arrayBuffer.slice(0));
3306
+ })().catch((error) => {
3307
+ audioBufferCache.delete(source);
3308
+ throw error;
3309
+ });
3310
+ audioBufferCache.set(source, promise);
3311
+ return promise;
3312
+ };
3313
+ const playWithAudioBuffer = async (buffer) => {
3314
+ const context = getAudioContext();
3315
+ if (!context) {
3316
+ throw new Error("Web Audio API is not available");
3317
+ }
3318
+ if (context.state === "suspended") {
3319
+ await context.resume();
3320
+ }
3321
+ await new Promise((resolve, reject) => {
3322
+ const sourceNode = context.createBufferSource();
3323
+ sourceNode.buffer = buffer;
3324
+ sourceNode.connect(context.destination);
3325
+ sourceNode.onended = () => resolve();
3326
+ try {
3327
+ sourceNode.start(0);
3328
+ } catch (error) {
3329
+ reject(error);
3330
+ }
3331
+ });
3332
+ };
3333
+ const playWithAudioElement = async (source) => {
3334
+ const audio = new Audio(source);
3335
+ audio.preload = "auto";
3336
+ audio.currentTime = 0;
3337
+ await audio.play();
3338
+ };
3273
3339
  const playSound = React22.useCallback((type) => {
3274
3340
  if (typeof window === "undefined") {
3275
3341
  return;
@@ -3277,35 +3343,28 @@ var useSound = () => {
3277
3343
  const requestedType = isSoundType(type) ? type : DEFAULT_SOUND_TYPE;
3278
3344
  const src = soundMap[requestedType];
3279
3345
  if (!src) {
3280
- console.error(
3281
- `Sound playback failed: no source found for "${requestedType}"`
3282
- );
3346
+ console.error(`Sound playback failed: no source found for "${requestedType}"`);
3283
3347
  return;
3284
3348
  }
3285
- try {
3286
- let audio = audioCache.current[requestedType];
3287
- if (!audio) {
3288
- audio = new Audio(src);
3289
- audio.preload = "auto";
3290
- audioCache.current[requestedType] = audio;
3349
+ void (async () => {
3350
+ let lastError;
3351
+ if (getAudioContext()) {
3352
+ try {
3353
+ const buffer = await loadAudioBuffer(src);
3354
+ await playWithAudioBuffer(buffer);
3355
+ return;
3356
+ } catch (error) {
3357
+ lastError = error;
3358
+ }
3291
3359
  }
3292
- audio.pause();
3293
- audio.currentTime = 0;
3294
- const playPromise = audio.play();
3295
- if (playPromise !== void 0) {
3296
- playPromise.catch((error) => {
3297
- console.error(
3298
- `Sound playback failed for "${requestedType}":`,
3299
- error
3300
- );
3301
- });
3360
+ try {
3361
+ await playWithAudioElement(src);
3362
+ return;
3363
+ } catch (error) {
3364
+ lastError = error;
3302
3365
  }
3303
- } catch (error) {
3304
- console.error(
3305
- `Sound playback failed for "${requestedType}":`,
3306
- error
3307
- );
3308
- }
3366
+ console.error(`Sound playback failed for "${requestedType}":`, lastError);
3367
+ })();
3309
3368
  }, []);
3310
3369
  return {
3311
3370
  playSound