From 4ae4b667ffa317537fdf1be5d4b594781778186a Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 6 Jul 2018 12:40:36 -0400 Subject: [PATCH] decrypt many collections service function --- src/abstractions/collection.service.ts | 1 + src/services/collection.service.ts | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/abstractions/collection.service.ts b/src/abstractions/collection.service.ts index 041db130bcc..64bf22def6a 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 8cf1b8ac17a..ce26f72dcc9 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; }