Skip to content

Commit 35a0908

Browse files
author
Harvey Woo
committed
📝 add doc for 1.4
1 parent d9e6cb8 commit 35a0908

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ const resource = await pool.acquire(true);
143143
// When the resource pool is empty, will wait for the resource to be released
144144
```
145145

146+
```javascript
147+
// Asynchronous acquire can be aborted by passing in an `AbortSignal`
148+
const controller = new AbortController();
149+
const resource = await pool.acquire(true, controller.signal);
150+
controller.abort();
151+
// Error will be thrown
152+
resource.id === 0
153+
```
154+
146155
### Release pool resources
147156

148157
```javascript
@@ -165,6 +174,34 @@ pool.on('release', (resource) => {
165174

166175
```
167176

177+
### Iterable
178+
179+
Pool implements the `Iterable` interface, you can use `for...of` to iterate.
180+
At the same time, `Pool` also implements the `AsyncIterable` interface, you can use `for await...of` to iterate asynchronously.
181+
182+
```javascript
183+
// Iterate over the resources in the resource pool, and get the resources at the same time,
184+
// When iterating synchronously, if there are no resources in the resource pool, the iteration will exit
185+
for (const resource of pool) {
186+
console.log(resource.id);
187+
}
188+
189+
// When iterating asynchronously, if there are no resources in the resource pool, it will wait for the resources to be acquired
190+
// So when iterating asynchronously, it will not exit the iteration, which is equivalent to an infinite loop
191+
for await (const resource of pool) {
192+
console.log(resource.id);
193+
}
194+
```
195+
196+
### PromiseLike
197+
198+
Pool implements the `PromiseLike` interface, you can use `await` to wait.
199+
200+
```javascript
201+
// await pool means waiting for all resources to be released before returning
202+
await pool;
203+
```
204+
168205
### Resource limiter
169206

170207
```javascript

README_CN.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ const resource = await pool.acquire(true);
146146

147147
```
148148

149+
```javascript
150+
// 异步获取资源可以通过传入 `AbortSignal` 来中断
151+
const controller = new AbortController();
152+
const resource = await pool.acquire(true, controller.signal);
153+
controller.abort();
154+
// 会抛出错误
155+
resource.id === 0
156+
```
157+
158+
149159
### 释放资源
150160

151161
```javascript
@@ -195,6 +205,34 @@ limiter.abort(new DOMException('AbortError'));
195205

196206
```
197207

208+
### Iterable
209+
210+
Pool 实现了 `Iterable` 接口,可以使用 `for...of` 进行遍历。
211+
同时 `Pool` 也实现了 `AsyncIterable` 接口,可以使用 `for await...of` 进行异步遍历。
212+
213+
```javascript
214+
// 遍历资源池里边的资源,同时会获取资源,
215+
// 同步遍历时,如果资源池中没有资源,会退出遍历
216+
for (const resource of pool) {
217+
console.log(resource.id);
218+
}
219+
220+
// 异步遍历时,如果资源池中没有资源,会等待资源的获取
221+
// 所以异步遍历时,不会退出遍历,相当于一个无限循环
222+
for await (const resource of pool) {
223+
console.log(resource.id);
224+
}
225+
```
226+
227+
### PromiseLike
228+
229+
Pool 实现了 `PromiseLike` 接口,可以使用 `await` 进行等待。
230+
231+
```javascript
232+
// await pool 意味着等待所有资源都被释放,才会返回
233+
await pool;
234+
```
235+
198236
### pLimit / Pool.limit
199237

200238
是的,`cat5th/pool.js` 很贴心的提供了一个 `pLimit``Pool.limit` 的方法,它是`Pool.prototype.limit`的别名。

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cat5th/pool.js",
3-
"version": "1.3.0",
3+
"version": "1.4.1",
44
"description": "Represents a generic object pool for javascript",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

0 commit comments

Comments
 (0)