This tiny package implements the observer
pattern.
Run:
npm i @stein197/observer
import Event from "@stein197/observer/Event";
class JoinEvent extends Event {}
class LeaveEvent extends Event {}
const dispatcher = new EventDispatcher<[JoinEvent, LeaveEvent]>();
dispatcher.addEventListener(JoinEvent, event => {}); // Adding listener on JoinEvent event
dispatcher.addEventListener(LeaveEvent, event => {}); // Adding listener on LeaveEvent event
dispatcher.dispatch(JoinEvent, new JoinEvent()); // Firing all listeners subscribed on "AfterJoin" event
dispatcher.dispatch(LeaveEvent, new LeaveEvent()); // Firing all listeners subscribed on "AfterUnjoin" event
Every class that emits event must implement EventEmitter
interface:
import type {EventEmitter} from "@stein197/observer/EventEmitter";
class User implements EventEmitter<[]> {
public addEventListener(/* ... */) {/* ... */}
public removeEventListener(/* ... */) {/* ... */}
}
clean
cleans working directorybuild
compiles source codetest
runs unit tests