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

emitter-service

v1.1.5

Published

is typescript pubsub

Downloads

3

Readme

emitter-service

在 mitt 基础上添加了 once,runOnce 方法的发布订阅库 再次感谢 mitt 的作者,为我门提供如此优秀的解决方案,https://github.com/developit/mitt

// using ES6 modules import pubsub from 'emitter-service'

// using CommonJS modules var pubsub = require('emitter-service')

用法

import pubsub from 'emitter-service'

const emitter = pubsub()

// 订阅foo事件
emitter.on('foo', e => console.log('foo', e))

// 订阅全部事件
emitter.on('*', (type, e) => console.log(type, e))

// 发布foo事件
emitter.emit('foo', { a: 'b' })

// 清除所有订阅
emitter.all.clear()

function onFoo() {}
emitter.on('foo', onFoo) // 订阅
emitter.off('foo', onFoo) // 取消订阅

emitter.once('foo', onFoo) // 订阅一次

// runOnce 用法
// runOnce 类似节流的方式实现让promise只执行一次,但可以在结果返回的时候全部拿到结果,这在某些特定情况很有用
const promiseFn = (num: number) => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(num)
    }, Math.random() * 50)
  })
}
const wrap = emitter.runOnce(promiseFn)

for (let i = 0; i < 10; i++) {
  wrap(i)
    .then(res => {
      console.log(res) // 这里会输出10次0 因为只会执行一次promiseFn
    })
    .catch(err => {
      console.log(err)
    })
}

// 还提供了缓存功能,比如某个请求你只想发送一次,后面想直接拿到结果就可以用这个参数来控制
const wrap = emitter.runOnce(promiseFn, promiseFn.toString(), true)

for (let i = 0; i < 10; i++) {
  await wrap(i)
    .then(res => {
      console.log(res) // 这里会输出10次0 因为缓存了第一次执行的结果
    })
    .catch(err => {
      console.log(err)
    })
}