diff --git a/src/abstractions/collection.service.ts b/src/abstractions/collection.service.ts index 041db130bc..64bf22def6 100644 --- a/src/abstractions/collection.service.ts +++ b/src/abstractions/collection.service.ts @@ -9,6 +9,7 @@ export abstract class CollectionService { clearCache: () => void; encrypt: (model: CollectionView) => Promise; + decryptMany: (collections: Collection[]) => Promise; get: (id: string) => Promise; getAll: () => Promise; getAllDecrypted: () => Promise; diff --git a/src/services/collection.service.ts b/src/services/collection.service.ts index 8cf1b8ac17..ce26f72dcc 100644 --- a/src/services/collection.service.ts +++ b/src/services/collection.service.ts @@ -41,6 +41,19 @@ export class CollectionService implements CollectionServiceAbstraction { return collection; } + async decryptMany(collections: Collection[]): Promise { + if (collections == null) { + return []; + } + const decCollections: CollectionView[] = []; + const promises: Array> = []; + collections.forEach((collection) => { + promises.push(collection.decrypt().then((c) => decCollections.push(c))); + }); + await Promise.all(promises); + return decCollections.sort(this.getLocaleSortingFunction()); + } + async get(id: string): Promise { const userId = await this.userService.getUserId(); const collections = await this.storageService.get<{ [id: string]: CollectionData; }>( @@ -75,16 +88,8 @@ export class CollectionService implements CollectionServiceAbstraction { throw new Error('No key.'); } - const decCollections: CollectionView[] = []; - const promises: Array> = []; const collections = await this.getAll(); - collections.forEach((collection) => { - promises.push(collection.decrypt().then((c) => decCollections.push(c))); - }); - - await Promise.all(promises); - decCollections.sort(this.getLocaleSortingFunction()); - this.decryptedCollectionCache = decCollections; + this.decryptedCollectionCache = await this.decryptMany(collections); return this.decryptedCollectionCache; }