Evem
Tiny global event emitter for Micro-frontends (or if you need to talk to other js files in your document)
Installation
npm
npm install evem
yarn
yarn add evem
Usage
Creating an Evem instance
// Emitter class is a singleton: one instance for the whole document
// you can optionally pass a debug option for friendly console warns
const emitter = new Evem({ debug: true });
Sample supported callback
interface ThisCBData {
from: string;
}
const CLICK_EVENT = "click-event";
// the data is what is passed in the 2nd argument during emit(EVENT, data);
const clickCallback = (data: ThisCBData) => {
alert(`clicked from ${data.from}!`);
};
Adding a callback
emitter.on(CLICK_EVENT, clickCallback);
Or using once
for one-time callbacks
emitter.once(CLICK_EVENT, clickCallback);
Emitting an event (anywhere, outside of your app, probably...)
// `emitter.emit()` returns the Evem instance, so its chainable
myButton.addEventListener("click", () => {
const data: ThisCBData = { from: "me" };
emitter.emit(CLICK_EVENT, data).emit(CART_UPDATE, data);
});
Removing a callback
emitter.removeOn(CLICK_EVENT, clickCallback);
// or if you prefer to not keep track of listener callbacks, `on` returns a callback remover
const removeClickListener = emitter.on(CLICK_EVENT, clickCallback);
removeClickListener();
Removing a registered event and all its callbacks
emitter.removeEvent(CLICK_EVENT);
Running the example
yarn global add parcel
yarn
yarn start
License
MIT
0.0.0
1 year ago
1.0.20
3 years ago
1.0.17
3 years ago
1.0.16
4 years ago
1.0.15
4 years ago
1.0.14
4 years ago
1.0.13
4 years ago
1.0.12
4 years ago
1.0.11
4 years ago
1.0.10
4 years ago
1.0.9
4 years ago
1.0.8
4 years ago
1.0.7
4 years ago
1.0.6
4 years ago
1.0.5
4 years ago
1.0.4
4 years ago
1.0.3
4 years ago
1.0.2
4 years ago
1.0.1
4 years ago
1.0.0
4 years ago