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

async-hook-manager

v0.5.1

Published

Async hook manager built on top of the Q promise library.

Downloads

11

Readme

Async hook manager built on top of the Q promise library.

Currently only CommonJS support.

Basic usage

npm install async-hook-manager

    
    // Load up AHM.
    var AHM = require('async-hook-manager');

    // Create a new instance of AHM.
    var myHookManager = AHM();

    // Register an async hook.
    // The return value is the ID of the hook, which can later be used to
    // unregister the hook.
    var asyncHookId = myHookManager.registerHook(function (call) {
      
      // Do some async stuff...
      example.fireTransitionalAnimation(function () {
        
        // On animation complete.
        call.resolve();

        // Or reject the transition with.
        // call.reject();
      });
    });

    // Register a sync hook.
    var syncHookId = myHookManager.registerHook(function () {

      // Do something synchronously here...
      example.updateSomeDataSynchronously();

      return true;
    });

    // Make a call via the hook manager instance.
    // This will call all of the registered hooks, and wait for all to respond
    // before firing its callback.
    myHookManager.makeCall(function () {

      // Do something that has to wait for all of the hooks to return.
      example.updateTheCurrentRoute();

    }, function () {
      
      // Handle a failed call...

    });

    // Unregister a hook that is no longer required.
    myHookManager.unregisterHook(syncHookId);

API instance Methods

Register a new hook

.registerHook(hookCallback[, reference])

Register a hook. The callback passed to this method is called whenever a call to the corresponding AHM instance. The hookCallback is passed an object that contains the optional payload property that can be passed to .makeCall, as well as the callId, and callback functions to resolve or reject the call. These callbacks provide the simplest way of performing async work and then resolving/rejecting. Alternatively, the callback can return a boolean to resolve/reject synchronously, or return a promise to later be resolved/rejected.

This method returns the id of the hook that's been registered. This can be used to unregister the hook. The reference argument is optional, and can contains a string or an object to help with identification of the hook.

Unregister a hook

.unregisterHook(hookId)

The hookId corresponds to the return value of .registerHook. Unregisters the hook from the instance.

Unregister all hooks

'.unregisterAllHooks()'

Unregister all hooks registered to the corresponding instance.

Get all currently registered hooks

.getAllRegisteredHooks()

Returns the hook registry. This contains the callback, the hookId and an optional reference for each registered hook.

Make a call

.makeCall([thenCallback, ][failCallback, ][options])

Options:

  • respectPreviousCalls [boolean]: whether to wait for previous calls to resolve / reject before firing the current call's callback
  • payload [object/any]: will be passed to each of the registered hooks' callback functions

Can be passed then and fail callbacks directly. Additionally returns a promise that has its own .then method. If you are concerned with the order that calls are called in, then avoid using the returned promise's .fail method, as this cannot be guaranteed to fire sequentially. You can pass an optional payload to the options argument; this will be delivered to all of the registered hooks.

Get the ID of the last call.

.getLastCallId()

Get the ID of the last call to be made to the instance.

Development / testing

Clone the repository, and run npm install to install dev and runtime dependencies. You can then run npm test to run the suite of tests. This includes jshint.