@waveform-playlist/recording 14.0.0-next.19 → 14.0.0-next.20

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.mjs CHANGED
@@ -39,7 +39,7 @@ function useRecordingWithTakeEnded(stream, options = {}, onTakeEnded) {
39
39
  const isStoppingRef = useRef(false);
40
40
  const stopPromiseRef = useRef(null);
41
41
  const finishStopRecordingRef = useRef(null);
42
- const isStartingRef = useRef(false);
42
+ const startingEpochRef = useRef(null);
43
43
  const isUnmountedRef = useRef(false);
44
44
  const mountEpochRef = useRef(0);
45
45
  const durationRef = useRef(0);
@@ -77,21 +77,22 @@ function useRecordingWithTakeEnded(stream, options = {}, onTakeEnded) {
77
77
  }
78
78
  }, []);
79
79
  const startRecording = useCallback(async () => {
80
- if (isRecordingRef.current || isStartingRef.current) return false;
80
+ const refuseEndedStream = (message) => {
81
+ if (stream?.getAudioTracks()[0]?.readyState !== "ended") return false;
82
+ setError(new Error(message));
83
+ return true;
84
+ };
85
+ if (isRecordingRef.current || startingEpochRef.current === mountEpochRef.current) return false;
81
86
  if (!stream) {
82
87
  setError(new Error("No microphone stream available"));
83
88
  return false;
84
89
  }
85
- if (stream.getAudioTracks()[0]?.readyState === "ended") {
86
- setError(
87
- new Error(
88
- "The microphone stream has ended (the device was disconnected or its permission revoked). Request microphone access again."
89
- )
90
- );
90
+ if (refuseEndedStream(
91
+ "The microphone stream has ended (the device was disconnected or its permission revoked). Request microphone access again."
92
+ ))
91
93
  return false;
92
- }
93
- isStartingRef.current = true;
94
94
  const mountEpoch = mountEpochRef.current;
95
+ startingEpochRef.current = mountEpoch;
95
96
  try {
96
97
  setError(null);
97
98
  const context = getGlobalContext();
@@ -100,6 +101,10 @@ function useRecordingWithTakeEnded(stream, options = {}, onTakeEnded) {
100
101
  }
101
102
  await loadWorklet();
102
103
  if (isUnmountedRef.current || mountEpoch !== mountEpochRef.current) return false;
104
+ if (refuseEndedStream(
105
+ "The microphone changed or was disconnected while recording was starting. Start recording again."
106
+ ))
107
+ return false;
103
108
  const source = context.createMediaStreamSource(stream);
104
109
  mediaStreamSourceRef.current = source;
105
110
  const detectedChannelCount = stream.getAudioTracks()[0]?.getSettings().channelCount;
@@ -183,7 +188,7 @@ function useRecordingWithTakeEnded(stream, options = {}, onTakeEnded) {
183
188
  setError(err instanceof Error ? err : new Error("Failed to start recording"));
184
189
  return false;
185
190
  } finally {
186
- isStartingRef.current = false;
191
+ if (startingEpochRef.current === mountEpoch) startingEpochRef.current = null;
187
192
  }
188
193
  }, [
189
194
  stream,
@@ -310,9 +315,10 @@ function useRecordingWithTakeEnded(stream, options = {}, onTakeEnded) {
310
315
  }, [startDurationLoop]);
311
316
  useEffect(() => {
312
317
  isUnmountedRef.current = false;
313
- mountEpochRef.current += 1;
318
+ const epoch = mountEpochRef.current;
314
319
  return () => {
315
320
  isUnmountedRef.current = true;
321
+ mountEpochRef.current = epoch + 1;
316
322
  if (audioTrackRef.current && onTrackEndedRef.current) {
317
323
  audioTrackRef.current.removeEventListener("ended", onTrackEndedRef.current);
318
324
  }
@@ -379,12 +385,12 @@ function useMicrophoneAccess() {
379
385
  const [stream, setStream] = useState2(null);
380
386
  const [devices, setDevices] = useState2([]);
381
387
  const [hasLabels, setHasLabels] = useState2(false);
382
- const [hasPermission, setHasPermission] = useState2(false);
383
388
  const [isLoading, setIsLoading] = useState2(false);
384
389
  const [error, setError] = useState2(null);
385
390
  const streamRef = useRef2(null);
386
391
  const requestGenerationRef = useRef2(0);
387
392
  const isUnmountedRef = useRef2(false);
393
+ const mountEpochRef = useRef2(0);
388
394
  const enumerateDevices = useCallback2(async () => {
389
395
  try {
390
396
  const { devices: audioInputs, hasLabels: labelled } = await enumerateMicrophones();
@@ -398,6 +404,8 @@ function useMicrophoneAccess() {
398
404
  const requestAccess = useCallback2(
399
405
  async (deviceId, audioConstraints) => {
400
406
  const generation = ++requestGenerationRef.current;
407
+ const mountEpoch = mountEpochRef.current;
408
+ const isStale = () => generation !== requestGenerationRef.current || mountEpoch !== mountEpochRef.current || isUnmountedRef.current;
401
409
  setIsLoading(true);
402
410
  setError(null);
403
411
  try {
@@ -429,18 +437,16 @@ function useMicrophoneAccess() {
429
437
  );
430
438
  }
431
439
  const newStream = await mediaDevices.getUserMedia(constraints);
432
- if (generation !== requestGenerationRef.current || isUnmountedRef.current) {
440
+ if (isStale()) {
433
441
  newStream.getTracks().forEach((track) => track.stop());
434
442
  return;
435
443
  }
436
444
  streamRef.current = newStream;
437
445
  setStream(newStream);
438
- setHasPermission(true);
439
446
  const onEnded = () => {
440
447
  if (streamRef.current !== newStream) return;
441
448
  streamRef.current = null;
442
449
  setStream(null);
443
- setHasPermission(false);
444
450
  setError(
445
451
  new Error(
446
452
  "The microphone was disconnected or its permission was revoked. Request microphone access again."
@@ -451,9 +457,8 @@ function useMicrophoneAccess() {
451
457
  await enumerateDevices();
452
458
  } catch (err) {
453
459
  console.warn("[waveform-playlist] Failed to access microphone:", String(err));
454
- if (generation === requestGenerationRef.current) {
460
+ if (!isStale()) {
455
461
  setError(err instanceof Error ? err : new Error("Failed to access microphone"));
456
- setHasPermission(false);
457
462
  }
458
463
  } finally {
459
464
  if (generation === requestGenerationRef.current) {
@@ -465,26 +470,28 @@ function useMicrophoneAccess() {
465
470
  );
466
471
  const stopStream = useCallback2(() => {
467
472
  requestGenerationRef.current++;
473
+ setIsLoading(false);
468
474
  if (streamRef.current) {
469
475
  streamRef.current.getTracks().forEach((track) => track.stop());
470
476
  streamRef.current = null;
471
477
  setStream(null);
472
- setHasPermission(false);
473
478
  }
474
479
  }, []);
475
480
  useEffect2(() => {
476
481
  isUnmountedRef.current = false;
482
+ const epoch = mountEpochRef.current;
477
483
  enumerateDevices();
478
484
  const mediaDevices = typeof navigator !== "undefined" ? navigator.mediaDevices : void 0;
479
485
  mediaDevices?.addEventListener("devicechange", enumerateDevices);
480
486
  return () => {
481
487
  mediaDevices?.removeEventListener("devicechange", enumerateDevices);
482
488
  isUnmountedRef.current = true;
489
+ mountEpochRef.current = epoch + 1;
490
+ setIsLoading(false);
483
491
  if (streamRef.current) {
484
492
  streamRef.current.getTracks().forEach((track) => track.stop());
485
493
  streamRef.current = null;
486
494
  setStream(null);
487
- setHasPermission(false);
488
495
  }
489
496
  };
490
497
  }, [enumerateDevices]);
@@ -492,7 +499,6 @@ function useMicrophoneAccess() {
492
499
  stream,
493
500
  devices,
494
501
  hasLabels,
495
- hasPermission,
496
502
  isLoading,
497
503
  requestAccess,
498
504
  stopStream,
@@ -606,7 +612,7 @@ function useMicrophoneLevel(stream, options = {}) {
606
612
  }
607
613
 
608
614
  // src/hooks/useIntegratedRecording.ts
609
- import { useState as useState4, useCallback as useCallback4, useMemo, useRef as useRef4 } from "react";
615
+ import { useState as useState4, useCallback as useCallback4, useEffect as useEffect4, useMemo, useRef as useRef4 } from "react";
610
616
 
611
617
  // src/hooks/takeTarget.ts
612
618
  import {
@@ -693,7 +699,14 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
693
699
  const recordingStartTimeRef = useRef4(0);
694
700
  const recordingTrackIdRef = useRef4(null);
695
701
  const [recordingTrackId, setRecordingTrackId] = useState4(null);
696
- const isStartingRef = useRef4(false);
702
+ const startingEpochRef = useRef4(null);
703
+ const mountEpochRef = useRef4(0);
704
+ useEffect4(() => {
705
+ const epoch = mountEpochRef.current;
706
+ return () => {
707
+ mountEpochRef.current = epoch + 1;
708
+ };
709
+ }, []);
697
710
  const selectedTrackIdRef = useRef4(selectedTrackId);
698
711
  selectedTrackIdRef.current = selectedTrackId;
699
712
  const currentTimeRef = useRef4(currentTime);
@@ -702,14 +715,7 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
702
715
  audioConstraintsRef.current = audioConstraints;
703
716
  const finishTakeRef = useRef4(() => {
704
717
  });
705
- const {
706
- stream,
707
- devices,
708
- hasLabels,
709
- hasPermission,
710
- requestAccess,
711
- error: micError
712
- } = useMicrophoneAccess();
718
+ const { stream, devices, hasLabels, requestAccess, error: micError } = useMicrophoneAccess();
713
719
  const selectedDevice = useMemo(() => openDeviceId(stream), [stream]);
714
720
  const {
715
721
  meter,
@@ -738,7 +744,7 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
738
744
  const startRecRef = useRef4(startRec);
739
745
  startRecRef.current = startRec;
740
746
  const startRecording = useCallback4(async () => {
741
- if (isRecordingRef.current || isStartingRef.current) return false;
747
+ if (isRecordingRef.current || startingEpochRef.current === mountEpochRef.current) return false;
742
748
  if (!selectedTrackIdRef.current) {
743
749
  setHookError(
744
750
  new Error("Cannot start recording: no track selected. Select or create a track first.")
@@ -750,25 +756,30 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
750
756
  setHookError(refusal);
751
757
  return false;
752
758
  }
753
- isStartingRef.current = true;
759
+ const mountEpoch = mountEpochRef.current;
760
+ startingEpochRef.current = mountEpoch;
761
+ const isStale = () => mountEpoch !== mountEpochRef.current;
754
762
  try {
755
763
  setHookError(null);
756
764
  if (!audioResumedRef.current) {
757
765
  await resumeGlobalAudioContext();
758
766
  audioResumedRef.current = true;
759
767
  }
768
+ if (isStale()) return false;
760
769
  recordingStartTimeRef.current = currentTimeRef.current;
761
770
  recordingTrackIdRef.current = selectedTrackIdRef.current;
762
771
  setRecordingTrackId(selectedTrackIdRef.current);
763
772
  const started = await startRecRef.current();
764
- if (!started) setRecordingTrackId(null);
773
+ if (!started && !isStale()) setRecordingTrackId(null);
765
774
  return started;
766
775
  } catch (err) {
767
- setRecordingTrackId(null);
768
- setHookError(err instanceof Error ? err : new Error(String(err)));
776
+ if (!isStale()) {
777
+ setRecordingTrackId(null);
778
+ setHookError(err instanceof Error ? err : new Error(String(err)));
779
+ }
769
780
  return false;
770
781
  } finally {
771
- isStartingRef.current = false;
782
+ if (startingEpochRef.current === mountEpoch) startingEpochRef.current = null;
772
783
  }
773
784
  }, [arrangement]);
774
785
  const finishTake = useCallback4(
@@ -884,7 +895,6 @@ function useIntegratedRecording(arrangement, selectedTrackId, options = {}) {
884
895
  stream,
885
896
  devices,
886
897
  hasLabels,
887
- hasPermission,
888
898
  selectedDevice,
889
899
  // Recording controls
890
900
  startRecording,