2.1.0 • Published 11 months ago
publer v2.1.0
Publer
publer
is a tiny, type-safe pubsub library written in typescript
.
Installation
npm install publer
Usage
import { publer } from "publer";
type Peer = {
id: number;
username: string
}
interface Events {
login: [email: string; password: string];
logout: []; // no arguments
peerConnected: [peer: Peer]
peerDisconnected: [peer: Peer]
}
const [pub, sub] = publer<Events>();
// returns a cleanup function to remove listener
const unsub = sub("login", (email, password) => {
// payload is inferred
});
// fails without arguments, must be provided
pub("login", 'john@doe.com', 'johndoe123');
// fails with arguments
pub("logout");