oj-pool
v6.0.8
Published
Generic pool, Promise based
Downloads
5
Readme
promise based queue
uses oj-eventaggregator and oj-queue
Usage
import { Pool } from "oj-pool"
const pool = new Pool({
create: async () => {
const browser = await chromium.launch()
return browser
},
destroy: async resource => {
const browser = resource.get()
await browser.close()
},
validate: async resource => {
return resource.uses() < 5
}
}, {
min: 1,
max: 4,
})
pool.on("*", console.log)
export const launch = async <T>(cb: (page: Page) => Promise<T>) =>
pool.use(async resource => {
const page = await resource.get().newPage()
let result: T
try {
result = await cb(page)
}
catch (err) {
console.error(err)
}
await page.close()
return result
})