race-events
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

race-events.js

npm size deps

Race event emissions from a set of emitters and events.

  • Supports Node's EventEmitter and browser's EventTarget
  • Supports Typescript

Installation

npm i race-events

API

raceEvents(...specs[Emitter, ResolveEventName[], RejectEventName[]?][]):
  Promise<{ target: Emitter, type: ResolveEventName, data: any[] }>

Usage

import r from "race-events";
import WebSocket from "ws";
 
type WSMessage = { target: WebSocket; type: "message"; data: [WebSocket.Data] };
type WSClose = { target: WebSocket; type: "close"; data: [number, string] };
type WSError = { target: WebSocket; type: "error"; data: [Error] };
 
const wss = new WebSocket.Server();
 
wss.on("connection", async function(ws) {
  try {
 
    // await messages, throw close and error events
    let a = await r<WSMessage>([ws, ["message"], ["close", "error"]]);
    console.log(a.type, a.target, a.data);
 
    // do not throw close and error events
    let b = await r<WSMessage | WSClose | WSError>([ws, ["message", "close", "error"]]);
 
  } catch (e) {
    console.error("threw", e);
  }
});

Related libraries

Package Sidebar

Install

npm i race-events

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

7.27 kB

Total Files

6

Last publish

Collaborators

  • rliang