Re-write net docs
This commit is contained in:
+30
-24
@@ -1,37 +1,43 @@
|
||||
# Pool
|
||||
|
||||
The Pool class manages a collection of relay connections, providing a centralized way to track and reuse connections across your application.
|
||||
The Pool class manages a collection of websocket connections to relay servers, providing connection pooling and lifecycle management.
|
||||
|
||||
## Overview
|
||||
## Classes
|
||||
|
||||
- Creates and caches connections
|
||||
- Ensures single connection per relay
|
||||
- Handles cleanup of unused connections
|
||||
- Provides connection lookup
|
||||
### Pool
|
||||
|
||||
## Usage
|
||||
A connection pool that creates and manages Socket instances for different relay URLs.
|
||||
|
||||
**Methods:**
|
||||
- `static get()` - Returns the singleton pool instance
|
||||
- `has(url)` - Checks if a socket exists for the given URL
|
||||
- `get(url)` - Gets or creates a socket for the given URL
|
||||
- `subscribe(callback)` - Subscribes to new socket creation events
|
||||
- `remove(url)` - Removes and cleans up a socket
|
||||
- `clear()` - Removes all sockets from the pool
|
||||
|
||||
## Functions
|
||||
|
||||
### makeSocket(url, policies)
|
||||
|
||||
Creates a new Socket instance with the given URL and applies default policies.
|
||||
|
||||
## Example
|
||||
|
||||
```typescript
|
||||
import {Pool} from '@welshman/net'
|
||||
import {Pool} from "@welshman/net"
|
||||
|
||||
// Create pool
|
||||
const pool = new Pool()
|
||||
// Get the singleton - Pool can also be instantiated directly if you want multiple pools
|
||||
const pool = Pool.get()
|
||||
|
||||
// Get or create connection
|
||||
const connection = pool.get("wss://relay.example.com")
|
||||
// Get a socket for a relay
|
||||
const socket = pool.get("wss://relay.example.com")
|
||||
|
||||
// Check if relay is in pool
|
||||
if (pool.has("wss://relay.example.com")) {
|
||||
// Use existing connection
|
||||
}
|
||||
// Subscribe to new socket creation
|
||||
const unsubscribe = pool.subscribe((socket) => {
|
||||
console.log("New socket created:", socket.url)
|
||||
})
|
||||
|
||||
// Remove connection
|
||||
// Clean up
|
||||
pool.remove("wss://relay.example.com")
|
||||
|
||||
// Clear all connections
|
||||
pool.clear()
|
||||
```
|
||||
|
||||
|
||||
The Pool is typically used internally by the router and executor, but can be used directly for custom connection management.
|
||||
It ensures efficient connection reuse across your application.
|
||||
|
||||
Reference in New Issue
Block a user