Refactor queue storage

This commit is contained in:
Anton Vakhrushev 2020-04-18 12:03:08 +03:00
parent 631151ab0b
commit 7fa3d67df4
4 changed files with 45 additions and 42 deletions

View File

@ -19,6 +19,8 @@ export class Scheduler {
this.actionQueue = new ActionQueue(); this.actionQueue = new ActionQueue();
this.logger = new ConsoleLogger(this.constructor.name); this.logger = new ConsoleLogger(this.constructor.name);
// this.taskQueue.push(GrabVillageState.name, {}, timestamp());
this.scheduleUniqTask(3600, SendOnAdventureTask.name); this.scheduleUniqTask(3600, SendOnAdventureTask.name);
this.scheduleUniqTask(1200, BalanceHeroResourcesTask.name); this.scheduleUniqTask(1200, BalanceHeroResourcesTask.name);
this.scheduleUniqTask(180, GrabVillageState.name); this.scheduleUniqTask(180, GrabVillageState.name);

View File

@ -1,64 +1,57 @@
import { Command } from '../Common'; import { Command } from '../Common';
import { ConsoleLogger, Logger } from '../Logger'; import { ConsoleLogger, Logger } from '../Logger';
import { DataStorage } from './DataStorage';
const QUEUE_NAME = 'action_queue:v2'; const NAMESPACE = 'actions.v1';
const QUEUE_NAME = 'queue';
class State { export type ActionList = Array<Command>;
items: Array<Command>;
constructor(items: Array<Command>) {
this.items = items;
}
pop(): Command | undefined {
return this.items.shift();
}
push(cmd: Command) {
this.items.push(cmd);
}
}
export class ActionQueue { export class ActionQueue {
private storage: DataStorage;
private readonly logger: Logger; private readonly logger: Logger;
constructor() { constructor() {
this.storage = new DataStorage(NAMESPACE);
this.logger = new ConsoleLogger(this.constructor.name); this.logger = new ConsoleLogger(this.constructor.name);
} }
pop(): Command | undefined { pop(): Command | undefined {
const state = this.getState(); const commands = this.getCommands();
const first = state.pop(); const first = commands.shift();
this.flushState(state); this.flushState(commands);
return first; return first;
} }
push(cmd: Command): void { push(cmd: Command): void {
const state = this.getState(); const commands = this.getCommands();
state.push(cmd); commands.push(cmd);
this.flushState(state); this.flushState(commands);
} }
assign(items: Array<Command>): void { assign(commands: ActionList): void {
this.flushState(new State(items)); this.flushState(commands);
} }
clear(): void { clear(): void {
this.flushState(new State([])); this.flushState([]);
} }
private getState(): State { private getCommands(): ActionList {
const serialized = localStorage.getItem(QUEUE_NAME); const serialized = this.storage.get(QUEUE_NAME);
if (serialized === null) { if (!Array.isArray(serialized)) {
return new State([]); return [];
} }
let parsed = JSON.parse(serialized) as State; const items = serialized as Array<{ [key: string]: any }>;
this.logger.log('STATE', parsed);
return new State(parsed.items); return items.map(i => {
const command = new Command('', {});
return Object.assign(command, i);
});
} }
private flushState(state: State): void { private flushState(commands: ActionList): void {
localStorage.setItem(QUEUE_NAME, JSON.stringify(state)); this.storage.set(QUEUE_NAME, commands);
} }
} }

View File

@ -18,9 +18,9 @@ export class DataStorage {
get(key: string): any { get(key: string): any {
const fullKey = join(NAMESPACE, this.name, key); const fullKey = join(NAMESPACE, this.name, key);
this.logger.log('GET', key);
try { try {
const serialized = localStorage.getItem(fullKey); const serialized = localStorage.getItem(fullKey);
this.logger.log('GET', fullKey, serialized);
return JSON.parse(serialized || '"null"'); return JSON.parse(serialized || '"null"');
} catch (e) { } catch (e) {
if (e instanceof SyntaxError) { if (e instanceof SyntaxError) {

View File

@ -1,8 +1,10 @@
import { Args } from '../Common'; import { Args } from '../Common';
import { uniqId } from '../utils'; import { uniqId } from '../utils';
import { ConsoleLogger, Logger } from '../Logger'; import { ConsoleLogger, Logger } from '../Logger';
import { DataStorage } from './DataStorage';
const QUEUE_NAME = 'task_queue:v4'; const NAMESPACE = 'tasks:v1';
const QUEUE_NAME = 'queue';
export type TaskId = string; export type TaskId = string;
@ -31,8 +33,10 @@ export type TaskList = Array<Task>;
export class TaskQueue { export class TaskQueue {
private readonly logger: Logger; private readonly logger: Logger;
private storage: DataStorage;
constructor() { constructor() {
this.storage = new DataStorage(NAMESPACE);
this.logger = new ConsoleLogger(this.constructor.name); this.logger = new ConsoleLogger(this.constructor.name);
} }
@ -111,17 +115,21 @@ export class TaskQueue {
} }
private getItems(): TaskList { private getItems(): TaskList {
const serialized = localStorage.getItem(QUEUE_NAME); const serialized = this.storage.get(QUEUE_NAME);
const storedItems = serialized !== null ? (JSON.parse(serialized) as Array<{ [key: string]: any }>) : []; if (!Array.isArray(serialized)) {
const items: TaskList = []; return [];
storedItems.forEach(obj => { }
items.push(new Task(obj.id || uniqId(), +obj.ts, obj.name, obj.args));
const storedItems = serialized as Array<{ [key: string]: any }>;
return storedItems.map(i => {
const task = new Task(uniqId(), 0, '', {});
return Object.assign(task, i);
}); });
return items;
} }
private flushItems(items: TaskList): void { private flushItems(items: TaskList): void {
const normalized = items.sort((x, y) => x.ts - y.ts); const normalized = items.sort((x, y) => x.ts - y.ts);
localStorage.setItem(QUEUE_NAME, JSON.stringify(normalized)); this.storage.set(QUEUE_NAME, normalized);
} }
} }