Refactor queue storage
This commit is contained in:
parent
631151ab0b
commit
7fa3d67df4
@ -19,6 +19,8 @@ export class Scheduler {
|
||||
this.actionQueue = new ActionQueue();
|
||||
this.logger = new ConsoleLogger(this.constructor.name);
|
||||
|
||||
// this.taskQueue.push(GrabVillageState.name, {}, timestamp());
|
||||
|
||||
this.scheduleUniqTask(3600, SendOnAdventureTask.name);
|
||||
this.scheduleUniqTask(1200, BalanceHeroResourcesTask.name);
|
||||
this.scheduleUniqTask(180, GrabVillageState.name);
|
||||
|
@ -1,64 +1,57 @@
|
||||
import { Command } from '../Common';
|
||||
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 {
|
||||
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 type ActionList = Array<Command>;
|
||||
|
||||
export class ActionQueue {
|
||||
private storage: DataStorage;
|
||||
private readonly logger: Logger;
|
||||
|
||||
constructor() {
|
||||
this.storage = new DataStorage(NAMESPACE);
|
||||
this.logger = new ConsoleLogger(this.constructor.name);
|
||||
}
|
||||
|
||||
pop(): Command | undefined {
|
||||
const state = this.getState();
|
||||
const first = state.pop();
|
||||
this.flushState(state);
|
||||
const commands = this.getCommands();
|
||||
const first = commands.shift();
|
||||
this.flushState(commands);
|
||||
return first;
|
||||
}
|
||||
|
||||
push(cmd: Command): void {
|
||||
const state = this.getState();
|
||||
state.push(cmd);
|
||||
this.flushState(state);
|
||||
const commands = this.getCommands();
|
||||
commands.push(cmd);
|
||||
this.flushState(commands);
|
||||
}
|
||||
|
||||
assign(items: Array<Command>): void {
|
||||
this.flushState(new State(items));
|
||||
assign(commands: ActionList): void {
|
||||
this.flushState(commands);
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.flushState(new State([]));
|
||||
this.flushState([]);
|
||||
}
|
||||
|
||||
private getState(): State {
|
||||
const serialized = localStorage.getItem(QUEUE_NAME);
|
||||
if (serialized === null) {
|
||||
return new State([]);
|
||||
private getCommands(): ActionList {
|
||||
const serialized = this.storage.get(QUEUE_NAME);
|
||||
if (!Array.isArray(serialized)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
let parsed = JSON.parse(serialized) as State;
|
||||
this.logger.log('STATE', parsed);
|
||||
const items = serialized as Array<{ [key: string]: any }>;
|
||||
|
||||
return new State(parsed.items);
|
||||
return items.map(i => {
|
||||
const command = new Command('', {});
|
||||
return Object.assign(command, i);
|
||||
});
|
||||
}
|
||||
|
||||
private flushState(state: State): void {
|
||||
localStorage.setItem(QUEUE_NAME, JSON.stringify(state));
|
||||
private flushState(commands: ActionList): void {
|
||||
this.storage.set(QUEUE_NAME, commands);
|
||||
}
|
||||
}
|
||||
|
@ -18,9 +18,9 @@ export class DataStorage {
|
||||
|
||||
get(key: string): any {
|
||||
const fullKey = join(NAMESPACE, this.name, key);
|
||||
this.logger.log('GET', key);
|
||||
try {
|
||||
const serialized = localStorage.getItem(fullKey);
|
||||
this.logger.log('GET', fullKey, serialized);
|
||||
return JSON.parse(serialized || '"null"');
|
||||
} catch (e) {
|
||||
if (e instanceof SyntaxError) {
|
||||
|
@ -1,8 +1,10 @@
|
||||
import { Args } from '../Common';
|
||||
import { uniqId } from '../utils';
|
||||
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;
|
||||
|
||||
@ -31,8 +33,10 @@ export type TaskList = Array<Task>;
|
||||
|
||||
export class TaskQueue {
|
||||
private readonly logger: Logger;
|
||||
private storage: DataStorage;
|
||||
|
||||
constructor() {
|
||||
this.storage = new DataStorage(NAMESPACE);
|
||||
this.logger = new ConsoleLogger(this.constructor.name);
|
||||
}
|
||||
|
||||
@ -111,17 +115,21 @@ export class TaskQueue {
|
||||
}
|
||||
|
||||
private getItems(): TaskList {
|
||||
const serialized = localStorage.getItem(QUEUE_NAME);
|
||||
const storedItems = serialized !== null ? (JSON.parse(serialized) as Array<{ [key: string]: any }>) : [];
|
||||
const items: TaskList = [];
|
||||
storedItems.forEach(obj => {
|
||||
items.push(new Task(obj.id || uniqId(), +obj.ts, obj.name, obj.args));
|
||||
const serialized = this.storage.get(QUEUE_NAME);
|
||||
if (!Array.isArray(serialized)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
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 {
|
||||
const normalized = items.sort((x, y) => x.ts - y.ts);
|
||||
localStorage.setItem(QUEUE_NAME, JSON.stringify(normalized));
|
||||
this.storage.set(QUEUE_NAME, normalized);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user