EventAggregator
Usage
import in TypeScript
import {EventAggregator, debounce} from "eventaggregator";
import in JavaScript
const ea = require("eventaggregator");
const EventAggregator = ea.EventAggregator;
const debounce = ea.debounce;
get an instance
const ea = EventAggregator.getInstance("dom");
const ea = new EventAggregator();
emit an event
window.addEventListener("resize", event => ea.emit("resize", event));
subscribe to an event
ea.on("resize", (data: any, event: string): void => {
});
subscribe to only the first event
ea.once("resize", (data: any, event: string): void => {
});
unsubscribe a subscription
const sub = ea.on("resize", (data: any, event: string): void => {...});
sub.off();
subscribe to all events of the EventAggregator
ea.on("*", (data: any, event: string): void => {
});
multiple event subscriptions
const subs = ea.on(["click", "touchstart"], (data: any, event: string): void => {...});
subs.off();
remove all event subscriptions
ea.off("resize")
ea.off(["click", "touchstart"])
throttle an event
ea.on("very-frequent-event", throttle((data: any, event: string): void => {
}, 500));
debounce an event
ea.on("very-frequent-event", debounce((data: any, event: string): void => {
}, 500));