tweb/src/components/lazyLoadQueue.ts

335 lines
8.2 KiB
TypeScript
Raw Normal View History

2021-04-08 15:52:31 +02:00
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
import { debounce } from "../helpers/schedulers";
2020-06-21 14:25:17 +02:00
import { logger, LogLevels } from "../lib/logger";
2020-09-20 00:38:00 +02:00
import VisibilityIntersector, { OnVisibilityChange } from "./visibilityIntersector";
import { findAndSpliceAll } from "../helpers/array";
2020-09-20 00:38:00 +02:00
type LazyLoadElementBase = {
load: () => Promise<any>
2020-04-14 17:46:31 +02:00
};
type LazyLoadElement = Omit<LazyLoadElementBase, 'load'> & {
load: (target?: HTMLElement) => Promise<any>,
div: HTMLElement
wasSeen?: boolean,
2020-09-20 00:38:00 +02:00
};
2021-03-04 22:56:04 +01:00
const PARALLEL_LIMIT = 8;
2020-09-20 00:38:00 +02:00
export class LazyLoadQueueBase {
public queueId = 0;
protected queue: Array<LazyLoadElementBase> = [];
2020-09-20 00:38:00 +02:00
protected inProcess: Set<LazyLoadElementBase> = new Set();
2020-09-20 00:38:00 +02:00
protected lockPromise: Promise<void> = null;
protected unlockResolve: () => void = null;
2020-09-20 00:38:00 +02:00
protected log = logger('LL', LogLevels.error);
protected processQueue: () => void;
constructor(protected parallelLimit = PARALLEL_LIMIT) {
this.processQueue = debounce(() => this._processQueue(), 20, false, true);
}
public clear() {
2020-09-20 00:38:00 +02:00
this.inProcess.clear(); // ацтеки забьются, будет плохо
2020-06-21 14:25:17 +02:00
this.queue.length = 0;
2020-09-20 00:38:00 +02:00
// unreachable code
/* for(let item of this.inProcess) {
2020-06-16 22:48:08 +02:00
this.lazyLoadMedia.push(item);
2020-09-20 00:38:00 +02:00
} */
2020-04-14 17:46:31 +02:00
}
public lock() {
if(this.lockPromise) return;
//const perf = performance.now();
2020-04-14 17:46:31 +02:00
this.lockPromise = new Promise((resolve, reject) => {
this.unlockResolve = resolve;
});
/* if(DEBUG) {
this.lockPromise.then(() => {
this.log('was locked for:', performance.now() - perf);
});
} */
2020-04-14 17:46:31 +02:00
}
public unlock() {
if(!this.unlockResolve) return;
2020-04-14 17:46:31 +02:00
this.unlockResolve();
this.unlockResolve = this.lockPromise = null;
this.processQueue();
2020-04-14 17:46:31 +02:00
}
protected async processItem(item: LazyLoadElementBase) {
if(this.lockPromise) {
return;
}
2020-09-20 00:38:00 +02:00
this.inProcess.add(item);
/* if(DEBUG) {
this.log('will load media', this.lockPromise, item);
} */
2020-09-20 00:38:00 +02:00
try {
//await new Promise((resolve) => setTimeout(resolve, 2e3));
//await new Promise((resolve, reject) => window.requestAnimationFrame(() => window.requestAnimationFrame(resolve)));
//await item.load(item.div);
await this.loadItem(item);
2020-09-20 00:38:00 +02:00
} catch(err) {
2021-03-13 11:12:24 +01:00
if(err !== 'NO_ENTRY_FOUND') {
this.log.error('loadMediaQueue error:', err/* , item */);
}
}
2020-09-20 00:38:00 +02:00
this.inProcess.delete(item);
/* if(DEBUG) {
this.log('loaded media', item);
} */
2020-04-14 17:46:31 +02:00
this.processQueue();
}
protected loadItem(item: LazyLoadElementBase) {
return item.load();
2020-09-20 00:38:00 +02:00
}
2020-09-20 00:38:00 +02:00
protected getItem() {
return this.queue.shift();
2020-09-20 00:38:00 +02:00
}
protected addElement(method: 'push' | 'unshift', el: LazyLoadElementBase) {
this.queue[method](el);
this.processQueue();
2020-09-20 00:38:00 +02:00
}
protected _processQueue(item?: LazyLoadElementBase) {
if(!this.queue.length || this.lockPromise || (this.parallelLimit > 0 && this.inProcess.size >= this.parallelLimit)) return;
2020-06-21 14:25:17 +02:00
2020-09-20 00:38:00 +02:00
do {
if(item) {
this.queue.findAndSplice(i => i === item);
2020-09-20 00:38:00 +02:00
} else {
item = this.getItem();
}
2020-09-20 00:38:00 +02:00
if(item) {
this.processItem(item);
} else {
break;
}
item = null;
} while(this.inProcess.size < this.parallelLimit && this.queue.length);
2020-09-20 00:38:00 +02:00
}
public push(el: LazyLoadElementBase) {
this.addElement('push', el);
2020-09-20 00:38:00 +02:00
}
2020-09-20 00:38:00 +02:00
public unshift(el: LazyLoadElementBase) {
this.addElement('unshift', el);
2020-09-20 00:38:00 +02:00
}
}
2020-09-20 00:38:00 +02:00
export class LazyLoadQueueIntersector extends LazyLoadQueueBase {
protected queue: Array<LazyLoadElement> = [];
protected inProcess: Set<LazyLoadElement> = new Set();
2020-09-20 00:38:00 +02:00
public intersector: VisibilityIntersector;
protected intersectorTimeout: number;
constructor(protected parallelLimit = PARALLEL_LIMIT) {
2020-09-20 00:38:00 +02:00
super(parallelLimit);
}
public lock() {
super.lock();
this.intersector.lock();
}
2020-04-14 17:46:31 +02:00
2020-09-20 00:38:00 +02:00
public unlock() {
super.unlock();
this.intersector.unlock();
}
public unlockAndRefresh() {
super.unlock();
this.intersector.unlockAndRefresh();
}
public clear() {
super.clear();
this.intersector.disconnect();
}
public refresh() {
this.intersector.refresh();
}
protected loadItem(item: LazyLoadElement) {
return item.load(item.div);
}
protected addElement(method: 'push' | 'unshift', el: LazyLoadElement) {
const item = this.queue.find(i => i.div === el.div && i.load === el.load);
if(item) {
return false;
} else {
for(const item of this.inProcess) {
if(item.div === el.div && item.load === el.load) {
return false;
}
}
}
this.queue[method](el);
return true;
}
2020-09-20 00:38:00 +02:00
protected setProcessQueueTimeout() {
if(!this.intersectorTimeout) {
this.intersectorTimeout = window.setTimeout(() => {
this.intersectorTimeout = 0;
this.processQueue();
2020-09-20 00:38:00 +02:00
}, 0);
}
}
public push(el: LazyLoadElement) {
super.push(el);
}
public unshift(el: LazyLoadElement) {
super.unshift(el);
}
public unobserve(el: HTMLElement) {
findAndSpliceAll(this.queue, (i) => i.div === el);
this.intersector.unobserve(el);
}
2020-09-20 00:38:00 +02:00
}
export default class LazyLoadQueue extends LazyLoadQueueIntersector {
constructor(protected parallelLimit = PARALLEL_LIMIT) {
2020-09-20 00:38:00 +02:00
super(parallelLimit);
this.intersector = new VisibilityIntersector(this.onVisibilityChange);
}
private onVisibilityChange = (target: HTMLElement, visible: boolean) => {
if(visible) {
/* if(DEBUG) {
this.log('isIntersecting', target);
} */
2020-09-20 00:38:00 +02:00
// need for set element first if scrolled
findAndSpliceAll(this.queue, (i) => i.div === target).forEach(item => {
2020-09-20 00:38:00 +02:00
item.wasSeen = true;
this.queue.unshift(item);
2020-09-20 00:38:00 +02:00
//this.processQueue(item);
});
2020-09-20 00:38:00 +02:00
this.setProcessQueueTimeout();
}
2020-09-20 00:38:00 +02:00
};
protected getItem() {
return this.queue.findAndSplice(item => item.wasSeen);
}
2020-04-14 17:46:31 +02:00
2020-09-20 00:38:00 +02:00
public async processItem(item: LazyLoadElement) {
await super.processItem(item);
this.intersector.unobserve(item.div);
}
protected addElement(method: 'push' | 'unshift', el: LazyLoadElement) {
const inserted = super.addElement(method, el);
if(!inserted) return false;
this.intersector.observe(el.div);
/* if(el.wasSeen) {
this.processQueue(el);
} else */if(!el.hasOwnProperty('wasSeen')) {
2020-04-14 17:46:31 +02:00
el.wasSeen = false;
}
return true;
}
2020-09-20 00:38:00 +02:00
}
2020-06-21 14:25:17 +02:00
2020-09-20 00:38:00 +02:00
export class LazyLoadQueueRepeat extends LazyLoadQueueIntersector {
private _queue: Map<HTMLElement, LazyLoadElement> = new Map();
2020-06-21 14:25:17 +02:00
constructor(protected parallelLimit = PARALLEL_LIMIT, protected onVisibilityChange?: OnVisibilityChange) {
2020-09-20 00:38:00 +02:00
super(parallelLimit);
this.intersector = new VisibilityIntersector((target, visible) => {
const spliced = findAndSpliceAll(this.queue, (i) => i.div === target);
2020-09-20 00:38:00 +02:00
if(visible) {
const items = spliced.length ? spliced : [this._queue.get(target)];
items.forEach(item => {
this.queue.unshift(item || this._queue.get(target));
});
2020-09-20 00:38:00 +02:00
}
this.onVisibilityChange && this.onVisibilityChange(target, visible);
this.setProcessQueueTimeout();
});
2020-06-21 14:25:17 +02:00
}
public clear() {
super.clear();
this._queue.clear();
}
2020-09-20 00:38:00 +02:00
/* public async processItem(item: LazyLoadElement) {
//await super.processItem(item);
await LazyLoadQueueBase.prototype.processItem.call(this, item);
if(this.lazyLoadMedia.length) {
this.processQueue();
}
} */
public observe(el: LazyLoadElement) {
this._queue.set(el.div, el);
2020-09-20 00:38:00 +02:00
this.intersector.observe(el.div);
2020-06-21 14:25:17 +02:00
}
}
export class LazyLoadQueueRepeat2 extends LazyLoadQueueIntersector {
constructor(protected parallelLimit = PARALLEL_LIMIT, protected onVisibilityChange?: OnVisibilityChange) {
super(parallelLimit);
this.intersector = new VisibilityIntersector((target, visible) => {
const spliced = findAndSpliceAll(this.queue, (i) => i.div === target);
if(visible && spliced.length) {
spliced.forEach(item => {
this.queue.unshift(item);
});
}
this.onVisibilityChange && this.onVisibilityChange(target, visible);
this.setProcessQueueTimeout();
});
}
public observe(el: HTMLElement) {
this.intersector.observe(el);
}
}