0.2.2 • Published 3 years ago
@web-deps/event-manager v0.2.2
event-manager
A JavaScript library for managing events. It can be used for front-end and back-end applications.
Installation
NPM
npm install @web-deps/event-managerYarn
yarn add @web-deps/event-managerUsage
Creating an Event Emitter
import { EventEmitter } from "@web-deps/event-manager";
// Subject for events
let form = {
send: () => {
console.log("Sending form");
},
clear: () => {
console.log("Clearing form");
},
alert: (alertType) => {
console.log(`Alerting user about ${alertType}.`);
}
};
const events = ["submit", "success", "failure"];
const formEventManager = new EventEmitter(form, events);Adding an Observer
// Import EventEmitter and create an event emitter instance 'formEventManager'
formEventManager.addObserver("submit", ({ name, subject }) => {
subject.send();
});Emitting an Event
import { EventEmitter, Event } from "@web-deps/event-manager";
// Create an event emitter and add an observer
formEventManager.emit(new Event("submit"));
// Output: 'Sending form'API
Event
A class used to create events.
Constructor
Signature
constructor(name, subject [, data]);Params
name:- Type:
string - Description: The name of the event.
- Type:
subject:- Type:
object - Description: The subject of observation.
- Type:
data: (optional)- Type:
any - Description: The data associated with an event.
- Type:
Properties
name:- Type:
string - Description: The name of the event.
- Type:
subject:- Type:
object - Description: The subject of observation.
- Type:
data: (optional)- Type:
any - Description: The data associated with an event.
- Type:
EventEmitter
A class used to manage events.
Constructor
Signature
constructor(subject, events);Params
subject:- Type:
object - Description: The subject of observation.
- Type:
events:- Type:
arrayofstrings - Description: The events to be emitted and observed.
- Type:
Properties
subject: (readonly)- Type:
object - Description: The subject of observation.
- Type:
events: (readonly)- Type:
arrayofstrings - Description: The events to be emitted and observed.
- Type:
observers: (readonly)- Type:
objectoffunctions - Description: The observers of events.
- Type:
Methods
addObserver:- Description: Adds an observer for a particular event.
- Signature:
addObserver(eventName, observer) - Params:
eventName:- Type:
string - Description: The name of the event to be observed.
- Type:
observer:- Type:
function - Signature:
observer(event) - Params:
event:- Type:
Event - Description: The event emitted.
- Type:
- Type:
removeObserver:- Description: Removes an observer for a particular event.
- Signature:
removeObserver(eventName, observer) - Params:
eventName:- Type:
string - Description: The name of the event observed.
- Type:
observer:- Type:
function - Signature:
observer(event) - Params:
event:- Type:
Event - Description: The event emitted.
- Type:
- Type:
notifyObservers: (protected)- Description: Notifies observers of a particular event when the event has been fired.
- Signature:
notifyObservers(eventName [, data]) - Params:
eventName:- Type:
string - Description: The name of the event emitted.
- Type:
data: (optional)- Type:
any - Description: Data associated with the event.
- Type:
emit:- Description: Emits an event.
- Signature:
emit(eventName [, data]) - Params:
eventName:- Type:
string - Description: The name of the event to emit.
- Type:
data: (optional)- Type:
any - Description: Data associated with the event.
- Type:
eventIsRegistered:- Description: Checks whether a particular event is registered or not.
- Signature:
eventIsRegistered(eventName) - Params:
eventName:- Type:
string - Description: The name of the event to be checked.
- Type:
License
MIT License.