@soundtype/eventemitter

A typesafe event emitter for TypeScript

Usage no npm install needed!

<script type="module">
  import soundtypeEventemitter from 'https://cdn.skypack.dev/@soundtype/eventemitter';
</script>

README

@soundtype/eventemitter

A typesafe event emitter for TypeScript

Usage

import { EventEmitter } from '@soundtype/eventemitter';

const ee = new EventEmitter<{ someString: string; someNumber: number }>();

ee.emit('someString', 'string'); // autocompletes `someString`, and ensures the value is a string
ee.emit('someNumber', 42); // autocompletes `someNumber`, and ensures the value is a number

ee.emit('problem', 'will fail'); // <- this will fail in compile time (no such event name 'problem')
ee.emit('someString', {}); // <- this will fail in compile time (expected string)
ee.emit('someNumber', {}); // <- this will fail in compile time (expected string)

(async () => {
  const someString = await ee.waitFor('someString'); // you can get one with a Promise API. Types are inferred based on the event name

  ee.once('someString', value => {
    // you can run things only once, without a Promise API
    // types are inferred correctly based on the event name
  });

  const unsubscribe = ee.addListener('someString', value => {
    // you can add listeners for events
    // types are inferred correctly based on the event name
  });

  unsubscribe(); // you can easily unsubscribe by using the return value
})();

Local Development

This project was bootstrapped with TSDX.

Below is a list of commands you will probably find useful.

npm start or yarn start

Runs the project in development/watch mode. Your project will be rebuilt upon changes. TSDX has a special logger for you convenience. Error messages are pretty printed and formatted for compatibility VS Code's Problems tab.

Your library will be rebuilt if you make edits.

npm run build or yarn build

Bundles the package to the dist folder. The package is optimized and bundled with Rollup into multiple formats (CommonJS, UMD, and ES Module).

npm test or yarn test

Runs the test watcher (Jest) in an interactive mode. By default, runs tests related to files changed since the last commit.