A JavaScript library for managing events. It can be used for front-end and back-end applications.
npm install @web-deps/event-manager
yarn add @web-deps/event-manager
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);
// Import EventEmitter and create an event emitter instance 'formEventManager'
formEventManager.addObserver("submit", ({ name, subject }) => {
subject.send();
});
import { EventEmitter, Event } from "@web-deps/event-manager";
// Create an event emitter and add an observer
formEventManager.emit(new Event("submit"));
// Output: 'Sending form'
A class used to create events.
constructor(name, subject [, data]);
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:
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:
A class used to manage events.
constructor(subject, events);
subject
:- Type:
object
- Description: The subject of observation.
- Type:
events
:- Type:
array
ofstring
s - Description: The events to be emitted and observed.
- Type:
subject
: (readonly)- Type:
object
- Description: The subject of observation.
- Type:
events
: (readonly)- Type:
array
ofstring
s - Description: The events to be emitted and observed.
- Type:
observers
: (readonly)- Type:
object
offunction
s - Description: The observers of events.
- Type:
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:
MIT License.