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

plugin-hooks

v2.0.0

Published

`plugin-hooks` is a Typescript npm package that provides the ability to compose and execute functions as a chain of plugins. It supports both synchronous and asynchronous plugins.

Downloads

535

Readme

plugin-hooks

plugin-hooks is a Typescript npm package that provides the ability to compose and execute functions as a chain of plugins. It supports both synchronous and asynchronous plugins.

Installation

Install plugin-hooks using npm:

$ npm install plugin-hooks

Create a Plugin

You can create a plugin using one of the following functions:

Create an Asynchronous Plugin

To create an asynchronous plugin, use the createAsyncPlugin function. This function takes two optional parameters which are the initialValue and options.

import { createAsyncPlugin } from 'plugin-hooks';

const plugin = createAsyncPlugin<number, {}>();

The parameters are:

  • initialValue (optional): the initial value of the plugin.
  • options (optional): an object that contains options for the plugin.

The options object supports the following option:

  • returnOnFirst (optional): a boolean value that indicates whether the plugin should return the first fulfilled result instead of continuing

Create a Synchronous Plugin

To create a synchronous plugin, use the createSyncPlugin function. This function takes one optional parameter which is the initialValue.

import { createSyncPlugin } from 'plugin-hooks';

const plugin = createSyncPlugin<any>();

The parameters are:

  • initialValue: the initial value of the plugin.

Push Middleware

You can add middleware to the plugin using the pushMiddleware function. This function takes a function as its only parameter.

plugin.pushMiddleware(function times2(val) {
  // do something with the value
});

Dispatch

Once you have added all the middleware you need, you can dispatch the plugin with the dispatch function. This function takes the initialValue as its only parameter and returns the final value will be resolved with the final value.

const r1 = await asyncPlugin.dispatch(1);

Finish Plugin Early

For asynchronous plugins, you can finish the plugin early with the finish function. This function takes a value as its only parameter and immediately resolves the promise.

plugin(function times2(val, _, self) {
  return self.finish(555);
});

For synchronous plugins, you can finish the plugin early with the finish function. This function takes a value as its only parameter.

plugin(function times2(val, _, context) {
  return context.finish(555);
});

Return Initial Value When Without Plugins

If there are no middleware added to the plugin, the initial value will be returned when the dispatch function is called.

const r1 = await plugin.dispatch(1);
expect(r1).toBe(1);

Throw Errors

If an error is thrown in a synchronous plugin, it will be propagated to the dispatch function.

plugin(function times2(val) {
  throw 'foo';
});

expect(() => plugin.dispatch(mutable)).toThrow('foo');