fix(mobile): delete cleanup audio files on retry and isolate screen busy state across user switches
This commit is contained in:
parent
bd3c1a98d9
commit
cc3f7460be
5 changed files with 376 additions and 45 deletions
|
|
@ -473,6 +473,17 @@ export async function processQueuedAudioNow(
|
|||
return processItem(item);
|
||||
}
|
||||
|
||||
async function finishCleanupItem(item: DurableQueueItem): Promise<void> {
|
||||
try {
|
||||
await removeOwnedFile(item.path);
|
||||
await removeItem(item);
|
||||
} catch {
|
||||
if (!suspendedUsers.has(item.userId)) {
|
||||
scheduler.arm(item.userId, Date.now() + 60_000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* One pass over the user's queue. The listing only fixes the visiting order;
|
||||
* every item is re-read right before acting on it, so work done meanwhile by
|
||||
|
|
@ -492,12 +503,7 @@ async function runResumePass(userId: string): Promise<void> {
|
|||
if (item === undefined || item.userId !== userId) continue;
|
||||
if (item.status === 'terminal') continue;
|
||||
if (item.status === 'cleanup') {
|
||||
try {
|
||||
await removeOwnedFile(item.path);
|
||||
await removeItem(item);
|
||||
} catch {
|
||||
scheduler.arm(userId, Date.now() + 60_000);
|
||||
}
|
||||
await finishCleanupItem(item);
|
||||
continue;
|
||||
}
|
||||
if (item.nextAttemptAtMs > Date.now()) {
|
||||
|
|
@ -532,7 +538,7 @@ export async function retryQueuedAudio(userId: string, itemId: string): Promise<
|
|||
if (item === undefined) throw new AudioPipelineError('file-read', 'Queued audio was not found');
|
||||
if (item.status === 'running' || runningItemPromises.has(item.id)) return;
|
||||
if (item.status === 'cleanup') {
|
||||
await removeItem(item);
|
||||
await finishCleanupItem(item);
|
||||
return;
|
||||
}
|
||||
await replaceItem({
|
||||
|
|
|
|||
|
|
@ -98,6 +98,14 @@ export default function CommandsScreen(): React.ReactElement {
|
|||
const styles = useMemo(() => createStyles(palette), [palette])
|
||||
const alive = useRef(true)
|
||||
const request = useRef<AbortController | null>(null)
|
||||
const loadGeneration = useRef(0)
|
||||
const actorUserIdRef = useRef<string | null>(user?.id ?? null)
|
||||
const scopeGenerationRef = useRef(0)
|
||||
const isCurrentActor = useCallback((actorUserId: string | null, actorScope: number): boolean => (
|
||||
alive.current
|
||||
&& actorUserIdRef.current === actorUserId
|
||||
&& scopeGenerationRef.current === actorScope
|
||||
), [])
|
||||
const [commands, setCommands] = useState<SyncedCommand[]>([])
|
||||
const [activeId, setActiveId] = useState<string | null>(null)
|
||||
const [selectedId, setSelectedId] = useState<string | null>(null)
|
||||
|
|
@ -123,16 +131,20 @@ export default function CommandsScreen(): React.ReactElement {
|
|||
), [t])
|
||||
|
||||
const load = useCallback(async (pull = false, silent = false): Promise<void> => {
|
||||
if (!user?.id) {
|
||||
const generation = ++loadGeneration.current
|
||||
const actorUserId = user?.id ?? null
|
||||
const actorScope = scopeGenerationRef.current
|
||||
if (!actorUserId) {
|
||||
setLoading(false)
|
||||
setRefreshing(false)
|
||||
setSyncError('auth')
|
||||
return
|
||||
}
|
||||
if (pull) setRefreshing(true)
|
||||
else if (!silent) setLoading(true)
|
||||
try {
|
||||
const catalog = await loadSyncedCommandCatalog(user.id)
|
||||
if (!alive.current) return
|
||||
const catalog = await loadSyncedCommandCatalog(actorUserId)
|
||||
if (!isCurrentActor(actorUserId, actorScope) || generation !== loadGeneration.current) return
|
||||
setCommands(catalog.commands)
|
||||
setActiveId(catalog.activeInstructionId)
|
||||
setSelectedId((current) => (
|
||||
|
|
@ -142,19 +154,51 @@ export default function CommandsScreen(): React.ReactElement {
|
|||
))
|
||||
setSyncError(null)
|
||||
} catch (error) {
|
||||
if (alive.current) setSyncError(error instanceof CommandSyncError ? error.code : 'server')
|
||||
if (isCurrentActor(actorUserId, actorScope) && generation === loadGeneration.current) {
|
||||
setSyncError(error instanceof CommandSyncError ? error.code : 'server')
|
||||
}
|
||||
} finally {
|
||||
if (alive.current) {
|
||||
if (isCurrentActor(actorUserId, actorScope) && generation === loadGeneration.current) {
|
||||
setLoading(false)
|
||||
setRefreshing(false)
|
||||
}
|
||||
}
|
||||
}, [isCurrentActor, user?.id])
|
||||
|
||||
useEffect(() => {
|
||||
const nextUserId = user?.id ?? null
|
||||
if (actorUserIdRef.current === nextUserId) return
|
||||
actorUserIdRef.current = nextUserId
|
||||
scopeGenerationRef.current += 1
|
||||
loadGeneration.current += 1
|
||||
request.current?.abort()
|
||||
request.current = null
|
||||
setCommands([])
|
||||
setActiveId(null)
|
||||
setSelectedId(null)
|
||||
setSyncError(null)
|
||||
setMutationId(null)
|
||||
setEditor(null)
|
||||
setEditorError(null)
|
||||
setInput('')
|
||||
setOutput(null)
|
||||
setReportTarget(null)
|
||||
setRunning(false)
|
||||
setRunError(null)
|
||||
setRetryable(false)
|
||||
}, [user?.id])
|
||||
|
||||
useFocusEffect(useCallback(() => { void load() }, [load]))
|
||||
useEffect(() => () => {
|
||||
alive.current = false
|
||||
request.current?.abort()
|
||||
useFocusEffect(useCallback(() => {
|
||||
void load()
|
||||
return () => { loadGeneration.current += 1 }
|
||||
}, [load]))
|
||||
useEffect(() => {
|
||||
alive.current = true
|
||||
return () => {
|
||||
alive.current = false
|
||||
loadGeneration.current += 1
|
||||
request.current?.abort()
|
||||
}
|
||||
}, [])
|
||||
useEffect(() => {
|
||||
if (!user?.id) return undefined
|
||||
|
|
@ -176,25 +220,21 @@ export default function CommandsScreen(): React.ReactElement {
|
|||
return () => { void supabase.removeChannel(channel) }
|
||||
}, [load, user?.id])
|
||||
|
||||
const actorUserIdRef = useRef<string | null>(user?.id ?? null)
|
||||
useEffect(() => {
|
||||
actorUserIdRef.current = user?.id ?? null
|
||||
}, [user?.id])
|
||||
|
||||
async function toggleActive(command: SyncedCommand): Promise<void> {
|
||||
if (!user?.id || mutationId) return
|
||||
const actorUserId = user.id
|
||||
const actorScope = scopeGenerationRef.current
|
||||
setMutationId(command.id)
|
||||
try {
|
||||
const nextActiveId = await setActiveSyncedCommand(actorUserId, activeId === command.id ? null : command.id)
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
if (!isCurrentActor(actorUserId, actorScope)) return
|
||||
setActiveId(nextActiveId)
|
||||
setSyncError(null)
|
||||
} catch (error) {
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
if (!isCurrentActor(actorUserId, actorScope)) return
|
||||
setSyncError(error instanceof CommandSyncError ? error.code : 'server')
|
||||
} finally {
|
||||
if (alive.current) setMutationId(null)
|
||||
if (isCurrentActor(actorUserId, actorScope)) setMutationId(null)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -217,6 +257,7 @@ export default function CommandsScreen(): React.ReactElement {
|
|||
async function save(): Promise<void> {
|
||||
if (!editor || !user?.id || mutationId) return
|
||||
const actorUserId = user.id
|
||||
const actorScope = scopeGenerationRef.current
|
||||
setMutationId(editor.command?.id ?? 'create')
|
||||
setEditorError(null)
|
||||
try {
|
||||
|
|
@ -226,38 +267,39 @@ export default function CommandsScreen(): React.ReactElement {
|
|||
...editor,
|
||||
sortOrder: Math.max(1000, ...commands.map((item) => item.sortOrder + 10)),
|
||||
})
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
if (!isCurrentActor(actorUserId, actorScope)) return
|
||||
const next = [...commands.filter((item) => item.id !== saved.id), saved]
|
||||
.sort((a, b) => a.sortOrder - b.sortOrder || a.createdAt.localeCompare(b.createdAt))
|
||||
setCommands(next)
|
||||
setSelectedId(saved.id)
|
||||
setEditor(null)
|
||||
} catch (error) {
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
if (!isCurrentActor(actorUserId, actorScope)) return
|
||||
const code = error instanceof CommandSyncError ? error.code : 'server'
|
||||
setEditorError(code)
|
||||
if (code === 'conflict') void load(false, true)
|
||||
} finally {
|
||||
if (alive.current) setMutationId(null)
|
||||
if (isCurrentActor(actorUserId, actorScope)) setMutationId(null)
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(command: SyncedCommand): Promise<void> {
|
||||
if (!user?.id || mutationId) return
|
||||
const actorUserId = user.id
|
||||
const actorScope = scopeGenerationRef.current
|
||||
setMutationId(command.id)
|
||||
try {
|
||||
await deleteSyncedCommand(actorUserId, command)
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
if (!isCurrentActor(actorUserId, actorScope)) return
|
||||
const next = commands.filter((item) => item.id !== command.id)
|
||||
setCommands(next)
|
||||
if (selectedId === command.id) setSelectedId(next[0]?.id ?? null)
|
||||
if (activeId === command.id) setActiveId(null)
|
||||
} catch (error) {
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
if (!isCurrentActor(actorUserId, actorScope)) return
|
||||
setSyncError(error instanceof CommandSyncError ? error.code : 'server')
|
||||
} finally {
|
||||
if (alive.current) setMutationId(null)
|
||||
if (isCurrentActor(actorUserId, actorScope)) setMutationId(null)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -275,22 +317,24 @@ export default function CommandsScreen(): React.ReactElement {
|
|||
async function move(command: SyncedCommand, direction: 'up' | 'down'): Promise<void> {
|
||||
if (!user?.id || mutationId) return
|
||||
const actorUserId = user.id
|
||||
const actorScope = scopeGenerationRef.current
|
||||
setMutationId(command.id)
|
||||
try {
|
||||
const reordered = await reorderSyncedCommand(actorUserId, command, direction)
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
if (!isCurrentActor(actorUserId, actorScope)) return
|
||||
setCommands(reordered)
|
||||
} catch (error) {
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
if (!isCurrentActor(actorUserId, actorScope)) return
|
||||
setSyncError(error instanceof CommandSyncError ? error.code : 'server')
|
||||
} finally {
|
||||
if (alive.current) setMutationId(null)
|
||||
if (isCurrentActor(actorUserId, actorScope)) setMutationId(null)
|
||||
}
|
||||
}
|
||||
|
||||
const run = useCallback(async (): Promise<void> => {
|
||||
if (!selected || running) return
|
||||
const actorUserId = actorUserIdRef.current
|
||||
const actorScope = scopeGenerationRef.current
|
||||
const token = session?.access_token ?? ''
|
||||
if (!token) {
|
||||
setRunError('AUTH_REQUIRED')
|
||||
|
|
@ -308,10 +352,10 @@ export default function CommandsScreen(): React.ReactElement {
|
|||
model: preferences.preferredLlmModel,
|
||||
signal: controller.signal,
|
||||
})
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
if (!isCurrentActor(actorUserId, actorScope)) return
|
||||
setOutput(result)
|
||||
} catch (error) {
|
||||
if (!alive.current || actorUserIdRef.current !== actorUserId) return
|
||||
if (!isCurrentActor(actorUserId, actorScope)) return
|
||||
const failure = error instanceof CommandExecutionError
|
||||
? error
|
||||
: new CommandExecutionError('SERVER_ERROR', true)
|
||||
|
|
@ -321,9 +365,9 @@ export default function CommandsScreen(): React.ReactElement {
|
|||
}
|
||||
} finally {
|
||||
if (request.current === controller) request.current = null
|
||||
if (alive.current) setRunning(false)
|
||||
if (isCurrentActor(actorUserId, actorScope)) setRunning(false)
|
||||
}
|
||||
}, [input, preferences.preferredLlmModel, running, selected, session?.access_token])
|
||||
}, [input, isCurrentActor, preferences.preferredLlmModel, running, selected, session?.access_token])
|
||||
|
||||
return (
|
||||
<KeyboardAvoidingView
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ export default function TeamsScreen({ navigation }: TeamsScreenProps): React.Rea
|
|||
const loadGeneration = useRef(0)
|
||||
const mountedRef = useRef(true)
|
||||
const userIdRef = useRef<string | null>(user?.id ?? null)
|
||||
const scopeGenerationRef = useRef(0)
|
||||
const [teams, setTeams] = useState<TeamSummary[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [refreshing, setRefreshing] = useState(false)
|
||||
|
|
@ -80,7 +81,16 @@ export default function TeamsScreen({ navigation }: TeamsScreenProps): React.Rea
|
|||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
userIdRef.current = user?.id ?? null
|
||||
const nextUserId = user?.id ?? null
|
||||
if (userIdRef.current === nextUserId) return
|
||||
userIdRef.current = nextUserId
|
||||
scopeGenerationRef.current += 1
|
||||
setTeams([])
|
||||
setError(null)
|
||||
setEditorOpen(false)
|
||||
setName('')
|
||||
setEditorBusy(false)
|
||||
setEditorError(null)
|
||||
}, [user?.id])
|
||||
|
||||
useFocusEffect(useCallback(() => {
|
||||
|
|
@ -91,20 +101,26 @@ export default function TeamsScreen({ navigation }: TeamsScreenProps): React.Rea
|
|||
const submitCreate = async (): Promise<void> => {
|
||||
if (user === null || editorBusy) return
|
||||
const actorUserId = user.id
|
||||
const actorScope = scopeGenerationRef.current
|
||||
const isCurrentActor = (): boolean => (
|
||||
mountedRef.current
|
||||
&& userIdRef.current === actorUserId
|
||||
&& scopeGenerationRef.current === actorScope
|
||||
)
|
||||
setEditorBusy(true)
|
||||
setEditorError(null)
|
||||
try {
|
||||
const team = await createTeam(actorUserId, name)
|
||||
if (!mountedRef.current || userIdRef.current !== actorUserId) return
|
||||
if (!isCurrentActor()) return
|
||||
setTeams((current) => [team, ...current.filter((row) => row.id !== team.id)])
|
||||
setName('')
|
||||
setEditorOpen(false)
|
||||
navigation.navigate('TeamDetail', { teamId: team.id })
|
||||
} catch (requestError) {
|
||||
if (!mountedRef.current || userIdRef.current !== actorUserId) return
|
||||
if (!isCurrentActor()) return
|
||||
setEditorError(teamErrorCode(requestError))
|
||||
} finally {
|
||||
if (mountedRef.current) setEditorBusy(false)
|
||||
if (isCurrentActor()) setEditorBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue