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