Browse Source

Await promises in parallel

master
Menci 6 years ago
parent
commit
d47854e620
  1. 11
      models/common.ts
  2. 10
      models/judge_state.ts

11
models/common.ts

@ -173,12 +173,15 @@ export default class Model extends TypeORM.BaseEntity {
result.meta.top = result.data[0].id;
result.meta.bottom = result.data[result.data.length - 1].id;
result.meta.hasPrevPage = !!(await queryBuilderHasPrev.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '>' : '<'} :id`, {
// Run two queries in parallel.
await Promise.all(([
async () => result.meta.hasPrevPage = !!(await queryBuilderHasPrev.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '>' : '<'} :id`, {
id: result.meta.top
}).take(1).getOne());
result.meta.hasNextPage = !!(await queryBuilderHasNext.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '<' : '>'} :id`, {
}).take(1).getOne()),
async () => result.meta.hasNextPage = !!(await queryBuilderHasNext.andWhere(`id ${idOrder === PaginationIDOrder.DESC ? '<' : '>'} :id`, {
id: result.meta.bottom
}).take(1).getOne());
}).take(1).getOne())
]).map(f => f()));
return result;
}

10
models/judge_state.ts

@ -133,13 +133,15 @@ export default class JudgeState extends Model {
if (this.type === 0) {
await this.loadRelationships();
// No need to await them.
this.user.refreshSubmitInfo();
this.problem.resetSubmissionCount();
const promises = [];
promises.push(this.user.refreshSubmitInfo());
promises.push(this.problem.resetSubmissionCount());
if (!newSubmission) {
this.problem.updateStatistics(this.user_id);
promises.push(this.problem.updateStatistics(this.user_id));
}
await Promise.all(promises);
} else if (this.type === 1) {
let contest = await Contest.findById(this.type_info);
await contest.newSubmission(this);

Loading…
Cancel
Save