@hazae41/piscine v1.2.12
Create async pools with automatic retry
npm i @hazae41/piscine
Features
Current features
- 100% TypeScript and ESM
- No external dependency
- Simple API
- Automatic retry
- Get a fast random value using Math's PRNG
- Get a secure random value using WebCrypto's CSPRNG
Usage
WebSockets
Create a pool of 5 WebSockets
import { Pool } from "@hazae41/piscine"
const pool = new Pool<Disposer<WebSocket>>(async ({ pool, index, signal }) => {
const raw = new WebSocket(`/api`)
/**
* Define the socket
*/
using presocket = new Box(new Disposer(raw, () => raw.close()))
/**
* Prepare the entry
*/
const onCloseOrError = () => {
pool.restart(index)
}
raw.addEventListener("error", onCloseOrError)
raw.addEventListener("close", onCloseOrError)
/**
* Move socket in the entry
*/
const socket = presocket.moveOrThrow()
const onEntryClean = () => {
/**
* Dispose the socket if the entry still owns it
*/
using postsocket = socket
/**
* Clean the entry
*/
raw.removeEventListener("error", onCloseOrError)
raw.removeEventListener("close", onCloseOrError)
}
/**
* Define the entry
*/
using preentry = new Box(new Disposer(socket, onEntryClean))
/**
* Move the entry in the pool
*/
return new Ok(preentry.unwrapOrThrow())
}, { capacity: 5 })
Random
Get a random open socket using Math's PRNG
const socket = await pool.random()
socket.send("Hello world")
Get a random open socket using WebCrypto's CSPRNG
const socket = await pool.cryptoRandom()
socket.send("Hello world")
Iteration
Pools are iterator, so you can loop through open sockets or create an array
for (const socket of pool)
socket.send("Hello world")
const sockets = [...pool]
2 months ago
2 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
4 months ago
4 months ago
5 months ago
5 months ago
5 months ago
5 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
9 months ago
9 months ago
10 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
10 months ago
7 months ago
8 months ago
8 months ago
9 months ago
9 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
12 months ago
12 months ago
12 months ago
11 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
11 months ago
12 months ago
12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
11 months ago
11 months ago
11 months ago
12 months ago
11 months ago
12 months ago
12 months ago
11 months ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago