Use immutable arrays for export queue items
This commit is contained in:
parent
c203666280
commit
d30dffb579
@ -5,7 +5,9 @@ import { DataStorage } from '../DataStorage';
|
|||||||
const NAMESPACE = 'actions.v1';
|
const NAMESPACE = 'actions.v1';
|
||||||
const QUEUE_NAME = 'queue';
|
const QUEUE_NAME = 'queue';
|
||||||
|
|
||||||
export type ActionList = Array<Command>;
|
type ActionList = Array<Command>;
|
||||||
|
|
||||||
|
export type ImmutableActionList = ReadonlyArray<Command>;
|
||||||
|
|
||||||
export class ActionQueue {
|
export class ActionQueue {
|
||||||
private storage: DataStorage;
|
private storage: DataStorage;
|
||||||
@ -37,7 +39,7 @@ export class ActionQueue {
|
|||||||
this.flushState([]);
|
this.flushState([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
seeItems(): ActionList {
|
seeItems(): ImmutableActionList {
|
||||||
return this.getCommands();
|
return this.getCommands();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +34,9 @@ export class Task {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TaskList = Array<Task>;
|
type TaskList = Array<Task>;
|
||||||
|
|
||||||
|
export type ImmutableTaskList = ReadonlyArray<Task>;
|
||||||
|
|
||||||
export class TaskQueue {
|
export class TaskQueue {
|
||||||
private readonly logger: Logger;
|
private readonly logger: Logger;
|
||||||
@ -79,7 +81,7 @@ export class TaskQueue {
|
|||||||
this.flushItems(items);
|
this.flushItems(items);
|
||||||
}
|
}
|
||||||
|
|
||||||
seeItems(): TaskList {
|
seeItems(): ImmutableTaskList {
|
||||||
return this.getItems();
|
return this.getItems();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
import { timestamp } from './utils';
|
import { timestamp } from './utils';
|
||||||
import { UpgradeBuildingTask } from './Task/UpgradeBuildingTask';
|
import { UpgradeBuildingTask } from './Task/UpgradeBuildingTask';
|
||||||
import { Task, TaskId, TaskList, TaskQueue } from './Queue/TaskQueue';
|
import { ImmutableTaskList, Task, TaskId, TaskQueue } from './Queue/TaskQueue';
|
||||||
import { Args, Command } from './Command';
|
import { Args, Command } from './Command';
|
||||||
import { SendOnAdventureTask } from './Task/SendOnAdventureTask';
|
import { SendOnAdventureTask } from './Task/SendOnAdventureTask';
|
||||||
import { BalanceHeroResourcesTask } from './Task/BalanceHeroResourcesTask';
|
import { BalanceHeroResourcesTask } from './Task/BalanceHeroResourcesTask';
|
||||||
import { ConsoleLogger, Logger } from './Logger';
|
import { ConsoleLogger, Logger } from './Logger';
|
||||||
import { BuildBuildingTask } from './Task/BuildBuildingTask';
|
import { BuildBuildingTask } from './Task/BuildBuildingTask';
|
||||||
import { GrabVillageState } from './Task/GrabVillageState';
|
import { GrabVillageState } from './Task/GrabVillageState';
|
||||||
import { ActionList, ActionQueue } from './Queue/ActionQueue';
|
import { ActionQueue, ImmutableActionList } from './Queue/ActionQueue';
|
||||||
import { Resources, ResourcesInterface } from './Game';
|
import { Resources, ResourcesInterface } from './Game';
|
||||||
import { UpdateResourceContracts } from './Task/UpdateResourceContracts';
|
import { UpdateResourceContracts } from './Task/UpdateResourceContracts';
|
||||||
import { TrainTroopTask } from './Task/TrainTroopTask';
|
import { TrainTroopTask } from './Task/TrainTroopTask';
|
||||||
@ -40,11 +40,11 @@ export class Scheduler {
|
|||||||
setInterval(taskScheduler, seconds * 1000);
|
setInterval(taskScheduler, seconds * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
getTaskItems(): TaskList {
|
getTaskItems(): ImmutableTaskList {
|
||||||
return this.taskQueue.seeItems();
|
return this.taskQueue.seeItems();
|
||||||
}
|
}
|
||||||
|
|
||||||
getActionItems(): ActionList {
|
getActionItems(): ImmutableActionList {
|
||||||
return this.actionQueue.seeItems();
|
return this.actionQueue.seeItems();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,11 +161,11 @@ function withResources(task: Task, resources: ResourcesInterface): Task {
|
|||||||
return new Task(task.id, task.ts, task.name, { ...task.args, resources });
|
return new Task(task.id, task.ts, task.name, { ...task.args, resources });
|
||||||
}
|
}
|
||||||
|
|
||||||
function firstTaskTime(tasks: TaskList, predicate: (t: Task) => boolean): number | undefined {
|
function firstTaskTime(tasks: ImmutableTaskList, predicate: (t: Task) => boolean): number | undefined {
|
||||||
return tasks.find(predicate)?.ts;
|
return tasks.find(predicate)?.ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
function lastTaskTime(tasks: TaskList, predicate: (t: Task) => boolean): number | undefined {
|
function lastTaskTime(tasks: ImmutableTaskList, predicate: (t: Task) => boolean): number | undefined {
|
||||||
const queuedTaskIndex = findLastIndex(tasks, predicate);
|
const queuedTaskIndex = findLastIndex(tasks, predicate);
|
||||||
if (queuedTaskIndex === undefined) {
|
if (queuedTaskIndex === undefined) {
|
||||||
return undefined;
|
return undefined;
|
||||||
@ -173,7 +173,7 @@ function lastTaskTime(tasks: TaskList, predicate: (t: Task) => boolean): number
|
|||||||
return tasks[queuedTaskIndex].ts;
|
return tasks[queuedTaskIndex].ts;
|
||||||
}
|
}
|
||||||
|
|
||||||
function findLastIndex(tasks: TaskList, predicate: (t: Task) => boolean): number | undefined {
|
function findLastIndex(tasks: ImmutableTaskList, predicate: (t: Task) => boolean): number | undefined {
|
||||||
const count = tasks.length;
|
const count = tasks.length;
|
||||||
const indexInReversed = tasks
|
const indexInReversed = tasks
|
||||||
.slice()
|
.slice()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { TaskList } from './Queue/TaskQueue';
|
import { ImmutableTaskList } from './Queue/TaskQueue';
|
||||||
import { uniqId } from './utils';
|
import { uniqId } from './utils';
|
||||||
import * as dateFormat from 'dateformat';
|
import * as dateFormat from 'dateformat';
|
||||||
|
|
||||||
@ -10,7 +10,7 @@ function formatDate(ts: number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class TaskQueueRenderer {
|
export class TaskQueueRenderer {
|
||||||
render(tasks: TaskList) {
|
render(tasks: ImmutableTaskList) {
|
||||||
const ul = jQuery('<ul></ul>')
|
const ul = jQuery('<ul></ul>')
|
||||||
.attr({ id: ID })
|
.attr({ id: ID })
|
||||||
.css({
|
.css({
|
||||||
|
Loading…
Reference in New Issue
Block a user