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

zhook

v0.1.2

Published

Tiny hooks library, mostly for plugin system.

Downloads

4

Readme

🪝 zhook

Tiny hook library, with event dispatcher and filters (aka waterfall hook), mostly for creating plugin system.

This library is still experimental and its APIs may change in the future.

✨ Features

  • Small: No dependencies. Size is less than 130 bytes (minified and gzipped), controlled by Size Limit.
  • Works on All Runtimes: It doesn't rely on any platform API, so you can use it at browsers, Node.js or Deno.
  • Simple: Simple API, and doesn't rely on this.
  • TypeScript Friendly: Source code is written in TypeScript with better types.
  • Modern: Shipped as ES Modules, and targets to ES2017.

💿 Install

Node.js

Note that this library requires Node.js 12 or newer.

$ npm i zhook

Then you can import it with ES Modules:

import { /* */ } from 'zhook'

Browsers (No bundlers)

import { /* */ } from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/mod.js'

Deno

import { /* */ } from 'https://cdn.jsdelivr.net/npm/[email protected]/mod.ts'

🎬 Usage

createDispatcher

Dispatcher is similar to Node.js EventEmitter but it's decentralized and isolated for each event. So, each event can be easily typed.

import { createDispatcher } from 'zhook'

const dispatcher = createDispatcher<[x: number, y: number]>()

dispatcher.on((x, y) => console.log(x, y))
dispatcher.on((x, y) => Promise.resolve([x, y]))
dispatcher.emit(1, 2)

createSyncFilter

Filter is similar to waterfall hook in webpack Tapable.

All arguments will be passed in the order of registered listeners. All registered listeners must return a value and that value will be passed as the first argument to next listener, while the rest arguments won't be changed.

Note that all listeners must be synchronous, so listeners can't be async/await function or return a Promise. If your listeners need to be asynchronous, see below.

import { createSyncFilter } from 'zhook'

const filter = createSyncFilter<[value: number, context: number]>()

filter.on((value, context) => value + 1)
filter.on((value, context) => value + 2)
const result = filter.emit(/* initial value */ 1, 'some text')

createAsyncFilter

AsyncFilter is similar to SyncFilter, but this allows you to create filter while its listeners can be synchronous or asynchronous.

import { createAsyncFilter } from 'zhook'

const filter = createAsyncFilter<[value: number, context: number]>()

filter.on((value, context) => value + 1)
filter.on((value, context) => Promise.resolve(value + 2))
const result = await filter.emit(/* initial value */ 1, 'some text')

📃 License

MIT License

2021-present (c) Pig Fang