Files
orca/mobile/src/query/native-query-persistence.ts
T
Kelvin Amoaba 8b1c394a41 perf(mobile): open Home with last-known workspaces and counts (#40129)
* perf(mobile): open Home with last-known workspaces and counts

* fix(mobile): keep disconnected desktops on Home and judge agents by the client's connect time

* fix(mobile): store the seeded Home counts before removing the old snapshot

* fix(mobile): floor router reads after failures, catch up reads that span a stream reopen, seed the wide host list from Home

* fix(mobile): keep the old Home snapshot until the store can take it, and never re-date stored totals

* perf(mobile): key Home's counts in the host list module so the page boot never loads Home's code

* test(mobile): type the Home catalog adapter's modules with type imports

* perf(mobile): read Home's stored rows in one batch beside the host list, and keep mark-read a delta read

* perf(mobile): paint Home's desktop rows without waiting for the stored counts

* perf(mobile): restore Home's stored rows after the desktop list paints, from its host ids

* perf(mobile): read Home's counts on the connected edge itself, not a render later
2026-11-09 14:06:15 +00:00

71 lines
2.6 KiB
TypeScript

import { AppState, Platform } from 'react-native'
import type { QueryClient } from '@tanstack/react-query'
import { processDeviceStore } from '../device-store/device-store'
import {
createHostQueryPersistence,
hostQueryPersistenceOf,
type HostQueryPersistence
} from './host-query-persistence'
import { hostScopeKey } from './host-query-key'
// Per native world, the paired hosts whose data it keeps in the device store.
const keptHosts = new WeakMap<QueryClient, Set<string>>()
function keptHostsOf(queryClient: QueryClient): Set<string> {
const existing = keptHosts.get(queryClient)
if (existing) {
return existing
}
const hosts = new Set<string>()
keptHosts.set(queryClient, hosts)
// Pending writes go to disk when the app leaves the foreground, as a page's do on hide.
AppState.addEventListener('change', (state) => {
if (state !== 'active') {
for (const hostId of hosts) {
void hostQueryPersistenceOf(queryClient, hostId)?.flush()
}
}
})
return hosts
}
/** Keeps the native world's data for one paired host in the device store, read and written directly. */
export function keepNativeHostQueries(
queryClient: QueryClient,
hostId: string
): HostQueryPersistence | null {
// The web build of the native app has no device store.
if (Platform.OS === 'web') {
return null
}
const existing = hostQueryPersistenceOf(queryClient, hostId)
if (existing) {
return existing
}
const hosts = keptHostsOf(queryClient)
hosts.add(hostId)
const store = processDeviceStore()
return createHostQueryPersistence(queryClient, {
hostId,
read: (keys) => store.read(hostId, 'cache', keys),
// A write still pending when the host is removed must not land after the wipe.
write: async (ops) =>
hosts.has(hostId)
? { written: (await store.write(hostId, 'cache', ops)).written }
: { written: false }
})
}
/** Whether this world's writes can reach disk now: false on web, or while the store's key can't load. */
export async function nativeQueryStoreReady(): Promise<boolean> {
return Platform.OS !== 'web' && (await processDeviceStore().open()) === 'ready'
}
/** Drops a removed host's queries from this world and stops storing them; the store wipe follows. */
export function forgetNativeHostQueries(queryClient: QueryClient, hostId: string): void {
keptHosts.get(queryClient)?.delete(hostId)
// Removed while the persistence still listens, so it forgets what it restored for this host.
queryClient.removeQueries({ queryKey: hostScopeKey(hostId) })
hostQueryPersistenceOf(queryClient, hostId)?.stop()
}