Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/movesthatmatter/ts-pubsy
A Super Simple Typed Pub Sub Mechanism.
https://github.com/movesthatmatter/ts-pubsy
Last synced: about 2 months ago
JSON representation
A Super Simple Typed Pub Sub Mechanism.
- Host: GitHub
- URL: https://github.com/movesthatmatter/ts-pubsy
- Owner: movesthatmatter
- License: mit
- Created: 2022-12-29T15:56:36.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2022-12-29T20:53:31.000Z (almost 2 years ago)
- Last Synced: 2024-10-13T11:45:37.883Z (2 months ago)
- Language: TypeScript
- Size: 46.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# ts-pubsy
Fully Typed PubSub Mechanism with Channel and Payload autocompletion.
# Usage
```
import { Pubsy } from 'ts-pubsy';type Game = {
id: string;
players: [User[id], User[id]];
winner?: User[id];
}type GameEvents = {
onNewGame: Game;
onGameFinish: { id: string, winner: User[id] };
}const gamePubsy = new Pubsy();
// Subscriptions
gamePubsy.subscribe('onNewGame', (game) => {
// do stuff with the Game object
});gamePubsy.subscribe('onGameFinished', (game) => {
console.log(game.winner);
});// Publish (later on)
gamePubsy.publish('onNewGame', {
id: '1',
players: ['player-1', 'player-2'],
winner: undefined,
});gamePubsy.publish('onGameFinish', {
id: '1',
winner: 'player-2',
});```