From f5b71232b58d33c45342ab544a25efdd42486594 Mon Sep 17 00:00:00 2001 From: Peter Ling Date: Mon, 2 Oct 2023 16:27:25 +0100 Subject: [PATCH] VUU-54: Refactor promises --- .../LocalLayoutPersistenceManager.ts | 45 ++++++++++++------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/vuu-ui/packages/vuu-layout/src/layout-persistence/LocalLayoutPersistenceManager.ts b/vuu-ui/packages/vuu-layout/src/layout-persistence/LocalLayoutPersistenceManager.ts index d620283235..b6b5e62853 100644 --- a/vuu-ui/packages/vuu-layout/src/layout-persistence/LocalLayoutPersistenceManager.ts +++ b/vuu-ui/packages/vuu-layout/src/layout-persistence/LocalLayoutPersistenceManager.ts @@ -12,24 +12,32 @@ export class LocalLayoutPersistenceManager implements LayoutPersistenceManager { return new Promise(async (resolve) => { console.log(`Saving layout as ${metadata.name} to group ${metadata.group}...`); - const existingLayouts = await this.loadLayouts(); - const existingMetadata = await this.loadMetadata(); - - const id = getUniqueId(); - - this.appendAndPersist(id, metadata, layout, existingLayouts, existingMetadata); - - resolve(id); + Promise.all([this.loadLayouts(), this.loadMetadata()]) + .then(result => { + const [existingLayouts, existingMetadata] = result; + const id = getUniqueId(); + this.appendAndPersist( + id, + metadata, + layout, + existingLayouts, + existingMetadata + ); + resolve(id); + }); }) } + updateLayout(id: string, metadata: Omit, newLayoutJson: LayoutJSON): Promise { return new Promise(async (resolve, reject) => { this.validateIds(id) - .then(async () => { - const existingLayouts = (await this.loadLayouts()).filter(layout => layout.id !== id); - const existingMetadata = (await this.loadMetadata()).filter(metadata => metadata.id !== id); - this.appendAndPersist(id, metadata, newLayoutJson, existingLayouts, existingMetadata); + .then(() => Promise.all([this.loadLayouts(), this.loadMetadata()])) + .then(result => { + const [existingLayouts, existingMetadata] = result; + const newLayouts = existingLayouts.filter(layout => layout.id !== id); + const newMetadata = existingMetadata.filter(metadata => metadata.id !== id); + this.appendAndPersist(id, metadata, newLayoutJson, newLayouts, newMetadata); resolve(); }) .catch(e => reject(e)); @@ -39,9 +47,11 @@ export class LocalLayoutPersistenceManager implements LayoutPersistenceManager { deleteLayout(id: string): Promise { return new Promise(async (resolve, reject) => { this.validateIds(id) - .then(async () => { - const layouts = (await this.loadLayouts()).filter((layout) => layout.id !== id); - const metadata = (await this.loadMetadata()).filter(metadata => metadata.id !== id); + .then(() => Promise.all([this.loadLayouts(), this.loadMetadata()])) + .then(result => { + const [existingLayouts, existingMetadata] = result; + const layouts = existingLayouts.filter((layout) => layout.id !== id); + const metadata = existingMetadata.filter(metadata => metadata.id !== id); this.saveLayoutsWithMetadata(layouts, metadata); resolve(); }) @@ -52,8 +62,9 @@ export class LocalLayoutPersistenceManager implements LayoutPersistenceManager { loadLayout(id: string): Promise { return new Promise((resolve, reject) => { this.validateId(id, false) - .then(async () => { - const layouts = (await this.loadLayouts()).filter(layout => layout.id === id); + .then(() => this.loadLayouts()) + .then(result => { + const layouts = result.filter(layout => layout.id === id); resolve(layouts[0].json); }) .catch(e => reject(e));