mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-14 00:31:25 +10:00
fix: eslint errors, switch to using maps
This commit is contained in:
@ -1,89 +1,93 @@
|
||||
import type { FilterConditionally } from "./types";
|
||||
|
||||
interface PriorityTagged<T> {
|
||||
object: T,
|
||||
priority: number, // Higher takes priority
|
||||
addedIndex: number, // Lower takes priority
|
||||
object: T;
|
||||
priority: number; // Higher takes priority
|
||||
addedIndex: number; // Lower takes priority
|
||||
}
|
||||
|
||||
export class PriorityList<T> {
|
||||
private source: Array<PriorityTagged<T>> = [];
|
||||
private cachedSorted: Array<T> | undefined;
|
||||
private source: Array<PriorityTagged<T>> = [];
|
||||
private cachedSorted: Array<T> | undefined;
|
||||
|
||||
push(item: T, priority: number = 0) {
|
||||
this.source.push({
|
||||
object: item,
|
||||
priority,
|
||||
addedIndex: this.source.length,
|
||||
});
|
||||
this.cachedSorted = undefined;
|
||||
push(item: T, priority: number = 0) {
|
||||
this.source.push({
|
||||
object: item,
|
||||
priority,
|
||||
addedIndex: this.source.length,
|
||||
});
|
||||
this.cachedSorted = undefined;
|
||||
}
|
||||
|
||||
pop(index: number = 0) {
|
||||
this.cachedSorted = undefined;
|
||||
return this.source.splice(index, 1)[0];
|
||||
}
|
||||
|
||||
values() {
|
||||
if (this.cachedSorted !== undefined) {
|
||||
return this.cachedSorted;
|
||||
}
|
||||
|
||||
pop(index: number = 0) {
|
||||
this.cachedSorted = undefined;
|
||||
return this.source.splice(index, 1)[0];
|
||||
}
|
||||
|
||||
values() {
|
||||
if (this.cachedSorted !== undefined) {
|
||||
return this.cachedSorted;
|
||||
const sorted = this.source
|
||||
.sort((a, b) => {
|
||||
if (a.priority == a.priority) {
|
||||
return a.addedIndex - b.addedIndex;
|
||||
}
|
||||
|
||||
const sorted = this.source.sort((a, b) => {
|
||||
if (a.priority == a.priority) {
|
||||
return a.addedIndex - b.addedIndex;
|
||||
}
|
||||
return b.priority - a.priority;
|
||||
})
|
||||
.map((e) => e.object);
|
||||
this.cachedSorted = sorted;
|
||||
|
||||
return b.priority - a.priority;
|
||||
}).map((e) => e.object);
|
||||
this.cachedSorted = sorted;
|
||||
return this.cachedSorted;
|
||||
}
|
||||
|
||||
return this.cachedSorted;
|
||||
}
|
||||
|
||||
find(predicate: (value: T, index: number, obj: T[]) => boolean) {
|
||||
return this.source.map((e) => e.object).find(predicate);
|
||||
}
|
||||
find(predicate: (value: T, index: number, obj: T[]) => boolean) {
|
||||
return this.source.map((e) => e.object).find(predicate);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
type IndexableProperty<T> = keyof FilterConditionally<T, (() => string) | string>;
|
||||
type IndexableProperty<T> = keyof FilterConditionally<
|
||||
T,
|
||||
(() => string) | string
|
||||
>;
|
||||
export class PriorityListIndexed<T> extends PriorityList<T> {
|
||||
private indexName: IndexableProperty<T>;
|
||||
private indexMap: { [key: string]: T } = {};
|
||||
private indexName: IndexableProperty<T>;
|
||||
private indexMap = new Map<string, T>();
|
||||
|
||||
constructor(indexName: IndexableProperty<T>) {
|
||||
super();
|
||||
this.indexName = indexName;
|
||||
constructor(indexName: IndexableProperty<T>) {
|
||||
super();
|
||||
this.indexName = indexName;
|
||||
}
|
||||
|
||||
private getIndex(object: T): string {
|
||||
const index = object[this.indexName];
|
||||
|
||||
if (typeof index === "function") {
|
||||
return index();
|
||||
}
|
||||
|
||||
private getIndex(object: T): string {
|
||||
const index = object[this.indexName];
|
||||
return index as string;
|
||||
}
|
||||
|
||||
if (typeof index === 'function') {
|
||||
return index();
|
||||
}
|
||||
override push(item: T, priority?: number): void {
|
||||
const index = this.getIndex(item);
|
||||
this.indexMap.set(index, item);
|
||||
|
||||
return index as string;
|
||||
}
|
||||
super.push(item, priority);
|
||||
}
|
||||
|
||||
override push(item: T, priority?: number): void {
|
||||
const index = this.getIndex(item);
|
||||
this.indexMap[index] = item;
|
||||
override pop(position?: number): PriorityTagged<T> {
|
||||
const value = super.pop(position);
|
||||
|
||||
super.push(item, priority);
|
||||
}
|
||||
const index = this.getIndex(value.object);
|
||||
this.indexMap.delete(index);
|
||||
|
||||
override pop(position?: number): PriorityTagged<T> {
|
||||
const value = super.pop(position);
|
||||
return value;
|
||||
}
|
||||
|
||||
const index = this.getIndex(value.object);
|
||||
delete this.indexMap[index];
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
get(index: string) {
|
||||
return this.indexMap[index];
|
||||
}
|
||||
}
|
||||
get(index: string) {
|
||||
return this.indexMap.get(index);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user