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

als-hooks-manager

v0.5.0

Published

A lightweight JavaScript library for managing hooks in event-driven architectures.

Downloads

5

Readme

ALS Hooks Manager

als-hooks-manager is a powerful event management library, enabling you to add pre- and post-event hooks to your JavaScript functions. It's built on an EventEmitter pattern and supports asynchronous operations through promises.

Installation

To install als-hooks-manager, use npm:

npm install als-hooks-manager

Usage

How it works

By creating new instance, you creating event emitter. Unlike regular event emitter, there are two types of events ^pre events which runs before the main function and post events which runs after. Another difference, is on emit method, which gets the main function to run after pre events and before post events. emit method return promise and the whole process is running as chain, by calling next to call next function, or resolve/reject to resolve or reject the promise. The last next resolves the promise.

In addition, you can set timeout for promise and collect all errors to array.

Creating a HookManager Instance

const HookManager = require("als-hooks-manager");
const timeout = 1000; // timeout for promise
const hookManager = new HookManager(timeout);

Registering Hooks

Register a 'before' hook using the prefix ^ with your event name:

const preHook = async (args, {next}, result) => {
  // Your before-hook logic here
  next();
}
hookManager.on("^eventName", preHook);

Register an 'after' hook by simply using the event name:

const postHook = async (args, context, result) => {
  const { next, resolve, reject, errors } = context
  // Your after-hook logic here
  next();
}
hookManager.on("eventName",postHook);

Emitting Events

To emit an event, use the emit method with the event name and the main function. The main function should be an async function:

const mainFn = async (args,context,result) => {
  // Your main function logic
  context.resolve(result); // Use resolve to return a result
  return result // or just return the result
}
const args = []; // The arguments for all hooks and main function
const errors = []; // The array for collecting errors
const timeout = 1000; // timeout for resolving promise (throws timeout error if promise not resolved before this time)
hookManager.emit("eventName", mainFn,args,errors=[],timeout);

Each function in the event cycle (including the main function and hooks) receives a context object. This object includes methods such as next, resolve, and reject.

If the returned result from any function in the cycle is defined and next has not been explicitly called, the system will automatically call next(result). This feature simplifies scenarios where the result is immediately ready and there's no need for additional processing before moving to the next hook.

The result parameter is the argument in next function.

Handling Results and Errors

The emit method returns a promise, allowing you to handle results and errors:

hookManager
  .emit("eventName", async (context) => {
    context.resolve();
  })
  .then((result) => {
    // Handle result
  })
  .catch((error) => {
    // Handle error
  });

API Reference

constructor(timeout)

  • timeout (Number): Optional. Global timeout for all hooks in milliseconds.

on(eventName, listener)

  • eventName (String): Name of the event. Use ^eventName for 'before' hooks.
  • listener (Function): The function to be executed when the event is emitted.

emit(eventName, func,args,errors=[],timeout = this.timeout)

  • eventName (String): Name of the event to emit.
  • func (Function): The main function to be executed in the event cycle.
  • args (Any): The arguments for all hooks
  • errors (Array): array for collecting errors
  • timeout (Number): number of milisaconds to reject promise if not resolved before this time.
    • default value this.timeout - argument in constructor

once(eventName, listener)

  • eventName (String): Name of the event. The listener will be removed after its first invocation.

removeListener(listener)

  • listener (Function): The listener function to remove.

Context Object

Each hook function receives a context object with the following methods:

  • next(result): Proceeds to the next hook or resolves the main function.
  • resolve(result): Resolves the main promise with a result.
  • reject(error): Rejects the main promise with an error.
  • errors: The errors array for collecting errors

Example

const hookManager = new HookManager();

// Register a before hook
hookManager.on("^myEvent", async (args,context) => {
  console.log("Before main function");
  context.next();
});

// Register an after hook
hookManager.on("myEvent", async (args,context) => {
  console.log("After main function");
  context.next();
});

// Emit event
hookManager
  .emit("myEvent", async (args,context) => {
    console.log("Main function");
    context.resolve("Success");
    context.next();
  })
  .then((result) => {
    console.log(result); // Output: 'Success'
  })
  .catch((error) => {
    console.error(error);
  });