Add pause feature
This commit is contained in:
		| @@ -1,4 +1,4 @@ | |||||||
| import { parseLocation, uniqId, waitForLoad } from './utils'; | import { parseLocation, timestamp, uniqId, waitForLoad } from './utils'; | ||||||
| import { Scheduler } from './Scheduler'; | import { Scheduler } from './Scheduler'; | ||||||
| import { BuildingPageController } from './Page/BuildingPageController'; | import { BuildingPageController } from './Page/BuildingPageController'; | ||||||
| import { UpgradeBuildingTask } from './Task/UpgradeBuildingTask'; | import { UpgradeBuildingTask } from './Task/UpgradeBuildingTask'; | ||||||
| @@ -20,6 +20,7 @@ import { calcGatheringTimings } from './Core/GatheringTimings'; | |||||||
| import { DataStorage } from './DataStorage'; | import { DataStorage } from './DataStorage'; | ||||||
| import { getBuildingPageAttributes, isBuildingPage } from './Page/PageDetectors'; | import { getBuildingPageAttributes, isBuildingPage } from './Page/PageDetectors'; | ||||||
| import { debounce } from 'debounce'; | import { debounce } from 'debounce'; | ||||||
|  | import { ExecutionState } from './State/ExecutionState'; | ||||||
|  |  | ||||||
| interface QuickAction { | interface QuickAction { | ||||||
|     label: string; |     label: string; | ||||||
| @@ -48,6 +49,8 @@ export class ControlPanel { | |||||||
|         const scheduler = this.scheduler; |         const scheduler = this.scheduler; | ||||||
|         const quickActions: QuickAction[] = []; |         const quickActions: QuickAction[] = []; | ||||||
|  |  | ||||||
|  |         const executionState = new ExecutionState(); | ||||||
|  |  | ||||||
|         const state = { |         const state = { | ||||||
|             name: 'Dashboard', |             name: 'Dashboard', | ||||||
|             version: this.version, |             version: this.version, | ||||||
| @@ -56,10 +59,13 @@ export class ControlPanel { | |||||||
|             taskList: [], |             taskList: [], | ||||||
|             actionList: [], |             actionList: [], | ||||||
|             quickActions: quickActions, |             quickActions: quickActions, | ||||||
|  |             pauseSeconds: 0, | ||||||
|  |  | ||||||
|             refresh() { |             refresh() { | ||||||
|                 this.taskList = scheduler.getTaskItems(); |                 this.taskList = scheduler.getTaskItems(); | ||||||
|                 this.actionList = scheduler.getActionItems(); |                 this.actionList = scheduler.getActionItems(); | ||||||
|  |                 const { pauseTs } = executionState.getExecutionSettings(); | ||||||
|  |                 this.pauseSeconds = pauseTs - timestamp(); | ||||||
|                 this.refreshVillages(); |                 this.refreshVillages(); | ||||||
|             }, |             }, | ||||||
|  |  | ||||||
| @@ -78,6 +84,10 @@ export class ControlPanel { | |||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|             }, |             }, | ||||||
|  |  | ||||||
|  |             pause() { | ||||||
|  |                 executionState.setExecutionSettings({ pauseTs: timestamp() + 120 }); | ||||||
|  |             }, | ||||||
|         }; |         }; | ||||||
|  |  | ||||||
|         state.refresh(); |         state.refresh(); | ||||||
|   | |||||||
| @@ -3,6 +3,9 @@ | |||||||
|     <h1 class="title"> |     <h1 class="title"> | ||||||
|       [{{ shared.name }}] {{ villageName }} |       [{{ shared.name }}] {{ villageName }} | ||||||
|       <span class="version">- {{ shared.version }}</span> |       <span class="version">- {{ shared.version }}</span> | ||||||
|  |       <a href="#" v-on:click.prevent="pause()"> | ||||||
|  |         pause <span v-if="shared.pauseSeconds" v-text="shared.pauseSeconds"></span> | ||||||
|  |       </a> | ||||||
|     </h1> |     </h1> | ||||||
|   </section> |   </section> | ||||||
| </template> | </template> | ||||||
| @@ -20,6 +23,11 @@ export default { | |||||||
|       return village ? village.name : 'Unknown'; |       return village ? village.name : 'Unknown'; | ||||||
|     }, |     }, | ||||||
|   }, |   }, | ||||||
|  |   methods: { | ||||||
|  |     pause() { | ||||||
|  |       this.shared.pause(); | ||||||
|  |     }, | ||||||
|  |   }, | ||||||
| }; | }; | ||||||
| </script> | </script> | ||||||
|  |  | ||||||
|   | |||||||
| @@ -25,6 +25,7 @@ | |||||||
|  |  | ||||||
| <script> | <script> | ||||||
| import * as dateFormat from 'dateformat'; | import * as dateFormat from 'dateformat'; | ||||||
|  | import { timestamp } from '../utils'; | ||||||
|  |  | ||||||
| export default { | export default { | ||||||
|   data() { |   data() { | ||||||
|   | |||||||
| @@ -9,12 +9,18 @@ import { ConsoleLogger, Logger } from './Logger'; | |||||||
| import { GrabberManager } from './Grabber/GrabberManager'; | import { GrabberManager } from './Grabber/GrabberManager'; | ||||||
| import { Scheduler } from './Scheduler'; | import { Scheduler } from './Scheduler'; | ||||||
| import { Statistics } from './Statistics'; | import { Statistics } from './Statistics'; | ||||||
|  | import { ExecutionState } from './State/ExecutionState'; | ||||||
|  |  | ||||||
|  | export interface ExecutionSettings { | ||||||
|  |     pauseTs: number; | ||||||
|  | } | ||||||
|  |  | ||||||
| export class Executor { | export class Executor { | ||||||
|     private readonly version: string; |     private readonly version: string; | ||||||
|     private readonly scheduler: Scheduler; |     private readonly scheduler: Scheduler; | ||||||
|     private grabbers: GrabberManager; |     private grabbers: GrabberManager; | ||||||
|     private statistics: Statistics; |     private statistics: Statistics; | ||||||
|  |     private executionState: ExecutionState; | ||||||
|     private logger: Logger; |     private logger: Logger; | ||||||
|  |  | ||||||
|     constructor(version: string, scheduler: Scheduler) { |     constructor(version: string, scheduler: Scheduler) { | ||||||
| @@ -22,6 +28,7 @@ export class Executor { | |||||||
|         this.scheduler = scheduler; |         this.scheduler = scheduler; | ||||||
|         this.grabbers = new GrabberManager(); |         this.grabbers = new GrabberManager(); | ||||||
|         this.statistics = new Statistics(); |         this.statistics = new Statistics(); | ||||||
|  |         this.executionState = new ExecutionState(); | ||||||
|         this.logger = new ConsoleLogger(this.constructor.name); |         this.logger = new ConsoleLogger(this.constructor.name); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -44,6 +51,12 @@ export class Executor { | |||||||
|             } else { |             } else { | ||||||
|                 await sleepMicro(); |                 await sleepMicro(); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|  |             const { pauseTs } = this.executionState.getExecutionSettings(); | ||||||
|  |             if (pauseTs > timestamp()) { | ||||||
|  |                 continue; | ||||||
|  |             } | ||||||
|  |  | ||||||
|             await this.doTaskProcessingStep(); |             await this.doTaskProcessingStep(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|   | |||||||
							
								
								
									
										23
									
								
								src/State/ExecutionState.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/State/ExecutionState.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | import { DataStorage } from '../DataStorage'; | ||||||
|  | import { ExecutionSettings } from '../Executor'; | ||||||
|  |  | ||||||
|  | const NAMESPACE = 'execution.v1'; | ||||||
|  |  | ||||||
|  | const SETTINGS_KEY = 'settings'; | ||||||
|  |  | ||||||
|  | export class ExecutionState { | ||||||
|  |     private storage: DataStorage; | ||||||
|  |     constructor() { | ||||||
|  |         this.storage = new DataStorage(NAMESPACE); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     getExecutionSettings(): ExecutionSettings { | ||||||
|  |         return this.storage.getTyped<ExecutionSettings>(SETTINGS_KEY, { | ||||||
|  |             factory: () => ({ pauseTs: 0 }), | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     setExecutionSettings(statistics: ExecutionSettings): void { | ||||||
|  |         this.storage.set(SETTINGS_KEY, statistics); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user