-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbrowser-manager.js
More file actions
56 lines (52 loc) · 1.1 KB
/
browser-manager.js
File metadata and controls
56 lines (52 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const puppeteer = require('puppeteer');
const browserPromise = puppeteer.launch({
args: [
'--no-sandbox',
'--no-zygote',
'--disable-setuid-sandbox'
// '--disable-dev-shm-usage',
],
// defaultViewport: chromium.defaultViewport,
// executablePath: await chromium.executablePath,
headless: true,
});
const _makePromise = () => {
let accept, reject;
const p = new Promise((a, r) => {
accept = a;
reject = r;
});
p.accept = accept;
p.reject = reject;
return p;
};
class TicketManager {
constructor(tickets) {
this.tickets = tickets;
this.queue = [];
}
async lock() {
if (this.tickets > 0) {
this.tickets--;
} else {
const p = _makePromise();
this.queue.push(p.accept);
await p;
await this.lock();
}
}
unlock() {
this.tickets++;
if (this.queue.length > 0) {
this.queue.shift()();
}
}
}
module.exports = {
async getBrowser() {
return await browserPromise;
},
makeTicketManager(tickets) {
return new TicketManager(tickets);
},
};