@teachingtextbooks/event-manager
v3.3.0
Published
Javascript Event Manager
Downloads
4
Keywords
Readme
Event Manager
Installation
npm install @teachingtextbooks/event-manager
Usage
import EventManager from "@teachingtextbooks/event-manager"
const EM = new EventManager();
const callback = (data: any) => {
console.log("the data:", data);
};
// add event
EM.addEventListener("string name", callback);
// remove event
EM.removeEventListener("string name", callback);
// dispatch event
// method 1 (JSON object with `type`)
EM.dispatchEvent({ type: "string name", lawnColor: "green", garden: ["potatoes", "tomatoes", "carrots"] });
// method 2 (string first parameter, and optional JSON object as the second parameter)
EM.dispatchEvent("string name", { gameCharacters: ["Mario", "Zelda", "Donkey Kong"] });
// alias for those who want to use them
// EM.on, EM.off, and EM.emit