2021-06-14 22:03:55 -04:00
|
|
|
import { Serializable } from 'frigid';
|
2021-06-22 19:25:41 -04:00
|
|
|
import { Task, taskClasses } from './registries/Tasks.js';
|
|
|
|
|
import { render, Renderable, panels } from './ui/UI.js';
|
2021-06-14 22:03:55 -04:00
|
|
|
|
|
|
|
|
export class TaskList extends Serializable implements Renderable {
|
|
|
|
|
tasks: Task[] = [];
|
2021-06-15 15:02:47 -04:00
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
|
for(const task of this.tasks) {
|
|
|
|
|
this.removeTask(task);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-14 22:03:55 -04:00
|
|
|
|
|
|
|
|
static serializationDependencies() {
|
2021-06-22 22:38:49 -04:00
|
|
|
let a = Array.from(taskClasses.values())
|
|
|
|
|
return a as unknown as (typeof Serializable)[];
|
2021-06-14 22:03:55 -04:00
|
|
|
}
|
|
|
|
|
|
2021-06-22 19:25:41 -04:00
|
|
|
addTask({taskId, options}: TaskOptions) {
|
|
|
|
|
if(!taskClasses.has(taskId))
|
|
|
|
|
throw new Error('unknown task: ' + taskId);
|
|
|
|
|
|
2021-06-22 22:38:49 -04:00
|
|
|
// this is any because TS doesnt understand that
|
|
|
|
|
// static references and constructors are the
|
|
|
|
|
// SAME THING.
|
|
|
|
|
// in TS, they're MAGICALLY incompatible...
|
|
|
|
|
const taskClass: any = taskClasses.get(taskId);
|
2021-06-22 19:25:41 -04:00
|
|
|
const task = new taskClass();
|
|
|
|
|
|
2021-06-14 22:03:55 -04:00
|
|
|
this.tasks = [...this.tasks, task];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeTask(task) {
|
|
|
|
|
this.tasks = this.tasks.filter(v => v !== task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
// const width = tasksPanel.width;
|
2021-06-22 19:25:41 -04:00
|
|
|
panels.left.setContent(`${this.tasks.map(task => {
|
2021-06-14 22:03:55 -04:00
|
|
|
return task.toString();
|
|
|
|
|
}).join('\n')}`);
|
|
|
|
|
// return this.tasks.map(task => task.toString()).join('\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-22 19:25:41 -04:00
|
|
|
|
|
|
|
|
const taskTypes = {};
|
|
|
|
|
export function registerTask(name, clazz) {
|
|
|
|
|
taskTypes[name] = clazz;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type TaskOptions = {
|
|
|
|
|
taskId: string,
|
|
|
|
|
options: any
|
|
|
|
|
}
|