@aippy/runtime 0.2.0-dev.2 → 0.2.0-dev.3

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.
@@ -1,4 +1,4 @@
1
- import { c, a, i, b, p, u } from "../useAudioContext-2R1HNn6s.js";
1
+ import { c, a, i, b, p, u } from "../useAudioContext-BrvG8DKk.js";
2
2
  export {
3
3
  c as createHiddenMediaElement,
4
4
  a as createHiddenVideoElement,
@@ -33,7 +33,7 @@ function mergeConfig(userConfig) {
33
33
  }
34
34
  };
35
35
  }
36
- const version = "0.2.0-dev.2";
36
+ const version = "0.2.0-dev.3";
37
37
  const packageJson = {
38
38
  version
39
39
  };
@@ -3,7 +3,7 @@ import { A, E, c } from "../errors-DAz5_jDJ.js";
3
3
  import { CameraAPI, FileSystemAPI, GeolocationAPI, SensorsAPI, camera, fileSystem, geolocation, sensors, vibrate } from "../device/index.js";
4
4
  import { c as c2, a, P, b, p, d } from "../pwa-BkviTQoN.js";
5
5
  import { a as a2, b as b2 } from "../useTweaks-mK5PAWOs.js";
6
- import { c as c3, a as a3, i, b as b3, p as p2, u } from "../useAudioContext-2R1HNn6s.js";
6
+ import { c as c3, a as a3, i, b as b3, p as p2, u } from "../useAudioContext-BrvG8DKk.js";
7
7
  export {
8
8
  A as AippyRuntimeError,
9
9
  CameraAPI,
@@ -1,4 +1,4 @@
1
- import { useRef, useEffect } from "react";
1
+ import { useState, useRef, useEffect } from "react";
2
2
  function isIOSDevice() {
3
3
  const userAgent = navigator.userAgent;
4
4
  if (/iPad|iPhone|iPod/.test(userAgent)) {
@@ -292,31 +292,32 @@ function patchAudioContext(audioContext, options = {}) {
292
292
  }
293
293
  function useAudioContext(options = {}) {
294
294
  const { autoUnlock = true, ...patchOptions } = options;
295
- const audioContextRef = useRef(null);
296
- const isUnlockedRef = useRef(false);
295
+ const [audioContext, setAudioContext] = useState(null);
296
+ const [isUnlocked, setIsUnlocked] = useState(false);
297
297
  const unlockFnRef = useRef(null);
298
298
  useEffect(() => {
299
299
  const ctx = new AudioContext();
300
- audioContextRef.current = patchAudioContext(ctx, patchOptions);
300
+ const patchedCtx = patchAudioContext(ctx, patchOptions);
301
+ setAudioContext(patchedCtx);
301
302
  return () => {
302
- audioContextRef.current?.cleanup();
303
- audioContextRef.current?.close();
303
+ patchedCtx.cleanup();
304
+ patchedCtx.close();
304
305
  };
305
306
  }, []);
306
- if (!unlockFnRef.current) {
307
+ useEffect(() => {
308
+ if (!audioContext) return;
307
309
  unlockFnRef.current = async () => {
308
- const ctx = audioContextRef.current;
309
- if (!ctx || isUnlockedRef.current) return;
310
+ if (isUnlocked) return;
310
311
  try {
311
- await ctx.unlock();
312
- isUnlockedRef.current = true;
312
+ await audioContext.unlock();
313
+ setIsUnlocked(true);
313
314
  } catch (error) {
314
315
  console.warn("Failed to unlock audio:", error);
315
316
  }
316
317
  };
317
- }
318
+ }, [audioContext, isUnlocked]);
318
319
  useEffect(() => {
319
- if (!autoUnlock) return;
320
+ if (!autoUnlock || !audioContext) return;
320
321
  const handleInteraction = async () => {
321
322
  await unlockFnRef.current?.();
322
323
  };
@@ -326,11 +327,12 @@ function useAudioContext(options = {}) {
326
327
  document.removeEventListener("click", handleInteraction);
327
328
  document.removeEventListener("touchstart", handleInteraction);
328
329
  };
329
- }, [autoUnlock]);
330
+ }, [autoUnlock, audioContext]);
330
331
  return {
331
- audioContext: audioContextRef.current,
332
- isUnlocked: isUnlockedRef.current,
333
- unlock: unlockFnRef.current
332
+ audioContext,
333
+ isUnlocked,
334
+ unlock: unlockFnRef.current || (async () => {
335
+ })
334
336
  };
335
337
  }
336
338
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aippy/runtime",
3
- "version": "0.2.0-dev.2",
3
+ "version": "0.2.0-dev.3",
4
4
  "description": "Aippy Runtime SDK - Runtime SDK for Aippy projects",
5
5
  "private": false,
6
6
  "type": "module",