import {Utils} from '../../../common/Utils'; export interface TaskQueEntry { data: I; promise: { obj: Promise, resolve: Function, reject: Function }; } export class TaskQue { private tasks: TaskQueEntry[] = []; private processing: TaskQueEntry[] = []; constructor() { } private getSameTask(input: I): TaskQueEntry { return this.tasks.find(t => Utils.equalsFilter(t.data, input)) || this.processing.find(t => Utils.equalsFilter(t.data, input)); } private putNewTask(input: I): TaskQueEntry { const taskEntry: TaskQueEntry = { data: input, promise: { obj: null, resolve: null, reject: null } }; this.tasks.push(taskEntry); taskEntry.promise.obj = new Promise((resolve: Function, reject: Function) => { taskEntry.promise.reject = reject; taskEntry.promise.resolve = resolve; }); return taskEntry; } public isEmpty(): boolean { return this.tasks.length === 0; } public add(input: I): TaskQueEntry { return (this.getSameTask(input) || this.putNewTask(input)); } public get(): TaskQueEntry { const task = this.tasks.shift(); this.processing.push(task); return task; } public ready(task: TaskQueEntry): void { this.processing.slice(this.processing.indexOf(task), 1); } }