Skip to content

A JavaScript library for managing custom events. It can be used both on the frontend and backend.

License

Notifications You must be signed in to change notification settings

web-deps/event-manager

Repository files navigation

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-manager

Yarn

yarn add @web-deps/event-manager

Usage

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.
  • subject:
    • Type: object
    • Description: The subject of observation.
  • data: (optional)
    • Type: any
    • Description: The data associated with an event.

Properties

  • name:
    • Type: string
    • Description: The name of the event.
  • subject:
    • Type: object
    • Description: The subject of observation.
  • data: (optional)
    • Type: any
    • Description: The data associated with an event.

EventEmitter

A class used to manage events.

Constructor

Signature
constructor(subject, events);
Params
  • subject:
    • Type: object
    • Description: The subject of observation.
  • events:
    • Type: array of strings
    • Description: The events to be emitted and observed.

Properties

  • subject: (readonly)
    • Type: object
    • Description: The subject of observation.
  • events: (readonly)
    • Type: array of strings
    • Description: The events to be emitted and observed.
  • observers: (readonly)
    • Type: object of functions
    • Description: The observers of events.

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.
      • observer:
        • Type: function
        • Signature: observer(event)
        • Params:
          • event:
            • Type: Event
            • Description: The event emitted.
  • removeObserver:
    • Description: Removes an observer for a particular event.
    • Signature: removeObserver(eventName, observer)
    • Params:
      • eventName:
        • Type: string
        • Description: The name of the event observed.
      • observer:
        • Type: function
        • Signature: observer(event)
        • Params:
          • event:
            • Type: Event
            • Description: The event emitted.
  • 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.
      • data: (optional)
        • Type: any
        • Description: Data associated with the event.
  • emit:
    • Description: Emits an event.
    • Signature: emit(eventName [, data])
    • Params:
      • eventName:
        • Type: string
        • Description: The name of the event to emit.
      • data: (optional)
        • Type: any
        • Description: Data associated with the event.
  • 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.

License

MIT License.

About

A JavaScript library for managing custom events. It can be used both on the frontend and backend.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published