Skip to content

Commit

Permalink
add cancelCompute func
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzhi713 committed Apr 26, 2022
1 parent bee09f6 commit 5b9ed7c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/algorithm/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const options = {
/**
* compute the width and left of the blocks contained in each day
*/
export async function computeBlockPositions(days: ScheduleDays) {
export function computeBlockPositions(days: ScheduleDays) {
const Module = window.NativeModule;

console.time('native compute');
Expand Down
14 changes: 9 additions & 5 deletions src/models/Schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ export default abstract class Schedule {
this.hover(key, false);
}

public cancelCompute() {
window.clearTimeout(this.pendingCompute);
}

/**
* Compute the schedule view based on `this.All` and `this.preview`.
* If there is a pending compute task, remove that pending task.
Expand All @@ -236,18 +240,18 @@ export default abstract class Schedule {
* However, because we're running on small input sets (usually contain no more than 20 sections), it usually completes within 50ms.
* @note it is the caller's responsibility to call constructDateSeparators, which is necessary if new classes are added
*/
public async computeSchedule(sync = true, time = 100) {
window.clearTimeout(this.pendingCompute);
public computeSchedule(sync = true, time = 100) {
this.cancelCompute();
if (sync) {
await this._computeSchedule();
this._computeSchedule();
} else {
this.pendingCompute = window.setTimeout(() => {
this._computeSchedule();
}, time);
}
}

private async _computeSchedule() {
private _computeSchedule() {
const catalog = window.catalog;
if (!catalog) return;

Expand Down Expand Up @@ -336,7 +340,7 @@ export default abstract class Schedule {
for (const event of this.events) if (event.display) this.place(event, days);

// const tStart = performance.now();
await computeBlockPositions(days);
computeBlockPositions(days);
// console.log('compute blocks', performance.now() - tStart);

const totalBlocks = days.reduce((sum, blocks) => sum + blocks.length, 0);
Expand Down
7 changes: 4 additions & 3 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ export default class Store extends Vue {
} else {
const semester = parsed.currentSemester || this.semester.semesters[0];
if (force) this.noti.info(`Updating ${semester.name} data...`, 3600, true);
// rare case: if a pending compute occurs after semester switch, an error may occur due to difference in semester courses
this.schedule.cancelComputeAll();
const msg = await this.semester.selectSemester(semester, force);
this.noti.notify(msg);
if (!msg.payload) return;
Expand Down Expand Up @@ -278,7 +280,7 @@ export default class Store extends Vue {
if (this.schedule.generated) this.generateSchedules();
else this.schedule.switchSchedule(false);

this.schedule.recomputeAll(false, 100); // need to compute all schedules for rendering
this.schedule.recomputeAll(true); // need to compute all schedules for rendering
}

/**
Expand Down Expand Up @@ -363,8 +365,7 @@ export default class Store extends Vue {
* Select a semester and fetch all its associated data,
* note that the first profile corresponding to the target semester will be loaded.
*
* @param target the semester to switch to
* @param force whether to force-update semester data
* @param target the semester to switch to. If null, switch to the latest semester
*/
async selectSemester(target: SemesterJSON | null) {
if (!this.semester.semesters.length) {
Expand Down
7 changes: 7 additions & 0 deletions src/store/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ export class ScheduleStore implements StoreModule<ScheduleState, ScheduleStateJS
});
}

cancelComputeAll() {
if (this.currentSchedule !== this.proposedSchedule) {
this.currentSchedule.cancelCompute();
}
this.proposedSchedules.forEach(s => s.cancelCompute());
}

/**
* @note the schedules parsed from JSON are **not** computed (i.e. the `computeSchedule` method is not called)
* @note the `currentSchedule` is not assigned.
Expand Down

0 comments on commit 5b9ed7c

Please sign in to comment.