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

subscription

v3.0.0

Published

Simple abstraction for first-class event emitters

Downloads

21,586

Readme

Subscription

A subscription is an object that emits events. Rather than overloading objects with other roles to emit events, and having a single object emit several types, as in .addEventListener style events, a subscription is a dedicated object representing a single event source.

// Instead of adding listeners by string type
object.addEventListener("message", m => { ... })

// You create a subscription object when you construct the base
// object...
class MyObject {
  constructor() {
    this.message = new Subscription
    ...
  }
}

// ...and add handlers directly to that:
myObject.message.add(m => { ... })

The interface of a subscription is roughly based on js-signals, but whereas js-signals have a fixed dispatch algorithm, subscriptions come in several forms.

The perceived benefits of subscriptions are that they are easier to check statically (instead of a single method dispatching on a bunch of strings, you have actual objects representing your event types, each of which expects a single type of handler function), and lead to more explicit code.

Distribution

This module is licensed under an MIT license. The recommended way to install it is with NPM.

API

class Subscription

A subscription is an object that you can add subscribers (functions) to, which will be called every time the subscription is dispatched.

add(f: Function, priority: ?number)

Add a function of the appropriate type for this subscription to be called whenever the subscription is dispatched. When priority is provided (default is zero), it determines when the function is called relative to other handlers—those with a high priority come first.

addOnce(f: Function, priority: ?number)

Add a function to be called once, the next time this subscription is dispatched.

remove(f: Function)

Remove the given subscriber from the subscription.

hasHandler() → bool

Returns true if there are any functions registered with this subscription.

dispatch(...args: [any])

Call all handlers for this subscription with the given arguments.

class PipelineSubscription extends Subscription

A type of subscription that passes the value it is dispatched with through all its subscribers, feeding the return value from each subcriber to the next one, and finally returning the result.

dispatch(value: any) → any

Run all handlers on the given value, returning the result.

class StoppableSubscription extends Subscription

A stoppable subscription is a subscription that stops calling further subscribers as soon as a subscriber returns a truthy value.

dispatch(...args: [any]) → any

Call handlers with the given arguments. When one of them returns a truthy value, immediately return that value.

class DOMSubscription extends Subscription

A DOM subscription can be used to register intermediate handlers for DOM events. It will call subscribers until one of them returns a truthy value or calls preventDefault on the DOM event.

dispatch(event: Event) → bool

Run handlers on the given DOM event until one of them returns a truty value or prevents the event's default behavior. Returns true if the event was handled.