npm.io
4.0.0 • Published 2 weeks ago

ste-simple-events

Licence
MIT
Version
4.0.0
Deps
1
Size
13 kB
Vulns
0
Weekly
0
Stars
139

Strongly Typed Events - Simple events

Simple events are a type of event that will provide an argument with data when fired. If you use typescript, you can leverage the support for generics and get strongly typed code.

Version 4.0.0 requires Node.js 22.22.2 or newer.

Need a different type of event? Check out the others.

npm version License: MIT

Subscription made easy

An example says more than a 1000 words. Imagine if you have events like this on your class:

let clock = new Clock("Smu", 1000);

clock.onSequenceTick.subscribe(n =>
  console.log(`Sequence: ${n}`)
);

Events made easy

So let's look at the implementation from a TypeScript perspective. (Do you program NodeJs without typescript? Check this)

import { SimpleEventDispatcher } from "ste-simple-events";

class Clock {
  private _onSequenceTick = new SimpleEventDispatcher<number>();
  private _ticks: number = 0;

  constructor(public name: string, timeout: number) {
    setInterval(() => {
      this._ticks += 1;
      this._onSequenceTick.dispatch(this._ticks);
    }, timeout);
  }

  public get onSequenceTick() {
    return this._onSequenceTick.asEvent();
  }
}

Check the documentation or the examples for more information.

Need more info? Check the repo.

Maintenance

This project is maintained by Kees C. Bakker.

Keywords