Skip to content

Commit

Permalink
Change uses of then to async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
backspace committed Dec 23, 2023
1 parent fdb8d5e commit cd239f5
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 181 deletions.
39 changes: 15 additions & 24 deletions gathering/app/controllers/destination.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,17 @@ export default class DestinationController extends Controller {
}

@action
save(model) {
model
.save()
.then(() => {
return model.get('region');
})
.then((region) => {
if (region) {
this.lastRegion.setLastRegionId(region.id);
}
async save(model) {
await model.save();

return region ? region.save() : true;
})
.then(() => {
this.router.transitionTo('destinations');
});
let region = model.get('region');

if (region) {
this.lastRegion.setLastRegionId(region.id);
await region.save();
}

this.router.transitionTo('destinations');
}

@action
Expand All @@ -53,16 +48,12 @@ export default class DestinationController extends Controller {
}

@action
delete(model) {
async delete(model) {
// This is an unfortunate workaround to address test errors of this form:
// Attempted to handle event `pushedData` on … while in state root.deleted.inFlight
model
.reload()
.then((reloaded) => {
return reloaded.destroyRecord();
})
.then(() => {
this.router.transitionTo('destinations');
});
await model.reload();
await model.destroyRecord();

this.router.transitionTo('destinations');
}
}
40 changes: 17 additions & 23 deletions gathering/app/controllers/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,18 @@ export default class RegionController extends Controller {
}

@action
save(model) {
return model
.save()
.then(() => {
this.lastRegion.setLastRegionId(model.id);

return model.get('parent');
})
.then((parent) => {
return parent ? parent.save() : true;
})
.then(() => {
this.router.transitionTo('regions');
});
async save(model) {
await model.save();

this.lastRegion.setLastRegionId(model.id);

let parent = model.get('parent');

if (parent) {
await parent.save();
}

this.router.transitionTo('regions');
}

@action
Expand All @@ -45,14 +43,10 @@ export default class RegionController extends Controller {
}

@action
delete(model) {
model
.reload()
.then((reloaded) => {
return reloaded.destroyRecord();
})
.then(() => {
this.router.transitionTo('regions');
});
async delete(model) {
await model.reload();
await model.destroyRecord();

this.router.transitionTo('regions');
}
}
31 changes: 15 additions & 16 deletions gathering/app/controllers/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,25 @@ export default class SchedulerController extends Controller {
this.meeting.teams.push(team);
}

@action saveMeeting() {
@action async saveMeeting() {
const meeting = this.meeting;

meeting.set('offset', this.meetingOffset);

meeting
.save()
.then(() => {
return Promise.all([meeting.get('destination'), meeting.get('teams')]);
})
.then(([destination, teams]) => {
return Promise.all([
destination.save(),
...teams.map((team) => team.save()),
]);
})
.then(() => {
this.meeting = this.store.createRecord('meeting');
this.meetingOffsetOverride = undefined;
});
await meeting.save();

let [destination, teams] = await Promise.all([
meeting.get('destination'),
meeting.get('teams'),
]);

await Promise.all([
destination.save(),
...teams.map((team) => team.save()),
]);

this.meeting = this.store.createRecord('meeting');
this.meetingOffsetOverride = undefined;
}

@action resetMeeting() {
Expand Down
20 changes: 8 additions & 12 deletions gathering/app/controllers/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,14 @@ export default class SyncController extends Controller {
this.result = undefined;
this.syncPromise = syncPromise;

yield syncPromise
.then((result) => {
run(() => {
this.result = result;
});
})
.catch((error) => {
run(() => {
console.log('error with sync:', stringify(error));
this.error = error;
});
});
try {
let result = yield syncPromise;

this.result = result;
} catch (error) {
console.log('error with sync:', stringify(error));
this.error = error;
}
})
sync;

Expand Down
41 changes: 16 additions & 25 deletions gathering/app/controllers/waypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,17 @@ export default class WaypointController extends Controller {
}

@action
save(model) {
model
.save()
.then(() => {
return model.get('region');
})
.then((region) => {
if (region) {
this.lastRegion.setLastRegionId(region.id);
}

return region ? region.save() : true;
})
.then(() => {
this.router.transitionTo('waypoints');
});
async save(model) {
await model.save();

let region = model.get('region');

if (region) {
this.lastRegion.setLastRegionId(region.id);
await region.save();
}

this.router.transitionTo('waypoints');
}

@action
Expand All @@ -47,16 +42,12 @@ export default class WaypointController extends Controller {
}

@action
delete(model) {
async delete(model) {
// This is an unfortunate workaround to address test errors of this form:
// Attempted to handle event `pushedData` on … while in state root.deleted.inFlight
model
.reload()
.then((reloaded) => {
return reloaded.destroyRecord();
})
.then(() => {
this.router.transitionTo('waypoints');
});
await model.reload();
await model.destroyRecord();

this.router.transitionTo('destinations');
}
}
6 changes: 2 additions & 4 deletions gathering/app/routes/destination.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ export default class DestinationRoute extends Route {

@tracked regions;

beforeModel() {
return this.store
.findAll('region')
.then((regions) => (this.regions = regions));
async beforeModel() {
this.regions = await this.store.findAll('region');
}

setupController(controller, model) {
Expand Down
9 changes: 3 additions & 6 deletions gathering/app/routes/destinations/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ export default class NewRoute extends DestinationRoute {

@service store;

model() {
async model() {
if (this.controllerFor('destinations').region) {
return this.store.createRecord('destination', {
region: this.controllerFor('destinations').region,
});
}

const lastRegion = this.lastRegion.getLastRegion();

return lastRegion.then((region) => {
return this.store.createRecord('destination', { region });
});
const lastRegion = await this.lastRegion.getLastRegion();
return this.store.createRecord('destination', { region: lastRegion });
}

templateName = 'destination';
Expand Down
27 changes: 12 additions & 15 deletions gathering/app/routes/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export default class OutputRoute extends Route {
console.log('Unable to fetch map');
}

let fontResponses = await Promise.all(fontPaths);
let [header, bold, regular] = await Promise.all(
fontResponses.map((response) => response.arrayBuffer()),
);

return hash({
teams: this.store.findAll('team'),
meetings: this.store.findAll('meeting'),
Expand All @@ -67,21 +72,13 @@ export default class OutputRoute extends Route {

settings: this.store.findRecord('settings', 'settings'),

assets: Promise.all(fontPaths)
.then((responses) => {
return Promise.all(
responses.map((response) => response.arrayBuffer()),
);
})
.then(([header, bold, regular]) => {
return hash({
header,
bold,
regular,
map,
lowMap,
});
}),
assets: {
header,
bold,
regular,
map,
lowMap,
},
});
}

Expand Down
6 changes: 2 additions & 4 deletions gathering/app/routes/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ export default class RegionRoute extends Route {

@tracked regions;

beforeModel() {
return this.store
.findAll('region')
.then((regions) => (this.regions = regions));
async beforeModel() {
this.regions = await this.store.findAll('region');
}

setupController(controller, model) {
Expand Down
6 changes: 2 additions & 4 deletions gathering/app/routes/waypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ export default class WaypointRoute extends Route {

@tracked regions;

beforeModel() {
return this.store
.findAll('region')
.then((regions) => (this.regions = regions));
async beforeModel() {
this.regions = await this.store.findAll('region');
}

setupController(controller, model) {
Expand Down
9 changes: 3 additions & 6 deletions gathering/app/routes/waypoints/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ export default class NewRoute extends WaypointRoute {

@service store;

model() {
async model() {
if (this.controllerFor('waypoints').region) {
return this.store.createRecord('waypoint', {
region: this.controllerFor('waypoints').region,
});
}

const lastRegion = this.lastRegion.getLastRegion();

return lastRegion.then((region) => {
return this.store.createRecord('waypoint', { region });
});
const lastRegion = await this.lastRegion.getLastRegion();
return this.store.createRecord('waypoint', { region: lastRegion });
}

templateName = 'waypoint';
Expand Down
Loading

0 comments on commit cd239f5

Please sign in to comment.