Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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',
});

```