Bläddra i källkod

updated getFiles to handle get single collection and store collectiontime after storing files

Abhinav-grd 4 år sedan
förälder
incheckning
f72ab9b418
1 ändrade filer med 45 tillägg och 41 borttagningar
  1. 45 41
      src/services/fileService.ts

+ 45 - 41
src/services/fileService.ts

@@ -76,8 +76,13 @@ export const fetchFiles = async (
     collections: collection[]
 ) => {
     let files = await localFiles();
-    const fetchedFiles = await getFiles(collections, null, 100, token);
-
+    const collectionUpdationTime = new Map<string, string>();
+    let fetchedFiles = [];
+    for (let collection of collections) {
+        const files = await getFiles(collection, null, 100, token);
+        fetchedFiles.push(...files);
+        collectionUpdationTime.set(collection.id, files.length > 0 ? files.slice(-1)[0].updationTime.toString() : "0");
+    }
     files.push(...fetchedFiles);
     var latestFiles = new Map<string, file>();
     files.forEach((file) => {
@@ -95,53 +100,52 @@ export const fetchFiles = async (
         (a, b) => b.metadata.creationTime - a.metadata.creationTime
     );
     await localForage.setItem('files', files);
+    for (let [collectionID, updationTime] of collectionUpdationTime) {
+        await localForage.setItem(`${collectionID}-time`, updationTime);
+    }
     return files;
 };
 
-export const getFiles = async (collections: collection[], sinceTime: string, limit: number, token: string): Promise<file[]> => {
+export const getFiles = async (collection: collection, sinceTime: string, limit: number, token: string): Promise<file[]> => {
     try {
         const worker = await new CryptoWorker();
         let promises: Promise<file>[] = [];
-        for (const index in collections) {
-            const collection = collections[index];
-            if (collection.isDeleted) {
-                // TODO: Remove files in this collection from localForage and cache
-                continue;
-            }
-            let time =
-                sinceTime || (await localForage.getItem<string>(`${collection.id}-time`)) || "0";
-            let resp;
-            do {
-                resp = await HTTPService.get(`${ENDPOINT}/collections/diff`, {
-                    collectionID: collection.id,
-                    sinceTime: time,
-                    limit: limit.toString(),
-                },
-                    {
-                        'X-Auth-Token': token
-                    });
-                promises.push(...resp.data.diff.map(
-                    async (file: file) => {
-                        if (!file.isDeleted) {
-
-                            file.key = await worker.decryptB64(
-                                file.encryptedKey,
-                                file.keyDecryptionNonce,
-                                collection.key
-                            );
-                            file.metadata = await worker.decryptMetadata(file);
-                        }
-                        return file;
+        if (collection.isDeleted) {
+            // TODO: Remove files in this collection from localForage and cache
+            return;
+        }
+        let time =
+            sinceTime || (await localForage.getItem<string>(`${collection.id}-time`)) || "0";
+        let resp;
+        do {
+            resp = await HTTPService.get(`${ENDPOINT}/collections/diff`, {
+                collectionID: collection.id,
+                sinceTime: time,
+                limit: limit.toString(),
+            },
+                {
+                    'X-Auth-Token': token
+                });
+            promises.push(...resp.data.diff.map(
+                async (file: file) => {
+                    if (!file.isDeleted) {
+
+                        file.key = await worker.decryptB64(
+                            file.encryptedKey,
+                            file.keyDecryptionNonce,
+                            collection.key
+                        );
+                        file.metadata = await worker.decryptMetadata(file);
                     }
-                ));
-
-                if (resp.data.diff.length) {
-                    time = resp.data.diff.slice(-1)[0].updationTime.toString();
+                    return file;
                 }
-            } while (resp.data.diff.length === limit);
-            await localForage.setItem(`${collection.id}-time`, time);
-        }
-        return Promise.all(promises);
+            ));
+
+            if (resp.data.diff.length) {
+                time = resp.data.diff.slice(-1)[0].updationTime.toString();
+            }
+        } while (resp.data.diff.length === limit);
+        return await Promise.all(promises);
     } catch (e) {
         console.log("Get files failed" + e);
     }