@@ -40,7 +40,12 @@ import { useEnvironmentScope, usePreparedConnection } from "../state/session";
4040import { createBrowserVoiceInputPlatform } from "./browserVoiceInput" ;
4141import { preferredSpeechLanguages } from "./preferredSpeechLanguages" ;
4242import { runEnvironmentSpeech } from "./runEnvironmentSpeech" ;
43- import { bindAudioGrant , readCleanupGrant , useAudioChoice } from "./voiceConsent" ;
43+ import {
44+ bindAudioGrant ,
45+ readCleanupGrant ,
46+ subscribeAudioChoice ,
47+ useAudioChoice ,
48+ } from "./voiceConsent" ;
4449
4550const INITIAL_STATE : VoiceInputState = { phase : "idle" , error : null , errorAction : null } ;
4651const WAITING_STATE : VoiceInputState = { phase : "preparing" , error : null , errorAction : null } ;
@@ -82,18 +87,42 @@ type DraftAccess = {
8287
8388type DetachedRecording = {
8489 readonly controller : VoiceInputController ;
90+ readonly hasAudioConsent : ( ) => boolean ;
8591 readonly snapshot : VoiceDraftSnapshot ;
8692 readonly access : DraftAccess ;
8793 state : VoiceInputState ;
8894 committedText ?: string ;
8995} ;
9096
97+ const DETACHED_CONSENT_LOST =
98+ "Voice input was turned off or changed before the transcript was ready." ;
99+
100+ function failDetached ( description : string | null ) {
101+ toastManager . add ( {
102+ type : "error" ,
103+ title : "Dictation couldn't be added to its draft" ,
104+ description,
105+ } ) ;
106+ }
107+
91108// A recording finishing into its owner's draft after its editor moved to another owner or
92109// unmounted. Every editor reads it: the owner's shows it, the rest wait for it. The global voice
93110// session allows one recording at a time, so one slot is enough.
94111let detachedRecording : DetachedRecording | null = null ;
112+ let unwatchDetachedConsent = ( ) => { } ;
95113const detachedListeners = new Set < ( ) => void > ( ) ;
96114function publishDetached ( next : DetachedRecording | null ) {
115+ if ( next !== detachedRecording ) {
116+ unwatchDetachedConsent ( ) ;
117+ // No editor's effect sees a consent change once the recording is detached.
118+ unwatchDetachedConsent = next
119+ ? subscribeAudioChoice ( ( ) => {
120+ if ( next . hasAudioConsent ( ) ) return ;
121+ failDetached ( DETACHED_CONSENT_LOST ) ;
122+ next . controller . cancel ( ) ;
123+ } )
124+ : ( ) => { } ;
125+ }
97126 detachedRecording = next ;
98127 for ( const listener of detachedListeners ) listener ( ) ;
99128}
@@ -107,6 +136,7 @@ function subscribeDetached(listener: () => void) {
107136/** Finishes an active recording into its owner's draft while the editor shows another owner, or is gone. */
108137function detachRecording (
109138 controller : VoiceInputController ,
139+ hasAudioConsent : ( ) => boolean ,
110140 snapshot : VoiceDraftSnapshot | null ,
111141 { ownerKey, detachDraft } : HookInput ,
112142) : boolean {
@@ -119,22 +149,11 @@ function detachRecording(
119149 state . phase !== "post-processing" )
120150 )
121151 return false ;
122- publishDetached ( { controller, snapshot, access : detachDraft ( ) , state } ) ;
152+ publishDetached ( { controller, hasAudioConsent , snapshot, access : detachDraft ( ) , state } ) ;
123153 void controller . stop ( ) ;
124154 return true ;
125155}
126156
127- const DETACHED_CONSENT_LOST =
128- "Voice input was turned off or changed before the transcript was ready." ;
129-
130- function failDetached ( description : string | null ) {
131- toastManager . add ( {
132- type : "error" ,
133- title : "Dictation couldn't be added to its draft" ,
134- description,
135- } ) ;
136- }
137-
138157function cleanupContext ( { text, selection } : DraftInput ) : DraftInput {
139158 const limit = 1_024 ;
140159 const start = Math . max ( 0 , Math . min ( selection . start , text . length ) ) ;
@@ -272,6 +291,7 @@ export function useEnvironmentSpeechInput(input: HookInput) {
272291 : INITIAL_STATE ) ;
273292
274293 const controllerRef = useRef < VoiceInputController | null > ( null ) ;
294+ const audioConsentRef = useRef < ( ) => boolean > ( ( ) => false ) ;
275295 const startRequestRef = useRef ( 0 ) ;
276296 const pendingSubmissionRef = useRef < {
277297 ownerKey : string ;
@@ -458,7 +478,7 @@ export function useEnvironmentSpeechInput(input: HookInput) {
458478 commitDraft : ( text , selection ) => {
459479 const detached = detachedSelf ( ) ;
460480 if ( detached ) {
461- // Unmounted, consent changes no longer reach this controller through its effect .
481+ // Another window's revocation can land before its storage event reaches the watcher .
462482 if ( ! hasAudioConsent ( ) ) return failDetached ( DETACHED_CONSENT_LOST ) ;
463483 detached . access . commitDraft ( text , selection ) ;
464484 detached . committedText = text ;
@@ -487,19 +507,23 @@ export function useEnvironmentSpeechInput(input: HookInput) {
487507 } ,
488508 } ) ;
489509 controllerRef . current = controller ;
510+ audioConsentRef . current = hasAudioConsent ;
490511 return ( ) => {
491512 disposed = true ;
492513 pendingSubmissionRef . current = null ;
493514 startRequestRef . current += 1 ;
494515 setQueuedStart ( null ) ;
495516 if ( controllerRef . current === controller ) controllerRef . current = null ;
496- if ( detachedSelf ( ) ) {
497- if ( unmountingRef . current ) return ;
498- failDetached ( DETACHED_CONSENT_LOST ) ;
499- publishDetached ( null ) ;
500- } else if (
501- unmountingRef . current &&
502- detachRecording ( controller , lastSnapshotRef . current , latestRef . current . input )
517+ // A detached recording answers to its own consent watcher, not to this editor's settings.
518+ if (
519+ detachedSelf ( ) ||
520+ ( unmountingRef . current &&
521+ detachRecording (
522+ controller ,
523+ hasAudioConsent ,
524+ lastSnapshotRef . current ,
525+ latestRef . current . input ,
526+ ) )
503527 )
504528 return ;
505529 controller . dispose ( ) ;
@@ -561,7 +585,12 @@ export function useEnvironmentSpeechInput(input: HookInput) {
561585 if ( ! controller || detachedRecording ?. controller === controller ) return ;
562586 if (
563587 previousOwner === input . ownerKey ||
564- ! detachRecording ( controller , lastSnapshotRef . current , previousInputRef . current )
588+ ! detachRecording (
589+ controller ,
590+ audioConsentRef . current ,
591+ lastSnapshotRef . current ,
592+ previousInputRef . current ,
593+ )
565594 )
566595 controller . ownerChanged ( ) ;
567596 else setControllerState ( ( current ) => ( { ...current , value : INITIAL_STATE } ) ) ;
0 commit comments