evented

Fire and listen to events in JavaScript

Install

npm i evented-ts
Or
yarn add evented-ts

Use

import create from "evented-ts";


const evented = create();

evented.on("hello", (e) => console.log("hello", e.args));
            
evented.fire("hello", "world");

API

evented.fire(eventName: string, eventArgs?: any): void;

Fires an event with optional arguments.


evented.on(eventName: string, listener: (e: Event) => void): () => void;

Listens to an event. Returns a function that removes the listener when invoked.


evented.off(eventName: string, listener?: (e: Event) => void): void;

Removes the given listener for the event name. Otherwise removes all listeners for the event name.


evented.listensTo(eventName: string): boolean;

Returns whether listeners exist for a given event name.


evented.listeners(eventName: string): Array<(event: Event) => void>;

Returns listeners for a given event name.


Demo

Listening to the serve and return events 👂