1.1.0 • Published 10 months ago

@kiruse/typed-events v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

@kiruse/typed-events

Typed Events are an alternative event system designed for TypeScript. While there's obviously nothing functionally wrong with the standard EventEmitter system, it was built for JavaScript before TypeScript was even conceived. The objective of this library is simply to offer an event system which feels more natural to TypeScript.

Installation

Install via npm:

$ npm install @kiruse/typed-events

Usage

import { Event } from '@kiruse/typed-events';

// First generic parameter is the `event.args` field, second is the optional `event.result` field.
const event = Event<{ foo: string }, number>();

const unsub = event((e, args) => {
  console.log(args);
  e.result ??= 42;
  e.result++;
});
event.once((e) => {
  e.result ??= 42;
  e.result /= 2;
});

const e = await event.emit({ foo: 'bar' });
console.log(e.result); // 21.5
unsub();

Note: Both AsyncEvent and SyncEvent exist, with AsyncEvent being an alias of Event. The only difference between the two is that SyncEvent prohibits the use of asynchronous handlers, emit does not return a promise & returns the event directly, and has corresponding typing.

1.1.0

10 months ago

1.0.3

1 year ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago