Add rescheduling
This commit is contained in:
		| @@ -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); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|   | ||||
| @@ -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) { | ||||
|   | ||||
| @@ -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); | ||||
|     } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user