Skip to content

Commit

Permalink
📝 add doc for 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Harvey Woo committed Dec 11, 2023
1 parent d9e6cb8 commit 35a0908
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ const resource = await pool.acquire(true);
// When the resource pool is empty, will wait for the resource to be released
```

```javascript
// Asynchronous acquire can be aborted by passing in an `AbortSignal`
const controller = new AbortController();
const resource = await pool.acquire(true, controller.signal);
controller.abort();
// Error will be thrown
resource.id === 0
```

### Release pool resources

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

```

### Iterable

Pool implements the `Iterable` interface, you can use `for...of` to iterate.
At the same time, `Pool` also implements the `AsyncIterable` interface, you can use `for await...of` to iterate asynchronously.

```javascript
// Iterate over the resources in the resource pool, and get the resources at the same time,
// When iterating synchronously, if there are no resources in the resource pool, the iteration will exit
for (const resource of pool) {
console.log(resource.id);
}

// When iterating asynchronously, if there are no resources in the resource pool, it will wait for the resources to be acquired
// So when iterating asynchronously, it will not exit the iteration, which is equivalent to an infinite loop
for await (const resource of pool) {
console.log(resource.id);
}
```

### PromiseLike

Pool implements the `PromiseLike` interface, you can use `await` to wait.

```javascript
// await pool means waiting for all resources to be released before returning
await pool;
```

### Resource limiter

```javascript
Expand Down
38 changes: 38 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ const resource = await pool.acquire(true);

```

```javascript
// 异步获取资源可以通过传入 `AbortSignal` 来中断
const controller = new AbortController();
const resource = await pool.acquire(true, controller.signal);
controller.abort();
// 会抛出错误
resource.id === 0
```


### 释放资源

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

```

### Iterable

Pool 实现了 `Iterable` 接口,可以使用 `for...of` 进行遍历。
同时 `Pool` 也实现了 `AsyncIterable` 接口,可以使用 `for await...of` 进行异步遍历。

```javascript
// 遍历资源池里边的资源,同时会获取资源,
// 同步遍历时,如果资源池中没有资源,会退出遍历
for (const resource of pool) {
console.log(resource.id);
}

// 异步遍历时,如果资源池中没有资源,会等待资源的获取
// 所以异步遍历时,不会退出遍历,相当于一个无限循环
for await (const resource of pool) {
console.log(resource.id);
}
```

### PromiseLike

Pool 实现了 `PromiseLike` 接口,可以使用 `await` 进行等待。

```javascript
// await pool 意味着等待所有资源都被释放,才会返回
await pool;
```

### pLimit / Pool.limit

是的,`cat5th/pool.js` 很贴心的提供了一个 `pLimit``Pool.limit` 的方法,它是`Pool.prototype.limit`的别名。
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cat5th/pool.js",
"version": "1.3.0",
"version": "1.4.1",
"description": "Represents a generic object pool for javascript",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 35a0908

Please sign in to comment.