Move mobile dictation ref mirrors out of effect (#3234)

* Move mobile dictation refs out of effect

* Guard mobile dictation option refs

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Jinwoo-H <jinwoo0825@gmail.com>
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Neil
2026-05-30 19:19:17 -07:00
committed by GitHub
co-authored by Orca Jinwoo-H
parent 7366c3d32e
commit 2c55de5589
2 changed files with 33 additions and 2 deletions
@@ -0,0 +1,29 @@
import { readFileSync } from 'node:fs'
import { describe, expect, it } from 'vitest'
const source = readFileSync(new URL('./use-mobile-dictation.ts', import.meta.url), 'utf8')
function sliceBetween(startPattern: string, endPattern: string): string {
const start = source.indexOf(startPattern)
expect(start).toBeGreaterThanOrEqual(0)
const end = source.indexOf(endPattern, start)
expect(end).toBeGreaterThan(start)
return source.slice(start, end)
}
describe('useMobileDictation source invariants', () => {
it('publishes live option refs from committed renders before passive Effects flush', () => {
const refDeclarations = sliceBetween(
'const clientRef = useRef(client)',
'useLayoutEffect(() => {'
)
expect(refDeclarations).not.toContain('.current =')
const mirrorEffect = sliceBetween('useLayoutEffect(() => {', 'const reportError =')
expect(mirrorEffect).toContain('clientRef.current = client')
expect(mirrorEffect).toContain('enabledRef.current = enabled')
expect(mirrorEffect).toContain('onTranscriptRef.current = onTranscript')
expect(mirrorEffect).toContain('onErrorRef.current = onError')
expect(mirrorEffect).toContain('}, [client, enabled, onTranscript, onError])')
})
})
+4 -2
View File
@@ -1,7 +1,7 @@
/* oxlint-disable max-lines -- Why: mobile dictation keeps permission, recording,
* chunk upload, completion, and cancellation in one hook so native audio state
* cannot drift from the runtime RPC lifecycle. */
import { useCallback, useEffect, useRef, useState } from 'react'
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'
import { Buffer } from 'buffer'
import {
addExpoTwoWayAudioEventListener,
@@ -57,7 +57,9 @@ export function useMobileDictation(options: UseMobileDictationOptions): UseMobil
const generationRef = useRef(0)
const finishingIdRef = useRef<string | null>(null)
useEffect(() => {
useLayoutEffect(() => {
// Native audio events can arrive before passive Effects flush, but refs
// should only expose options from a committed render.
clientRef.current = client
enabledRef.current = enabled
onTranscriptRef.current = onTranscript