Add rescheduling

This commit is contained in:
Anton Vakhrushev 2020-03-29 15:56:33 +03:00
parent c6e535856a
commit 6e5b8f5ebf
4 changed files with 29 additions and 2 deletions

View File

@ -1,7 +1,9 @@
export class TryLaterError extends Error {
readonly seconds: number;
constructor(s: number) {
super();
constructor(s: number, msg: string = '') {
super(msg);
this.seconds = s;
// Set the prototype explicitly.
Object.setPrototypeOf(this, TryLaterError.prototype);
}
}

View File

@ -96,6 +96,8 @@ export default class Scheduler {
console.warn('ACTION ERROR', e);
if (e instanceof TryLaterError) {
console.warn('TRY AFTER', e.seconds);
this.actionQueue.clear();
this.taskQueue.postpone(e.seconds);
}
}
}

View File

@ -35,6 +35,10 @@ export default class ActionQueue {
this.flushState(new State(items));
}
clear(): void {
this.flushState(new State([]));
}
private getState(): State {
const serialized = localStorage.getItem(QUEUE_NAME);
if (serialized === null) {

View File

@ -37,6 +37,16 @@ class State {
}
return new State(first, items);
}
postpone(ds: number): State {
const current = this.current;
let items = this.items.slice();
if (current) {
const cmd = new CommandWithTime(current.cmd, current.ts + ds);
items.push(cmd);
}
return new State(null, items);
}
}
export default class TaskQueue {
@ -58,6 +68,15 @@ export default class TaskQueue {
return current;
}
postpone(ds: number) {
const state = this.getState().postpone(ds);
this.flushState(state);
}
state(): State {
return this.getState();
}
private defaultTs(): number {
return Math.floor(Date.now() / 1000);
}