Skip to content

Latest commit

 

History

History
47 lines (33 loc) · 746 Bytes

Readme.md

File metadata and controls

47 lines (33 loc) · 746 Bytes

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<GameEvents>();

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