npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

library-event-manager

v1.0.20

Published

Namespace and action based multi level events management for node and web applications

Downloads

6

Readme

Event Manager

Maintainability

Namespace and action based multi level events management for node and web applications.

This module can be used to pass variables from system to system in a stateless environment. A part can listen to an event, and another can raise this event. Methods can be set as chained promise calls or async.

Global access

To make sure that there is a single instance for the event manager, you can keep a reference to the created object in node's globals, or in dom's window modules depending on the platform you are building for.

To define the code completion and a Typescript reference, create a new typescript file in your project. If you are developing for node environment, you can extend the global object for event manager like this:

import EventManager from 'library-event-manager';

declare global {
  namespace NodeJS {
    interface Global {
      eventManager: EventManager;
    }
  }
}
  
global.eventManager = new EventManager();

If you are developing for web browsers, you can extend the window object for event manager like this

import EventManager from 'library-event-manager';

declare global {
  interface Window {
    eventManager: EventManager;
  }
}

window.eventManager = new EventManager();

Registering for events

In this example, we are defining an interface for passing an object called IDemoObject to demonstrate the possibility to wait for complex objects.

interface IDemoObject {
  name: string;
  surname: string;
}

const toBeCalled = ( left: number, right: number, person: IDemoObject ) => {
  const c = left + right;
  console.log( c );

  console.log( person.name );
  console.log( person.surname );
};

The correct way of defining namespace and action names is writing exported enums to make sure you are not making any spelling errors. To assign the function as a listening endpoint the namespace/action layer, use addEventListener method.

export enum ENamespace {
  admercury = 'com.admercury',
}

export enum EAction {
  dump = 'dump',
}

global.eventManager.addEventListener( ENamespace.admercury, EAction.dump, toBeCalled );

Calling events

On any point, to call an event, use the event method of event manager. After defining the namespace and action layer, you can pass multiple arguments to the event that will be piped directly to the listening methods.

const person = {
  name   : 'John',
  surname: 'Doe',
} as IDemoObject;

global.eventManager.event( ENamespace.admercury, EAction.dump, 1, 2, person );

Unregistering from events

To unregister from an event, you can either use removeEventListener that requires namespace and action layer to be defined or removeListener for all layers. First method removes the listener from the specified layer. Second method removes it from all possible layers that the method is listening for.

// Remove from a single namespace action layer
global.eventManager.removeEventListener( ENamespace.admercury, EAction.dump, toBeCalled );
// Remove from all possible layers
global.eventManager.removeListener( toBeCalled );

Single time calls

You can define a special listener that will only be called once by the defined event. This can be achieved by simply telling the addEventListener method that this method is a single time listener. These listeners will be removed from the internal reference list of event manager right after the call is made.

global.eventManager.addEventListener( ENamespace.admercury, EAction.dump, toBeCalled, true );

Notes

If you are specifying your target something newer than es6, Typescript will change your moduleResolution to "Classic" mode. This might cause an "extension not found" error in your code. Please make sure that your tsconfig.json file specifies module resolutions as Node.

{
  "compilerOptions": {
    "moduleResolution": "Node"
  }
}