Add rescheduling
This commit is contained in:
		| @@ -1,7 +1,9 @@ | |||||||
| export class TryLaterError extends Error { | export class TryLaterError extends Error { | ||||||
|     readonly seconds: number; |     readonly seconds: number; | ||||||
|     constructor(s: number) { |     constructor(s: number, msg: string = '') { | ||||||
|         super(); |         super(msg); | ||||||
|         this.seconds = s; |         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); |             console.warn('ACTION ERROR', e); | ||||||
|             if (e instanceof TryLaterError) { |             if (e instanceof TryLaterError) { | ||||||
|                 console.warn('TRY AFTER', e.seconds); |                 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)); |         this.flushState(new State(items)); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     clear(): void { | ||||||
|  |         this.flushState(new State([])); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     private getState(): State { |     private getState(): State { | ||||||
|         const serialized = localStorage.getItem(QUEUE_NAME); |         const serialized = localStorage.getItem(QUEUE_NAME); | ||||||
|         if (serialized === null) { |         if (serialized === null) { | ||||||
|   | |||||||
| @@ -37,6 +37,16 @@ class State { | |||||||
|         } |         } | ||||||
|         return new State(first, items); |         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 { | export default class TaskQueue { | ||||||
| @@ -58,6 +68,15 @@ export default class TaskQueue { | |||||||
|         return current; |         return current; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     postpone(ds: number) { | ||||||
|  |         const state = this.getState().postpone(ds); | ||||||
|  |         this.flushState(state); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     state(): State { | ||||||
|  |         return this.getState(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     private defaultTs(): number { |     private defaultTs(): number { | ||||||
|         return Math.floor(Date.now() / 1000); |         return Math.floor(Date.now() / 1000); | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user