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

a-signal

v2.2.4

Published

Simple signal emitter.

Downloads

147

Readme

a-signal

A lightweight, feature-rich signal/event emitter for single events. Perfect for handling state changes, async operations, and event-driven architectures.

Installation

npm install a-signal

Features

Unlike typical event emitters that handle multiple named events, a-signal provides specialized features:

  • Single Event Focus: Each signal instance is a dedicated event channel
  • Priority-based Listeners: Control execution order with numeric priorities (higher executes first)
  • Late Listener Support: Catch events that happened before subscription
  • Memory Mode: Remember exact arguments for late subscribers
  • Promise-based Waiting: Await next signal emission with optional timeout
  • Emission Control: Stop event propagation at any point
  • Memory Management: Clear memory and listeners independently
  • Extractable Methods: Create clean APIs by extracting methods
  • One-time Listeners: Auto-unsubscribe after first execution

Examples

Basic Usage

Simple subscription and emission - the foundation of signal usage.

const signal = new Signal()
signal.on(data => console.log(data))
signal.emit('Hello!')

Priority and Control Flow

Control the order of execution and stop propagation when needed. Useful for middleware-like patterns.

const signal = new Signal({ prioritized: true })

// Higher priority executes first
signal.on(() => console.log('Second'), 1)
signal.on(() => console.log('First'), 100)

// Stop propagation
signal.on(() => {
    console.log('Stop here')
    signal.break()
})

State Management

Perfect for handling initialization states and late-joining components. Combines late listeners with memory to ensure consistent state.

const signal = new Signal({ 
    late: true,      // Get events that happened before subscribing
    memorable: true  // Remember the arguments too
})

signal.emit('state', { value: 42 })

// Later subscriber still gets the event
signal.on((type, data) => {
    console.log(type, data.value) // 'state', 42
})

Async Operations

Convert event-based code into Promise-based code. Great for handling timeouts and async flows.

const signal = new Signal({ timeout: 5000 })

// Wait for next emission
try {
    const value = await signal.wait()
    console.log('Got:', value)
} catch {
    console.log('Timeout')
}

Clean APIs

Simple extraction: Create minimal, focused APIs by extracting just the methods you need.

const signal = new Signal()

// Extract methods to create a clean interface
const { emit, on } = {
    emit: signal.extractEmit(),
    on: signal.extractOn()
}

// Clean usage
on(data => console.log(data))
emit('Hello!')

With full type documentation: For larger applications, create well-documented, type-safe APIs with extracted methods.

class UserAPI {
    /** @type {Signal<[string, {id: number, name: string}]>} */
    #signal = new Signal()

    constructor() {
        /**
         * Emit user events
         * @param {string} event - Event type ('login'|'logout'|'update')
         * @param {{id: number, name: string}} user - User data
         * @returns {void}
         */
        this.emit = this.#signal.extractEmit()

        /**
         * Subscribe to user events
         * @param {(event: string, user: {id: number, name: string}) => void} handler
         * @returns {{ off: () => void }} Subscription handle
         */
        this.on = this.#signal.extractOn()
    }
}

// Usage remains type-safe
const api = new UserAPI()
api.on((event, user) => console.log(`${event}: ${user.name}`))
api.emit('login', { id: 1, name: 'John' })

Memory Management

Control signal's memory and subscription lifecycle. Useful for cleanup and managing long-living signals.

// Clear memory but keep listeners
signal.forget()

// Remove listeners but keep memory
signal.wipe()

// Clear everything
signal.wipe(true)

TypeScript Support

const signal = new Signal<string>()
signal.on((data: string) => console.log(data))

// Multiple arguments
const multiSignal = new Signal<[number, string]>()
multiSignal.on((num, str) => console.log(num, str))

License

MIT License - Volodymyr Ishchenko - feel free to use this project commercially.


With love ❤️ from Ukraine 🇺🇦