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.
72 lines
1.9 KiB
72 lines
1.9 KiB
![]()
4 years ago
|
import { CancellablePromise, deferredPromise } from "./cancellablePromise";
|
||
|
import { getHeavyAnimationPromise } from "../hooks/useHeavyAnimationCheck";
|
||
|
import { fastRaf } from "./schedulers";
|
||
|
|
||
|
type HeavyQueue<T> = {
|
||
|
items: any[],
|
||
|
process: (...args: any[]) => T,
|
||
|
context: any,
|
||
|
promise?: CancellablePromise<ReturnType<HeavyQueue<T>['process']>[]>
|
||
|
};
|
||
|
const heavyQueue: HeavyQueue<any>[] = [];
|
||
|
let processingQueue = false;
|
||
|
|
||
|
export default function pushHeavyTask<T>(queue: HeavyQueue<T>) {
|
||
|
queue.promise = deferredPromise<T[]>();
|
||
|
heavyQueue.push(queue);
|
||
|
processHeavyQueue();
|
||
|
|
||
|
return queue.promise;
|
||
|
}
|
||
|
|
||
|
function processHeavyQueue() {
|
||
|
if(!processingQueue) {
|
||
|
const queue = heavyQueue.shift();
|
||
|
timedChunk(queue).finally(() => {
|
||
|
processingQueue = false;
|
||
|
if(heavyQueue.length) {
|
||
|
processHeavyQueue();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function timedChunk<T>(queue: HeavyQueue<T>) {
|
||
|
if(!queue.items.length) return Promise.resolve([]);
|
||
|
const todo = queue.items.slice();
|
||
|
const results: T[] = [];
|
||
|
|
||
|
return new Promise<T[]>((resolve, reject) => {
|
||
|
const f = async() => {
|
||
|
const start = performance.now();
|
||
|
|
||
|
do {
|
||
|
await getHeavyAnimationPromise();
|
||
|
const possiblePromise = queue.process.call(queue.context, todo.shift());
|
||
|
let realResult: T;
|
||
|
if(possiblePromise instanceof Promise) {
|
||
|
try {
|
||
|
realResult = await possiblePromise;
|
||
|
} catch(err) {
|
||
|
reject(err);
|
||
|
return;
|
||
|
}
|
||
|
} else {
|
||
|
realResult = possiblePromise;
|
||
|
}
|
||
|
|
||
|
results.push(realResult);
|
||
|
} while(todo.length > 0 && (performance.now() - start) < 6);
|
||
|
|
||
|
if(todo.length > 0) {
|
||
|
fastRaf(f);
|
||
|
//setTimeout(f, 25);
|
||
|
} else {
|
||
|
resolve(results);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
fastRaf(f);
|
||
|
//setTimeout(f, 25);
|
||
|
}).then(queue.promise.resolve, queue.promise.reject);
|
||
|
}
|