Skip to content

Commit adaddcd

Browse files
committed
Add pool documentation
1 parent 9123cde commit adaddcd

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,32 @@ An incognito page will not share cookies/cache with other browser pages.
102102

103103
### .pool(options)
104104

105-
Tha main **browserless** constructor expose a singleton browser. This is enough for most scenarios, but in case you need you can intialize a **pool of instances**.
105+
**browserless** uses internally a singletion browser instance.
106+
107+
You can use `.pool` constructor for creating a **pool of instances**:
106108

107109
```js
108110
const createBrowserless = require('browserless')
109-
const browserless = createBrowserless.pool()
111+
const browserlessPool = createBrowserless.pool()
112+
```
113+
114+
After that, the API is the same. The acquire/release is done automagically.
115+
116+
You can interact with a **browserless** instance directly as well:
117+
118+
```js
119+
const createBrowserless = require('browserless')
120+
const browserlessPool = createBrowserless.pool()
121+
122+
// get a browserless instance from the pool
123+
browserlessPool(async browserless => {
124+
// get a page from the browser instance
125+
const page = await browserless.page()
126+
await browserless.goto(page, { url: url.toString() })
127+
const html = await page.content()
128+
console.log(html)
129+
process.exit()
130+
})
110131
```
111132

112133
#### options

examples/pool.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
const createBrowserless = require('..')
4+
const browserlessPool = createBrowserless.pool()
5+
6+
const url = new URL(process.argv[2])
7+
;(async () => {
8+
// get a browserless instance from the pool
9+
browserlessPool(async browserless => {
10+
// get a page from the browser instance
11+
const page = await browserless.page()
12+
await browserless.goto(page, { url: url.toString() })
13+
const html = await page.content()
14+
console.log(html)
15+
process.exit()
16+
})
17+
})()

src/pool/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ module.exports = ({ opts, ...launchOpts } = {}) => {
1111
const poolOpts = { ...POOL_OPTS, ...opts }
1212
const pool = createPool(poolOpts, launchOpts)
1313
;['html', 'text', 'pdf', 'screenshot'].forEach(key => {
14-
pool[key] = (url, opts) =>
15-
pool.use(browserless => browserless[key](url, opts))
14+
pool[key] = (url, opts) => pool(browserless => browserless[key](url, opts))
1615
})
1716
return pool
1817
}

0 commit comments

Comments
 (0)