fix(forefront): get random one in pool

このコミットが含まれているのは:
xiang 2023-06-08 18:50:10 +08:00
コミット 773f5c8393
2個のファイルの変更11行の追加2行の削除

ファイルの表示

@ -2,7 +2,7 @@ import puppeteer, {Browser, Page, PuppeteerLaunchOptions} from "puppeteer";
import path from "path";
import run from "node:test";
import * as fs from "fs";
import {sleep} from "../utils";
import {shuffleArray, sleep} from "../utils";
const runPath = path.join(__dirname, 'run');
@ -70,7 +70,7 @@ export class BrowserPool<T> {
//@ts-ignore
get(): [page: Page | undefined, data: T | undefined, done: (data: T) => void, destroy: (newID: string) => void] {
for (const item of this.pool) {
for (const item of shuffleArray(this.pool)) {
if (item.ready) {
item.ready = false;
return [

ファイルの表示

@ -57,3 +57,12 @@ export async function sleep(duration: number): Promise<void> {
setTimeout(() => resolve(), duration);
})
}
export function shuffleArray<T>(array: T[]): T[] {
const shuffledArray = [...array];
for (let i = shuffledArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
}
return shuffledArray;
}