ts-pubsy
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

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

Package Sidebar

Install

npm i ts-pubsy

Weekly Downloads

118

Version

0.3.0

License

MIT

Unpacked Size

8.93 kB

Total Files

11

Last publish

Collaborators

  • gabrielctroia