Skip to content

axtk/event-patterns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

event-patterns

Lightweight zero-dependency event emitter with flexible event type matching

Usage

Initialization:

import { EventEmitter } from "event-patterns";

let eventEmitter = new EventEmitter();

Adding a handler of a specific event type:

eventEmitter.on("task started", (event) => {
  console.log(event);
});

Of all events matching the pattern:

eventEmitter.on(/^task\s/, (event) => {
  console.log(event);
});

With captured parameters:

eventEmitter.on(/^(\S+)\s(?<status>.*)$/, (event) => {
  console.log(event.params[0], event.params.status);
});

Adding a handler of all events dispatched to the eventEmitter instance:

let listener = eventEmitter.on("*", (event) => {
  console.log(event);
});

Dispatching an event of a specific type and properties:

eventEmitter.emit("task started", { x: 42 });

Removing a previously declared listener:

listener.remove();

About

Event manager with flexible event type matching

Topics

Resources

Stars

Watchers

Forks

Contributors