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:
@@ -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])')
|
||||
})
|
||||
})
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user