Skip to content

racccoooon/simple-eventhub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simple-eventhub

A simple type based eventhub for js projects.

installation

npm install raccoon-eventhub

usage

First you create your event classes. These should model the event and its data. For example:

export class LoginEvent {
  constructor(username) {
    this.username = username;
  }
}

export class LogoutEvent {}

Then you import the eventhub. This is as simple as:

import eventhub from "raccoon-eventhub";

This example uses the default export from "raccoon-eventhub" which is a shared instance of the Eventhub. It is also possible to instantiate your new local eventhub instances:

import { EventHub } from "raccoon-eventhub";

let localEventhub = new EventHub();

To subscribe to events simply provide the type of the event and a function with one argument (the event) or fewer. The function is the subscriber callback and will be executed when an event of the specified type was fired. Note that anonymous functions cannot be unsubscribed. Like so:

function onLogin(evt){...}

eventhub.subscribe(LoginEvent, onLoginEvent);

To unsubscribe simple call the unsubscribe method on the eventhub with the event type and subscriber function used in the subscribe method above:

eventhub.unsubscribe(LoginEvent, onLoginEvent);

Finally to fire an event and notify all subscribers of that event type on that eventhub simply call the notify function on the eventhub instance. Like so:

eventhub.notify(new LoginEvent("Darkarotte"));

license

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published