Skip to content

Commit

Permalink
VUU-54: Refactor promises
Browse files Browse the repository at this point in the history
  • Loading branch information
pling-scottlogic committed Oct 2, 2023
1 parent 7ac3c4f commit f5b7123
Showing 1 changed file with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<LayoutMetadata, "id">, newLayoutJson: LayoutJSON): Promise<void> {
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));
Expand All @@ -39,9 +47,11 @@ export class LocalLayoutPersistenceManager implements LayoutPersistenceManager {
deleteLayout(id: string): Promise<void> {
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();
})
Expand All @@ -52,8 +62,9 @@ export class LocalLayoutPersistenceManager implements LayoutPersistenceManager {
loadLayout(id: string): Promise<LayoutJSON> {
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));
Expand Down

0 comments on commit f5b7123

Please sign in to comment.