Telegram Web K with changes to work inside I2P https://web.telegram.i2p/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.5 KiB

3 years ago
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
import insertInDescendSortedArray from "./array/insertInDescendSortedArray";
3 years ago
import { getMiddleware } from "./middleware";
import safeAssign from "./object/safeAssign";
3 years ago
3 years ago
export type SortedElementId = PeerId;
3 years ago
export type SortedElementBase = {
3 years ago
id: SortedElementId,
3 years ago
index: number
};
export default class SortedList<SortedElement extends SortedElementBase> {
3 years ago
protected elements: Map<SortedElementId, SortedElement>;
3 years ago
protected sorted: Array<SortedElement>;
3 years ago
protected getIndex: (element: SortedElement) => number;
3 years ago
protected onDelete: (element: SortedElement) => void;
protected onUpdate: (element: SortedElement) => void;
protected onSort: (element: SortedElement, idx: number) => void;
protected onElementCreate: (base: SortedElementBase, batch: boolean) => SortedElement;
protected updateElementWith = (callback: () => void) => callback();
protected updateListWith = (callback: (canUpdate: boolean | undefined) => void) => callback(true);
protected middleware = getMiddleware();
constructor(options: {
getIndex: SortedList<SortedElement>['getIndex'],
onDelete?: SortedList<SortedElement>['onDelete'],
onUpdate?: SortedList<SortedElement>['onUpdate'],
onSort?: SortedList<SortedElement>['onSort'],
onElementCreate: SortedList<SortedElement>['onElementCreate'],
updateElementWith?: SortedList<SortedElement>['updateElementWith'],
updateListWith?: SortedList<SortedElement>['updateListWith']
}) {
safeAssign(this, options);
this.elements = new Map();
this.sorted = [];
}
public clear() {
this.middleware.clean();
this.elements.clear();
this.sorted.length = 0;
}
protected _updateList() {
this.elements.forEach(element => {
this.update(element.id, true);
});
if(this.onSort) {
this.sorted.forEach((element, idx) => {
this.onSort(element, idx);
});
}
}
public updateList(callback: (updated: boolean) => void) {
const middleware = this.middleware.get();
this.updateListWith((canUpdate) => {
if(!middleware() || (canUpdate !== undefined && !canUpdate)) {
return callback(false);
}
this._updateList();
callback(true);
});
}
3 years ago
public has(id: SortedElementId) {
3 years ago
return this.elements.has(id);
}
3 years ago
public get(id: SortedElementId) {
3 years ago
return this.elements.get(id);
}
public getAll() {
return this.elements;
}
3 years ago
public add(id: SortedElementId, batch = false, updateElementWith?: SortedList<SortedElement>['updateElementWith'], updateBatch = batch) {
3 years ago
let element = this.get(id);
if(element) {
return element;
}
const base: SortedElementBase = {
id,
index: 0
};
element = this.onElementCreate(base, batch);
this.elements.set(id, element);
this.update(id, updateBatch, element, updateElementWith);
return element;
}
3 years ago
public delete(id: SortedElementId, noScheduler?: boolean) {
3 years ago
const element = this.elements.get(id);
if(!element) {
return false;
}
this.elements.delete(id);
const idx = this.sorted.indexOf(element);
if(idx !== -1) {
this.sorted.splice(idx, 1);
}
if(this.onDelete) {
if(noScheduler) {
this.onDelete(element);
} else {
const middleware = this.middleware.get();
this.updateElementWith(() => {
if(!middleware()) {
return;
}
this.onDelete(element);
});
}
}
return true;
}
3 years ago
public update(id: SortedElementId, batch = false, element = this.get(id), updateElementWith?: SortedList<SortedElement>['updateElementWith']) {
3 years ago
if(!element) {
return;
}
3 years ago
element.index = this.getIndex(element);
3 years ago
this.onUpdate && this.onUpdate(element);
const idx = insertInDescendSortedArray(this.sorted, element, 'index');
if(!batch && this.onSort) {
const middleware = this.middleware.get();
(updateElementWith || this.updateElementWith)(() => {
if(!middleware()) {
return;
}
// * в случае пересортировки этого же элемента во время ожидания вызовется вторая такая же. нужно соблюдать последовательность событий
this.onSort(element, idx);
/* if(this.get(id) === element) {
this.onSort(element, this.sorted.indexOf(element));
} */
});
}
}
}