Add immutable state for tasks
This commit is contained in:
parent
233b33cbd4
commit
3905097151
@ -4,6 +4,7 @@ import { v4 as uuid } from 'uuid';
|
|||||||
import Scheduler from './Scheduler';
|
import Scheduler from './Scheduler';
|
||||||
import UpgradeBuildingTask from './Task/UpgradeBuildingTask';
|
import UpgradeBuildingTask from './Task/UpgradeBuildingTask';
|
||||||
import { Command } from './Common';
|
import { Command } from './Common';
|
||||||
|
import TaskQueueRenderer from './TaskQueueRenderer';
|
||||||
|
|
||||||
export default class Dashboard {
|
export default class Dashboard {
|
||||||
private scheduler: Scheduler;
|
private scheduler: Scheduler;
|
||||||
@ -19,6 +20,7 @@ export default class Dashboard {
|
|||||||
await sleepShort();
|
await sleepShort();
|
||||||
|
|
||||||
markPage('Dashboard');
|
markPage('Dashboard');
|
||||||
|
new TaskQueueRenderer().render(this.scheduler.taskState());
|
||||||
|
|
||||||
if (p.pathname === '/build.php') {
|
if (p.pathname === '/build.php') {
|
||||||
console.log('BUILD PAGE DETECTED');
|
console.log('BUILD PAGE DETECTED');
|
||||||
|
@ -3,14 +3,14 @@ import UpgradeBuildingTask from './Task/UpgradeBuildingTask';
|
|||||||
import GoToBuildingAction from './Action/GoToBuildingAction';
|
import GoToBuildingAction from './Action/GoToBuildingAction';
|
||||||
import UpgradeBuildingAction from './Action/UpgradeBuildingAction';
|
import UpgradeBuildingAction from './Action/UpgradeBuildingAction';
|
||||||
import { TryLaterError } from './Errors';
|
import { TryLaterError } from './Errors';
|
||||||
import { TaskQueue, State } from './Storage/TaskQueue';
|
import { TaskQueue, State, ImmutableState } from './Storage/TaskQueue';
|
||||||
import ActionQueue from './Storage/ActionQueue';
|
import ActionQueue from './Storage/ActionQueue';
|
||||||
import { Args, Command } from './Common';
|
import { Args, Command } from './Common';
|
||||||
import TaskQueueRenderer from './TaskQueueRenderer';
|
import TaskQueueRenderer from './TaskQueueRenderer';
|
||||||
|
|
||||||
export default class Scheduler {
|
export default class Scheduler {
|
||||||
taskQueue: TaskQueue;
|
private taskQueue: TaskQueue;
|
||||||
actionQueue: ActionQueue;
|
private actionQueue: ActionQueue;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.taskQueue = new TaskQueue();
|
this.taskQueue = new TaskQueue();
|
||||||
@ -45,6 +45,10 @@ export default class Scheduler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
taskState(): ImmutableState {
|
||||||
|
return this.taskQueue.state();
|
||||||
|
}
|
||||||
|
|
||||||
pushTask(task: Command): void {
|
pushTask(task: Command): void {
|
||||||
this.log('PUSH TASK', task);
|
this.log('PUSH TASK', task);
|
||||||
this.taskQueue.push(task);
|
this.taskQueue.push(task);
|
||||||
|
@ -49,6 +49,15 @@ export class State {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class ImmutableState {
|
||||||
|
readonly current: CommandWithTime | null;
|
||||||
|
readonly items: Array<CommandWithTime>;
|
||||||
|
constructor(state: State) {
|
||||||
|
this.current = state.current;
|
||||||
|
this.items = state.items;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class TaskQueue {
|
export class TaskQueue {
|
||||||
push(cmd: Command, ts: number | null = null) {
|
push(cmd: Command, ts: number | null = null) {
|
||||||
this.log('PUSH TASK', cmd, ts);
|
this.log('PUSH TASK', cmd, ts);
|
||||||
@ -73,8 +82,8 @@ export class TaskQueue {
|
|||||||
this.flushState(state);
|
this.flushState(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
state(): State {
|
state(): ImmutableState {
|
||||||
return this.getState();
|
return new ImmutableState(this.getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
private defaultTs(): number {
|
private defaultTs(): number {
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import { State } from './Storage/TaskQueue';
|
import { ImmutableState } from './Storage/TaskQueue';
|
||||||
|
|
||||||
const ID = 'id-832654376836436939356';
|
const ID = 'id-832654376836436939356';
|
||||||
|
|
||||||
export default class TaskQueueRenderer {
|
export default class TaskQueueRenderer {
|
||||||
render(state: State) {
|
render(state: ImmutableState) {
|
||||||
const ul = jQuery('<ul></ul>')
|
const ul = jQuery('<ul></ul>')
|
||||||
.attr({ id: ID })
|
.attr({ id: ID })
|
||||||
.css({
|
.css({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user