add saf directory display fix
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { listDirectory, listFolders } from '../services/safDirectory';
|
||||
import { listDirectory, listFoldersChunked, yieldToMainThread } from '../services/safDirectory';
|
||||
import type { FileEntry } from '../services/safDirectory.types';
|
||||
import {
|
||||
checkpointDatabase,
|
||||
getFiles,
|
||||
getFolder,
|
||||
getFolders,
|
||||
@@ -47,9 +48,9 @@ export async function syncRoot(rootResourceId: string): Promise<SyncResult> {
|
||||
return withTransaction(async () => {
|
||||
const seen = new Set<string>();
|
||||
|
||||
const folders = listFolders(root.uri as string, { recursive: true, includeRoot: true }).sort(
|
||||
(a, b) => uriDepth(a.uri) - uriDepth(b.uri),
|
||||
);
|
||||
const folders = (
|
||||
await listFoldersChunked(root.uri as string, { recursive: true, includeRoot: true })
|
||||
).sort((a, b) => uriDepth(a.uri) - uriDepth(b.uri));
|
||||
|
||||
const resourceIdByUri = new Map<string, string>();
|
||||
const savedFolders: StoredFolder[] = [];
|
||||
@@ -66,9 +67,14 @@ export async function syncRoot(rootResourceId: string): Promise<SyncResult> {
|
||||
savedFolders.push(saved);
|
||||
}
|
||||
|
||||
let lastYield = Date.now();
|
||||
let files = 0;
|
||||
for (const folder of savedFolders) {
|
||||
if (!folder.uri) continue;
|
||||
if (Date.now() - lastYield >= 16) {
|
||||
lastYield = Date.now();
|
||||
await yieldToMainThread();
|
||||
}
|
||||
for (const entry of listDirectory(folder.uri)) {
|
||||
if (entry.isDirectory) continue;
|
||||
seen.add(entry.uri);
|
||||
@@ -109,15 +115,18 @@ export async function syncDevice(): Promise<SyncResult[]> {
|
||||
for (const root of roots) {
|
||||
results.push(await syncRoot(root.resource_id));
|
||||
}
|
||||
await checkpointDatabase();
|
||||
return results;
|
||||
}
|
||||
|
||||
export async function useSyncDevice(intervalMs = 30_000): Promise<void> {
|
||||
const preferences = await getUserPreferences();
|
||||
if (preferences.syncMode === 'none') return;
|
||||
export function useSyncDevice(intervalMs = 30_000): () => void {
|
||||
let stopped = false;
|
||||
|
||||
while (true) {
|
||||
const tick = async () => {
|
||||
try {
|
||||
const preferences = await getUserPreferences();
|
||||
if (preferences.syncMode === 'none') return;
|
||||
|
||||
const results = await syncDevice();
|
||||
console.info('syncDevice', JSON.stringify(results));
|
||||
// Puis pousser l'outbox (si un token est disponible) et rafraîchir le
|
||||
@@ -133,6 +142,24 @@ export async function useSyncDevice(intervalMs = 30_000): Promise<void> {
|
||||
} catch (error) {
|
||||
console.warn('syncDevice failed, retrying later', error);
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
||||
}
|
||||
};
|
||||
|
||||
const loop = async () => {
|
||||
while (!stopped) {
|
||||
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
||||
if (stopped) return;
|
||||
await tick();
|
||||
}
|
||||
};
|
||||
|
||||
// Premier cycle différé : laisser le boot et l'écran actif répondre avant
|
||||
// de lancer un walk SAF (potentiellement long) en arrière-plan.
|
||||
const first = setTimeout(() => {
|
||||
void loop();
|
||||
}, 2000);
|
||||
|
||||
return () => {
|
||||
stopped = true;
|
||||
clearTimeout(first);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user