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

@vyke/emitter

v0.1.1

Published

Functional and tiny (<1kb) functions to query and handle the dom in a safe, easy and TypeScript friendly.

Downloads

4

Readme

  • With a small core
  • Plugin support
  • Typescript friendly
  • No dependencies

Installation

npm i @vyke/emitter

Examples

import { createEmitter } from '@vyke/emitter'

const emitter = createEmitter()

function onLogin(session: { username: string }) {
	console.log('logged in', session.username)
}

const offLogin = emitter.on('login', onLogin)

emitter.emit('login', { username: 'albizures' })

offFoo()
// or
emitter.off('login', onLogin)

Typescript

import { createEmitter } from '@vyke/emitter'

type Events = {
	login: { username: string }
}

const emitter = createEmitter<Events>()

emitter.on('login', (session) => {
	console.log('logged in', session.username) // session.username is inferred as string
})

emitter.emit('login', { username: 'albizures' })

Plugins

import { createEmitter } from '@vyke/emitter'

const emitter = createEmitter().use((emitter) => {
	emitter.on('login', () => {
		console.log('login event')
	})
}).use((emitter) => {
	return {
		...emitter,
		onLogin: (cb: () => void) => emitter.on('login', cb),
	}
})

watcher

Plugin to watch all events emitted.

import { createEmitter } from '@vyke/emitter'
import { withWatcher } from '@vyke/emitter/watcher'

const emitter = createEmitter().use(withWatcher)

emitter.watch((name, value) => {
	console.log('event', name, 'emitted with', value)
})

unique handlers

Plugin to ensure that a handler is only added once.

import { createEmitter } from '@vyke/emitter'
import { withUniqueHandlers } from '@vyke/emitter/unique-handlers'

const emitter = createEmitter().use(withUniqueHandlers)

const onLogin = () => {
	console.log('login event')
}

emitter.on('login', onLogin)
emitter.on('login', onLogin) // this will not be added

once

Plugin to listen to an event only once.

import { createEmitter } from '@vyke/emitter'
import { withOnce } from '@vyke/emitter/once'

const emitter = createEmitter().use(withOnce)

emitter.once('login', () => {
	console.log('login event') // this will be emitted only once
})

emitter.emit('login')
emitter.emit('login') // this will be emitted but not listened anymore

watcher

Plugin to watch all events emitted.

import { createEmitter } from '@vyke/emitter'
import { withWatcher } from '@vyke/emitter/watcher'

const emitter = createEmitter().use(withWatcher)

emitter.watch((name, value) => {
	console.log('event', name, 'emitted with', value)
})

emitter.emit('login', { username: 'albizures' })

withOptions

Plugin to add a options object when listening to events. This plugin accepts a options handler that will be called when the handler is added.

Built-in options handlers options:

  • withGroups: to group events by a string. This will be useful to remove all events from a group.
import { createEmitter } from '@vyke/emitter'
import { createGroup, withGroups, withOptions } from '@vyke/emitter/options'

const authGroup = createGroup()

const emitter = createEmitter()
	.use(withOptions(withGroups))

emitter.on('login', () => {
	console.log('login event')
}, { group: authGroup })
emitter.on('logout', () => {
	console.log('logout event')
}, { group: authGroup })

authGroup.off()

emitter.emit('login')
emitter.emit('logout')
// nothing will be logged

API

createEmitter

functional event emitter / pubsub.

withOptions

Plugin that allows for adding options to event handlers.

const withLog = withOptions((options, { name, handler }) => {
	if (options.log) {
		console.log(`Adding handler for ${name}`)
	}
})

const emitter = createEmitter().use(withLog)

emitter.on('foo', () => {})
// Logs: Adding handler for foo

Others vyke projects