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-pro

v1.2.2

Published

一个简单的 Javascript 事件管理,支持浏览器端和 node 端。

Downloads

3,167

Readme

emitter-pro

npm codecov GitHub

一个简单的 Javascript 事件管理,支持浏览器端和 node 端。

使用

安装

npm install emitter-pro
yarn add emitter-pro
pnpm add emitter-pro

示例

import Emitter from 'emitter-pro';

const emitter = new Emitter();

// 注册监听方法
emitter.on('foo', () => console.log('bar'));
emitter.on('foo', () => console.log('baz'));
emitter.on('foo', () => console.log(42));

// 触发方法
emitter.emit('foo');

// 取消监听
emitter.off('foo');

实例方法

on(eventName: string|symbol, listener: F, context?: object | null)

注册监听方法。允许多次添加同一引用的函数。

返回当前实例。

const emitter = new Emitter();

// 注册监听方法
emitter.on('foo', () => console.log('bar'));
emitter.on('foo', () => console.log(42));

emitter.emit('foo');
// bar
// 42

emit(eventName: string|symbol, ...args: Parameters)

触发监听方法。

返回当前实例。

const emitter = new Emitter();

emitter.on('foo', () => console.log('bar'));
emitter.on('foo', () => console.log(42));

emitter.emit('foo');
// bar
// 42

// 支持传入参数
emitter.on('test' (a, b) => console.log(a + b));
emitter.on('test' (a, b) => console.log(a * b));

emitter.emit('test', 2, 5);
// 7
// 10

emitter.emit('test', 5, 5);
// 10
// 25

off(eventName: string|symbol, listener?: F)

取消监听方法。如果不传第二个参数,将取消该事件名称的全部监听方法。如果多次添加同一引用的函数,需要多次删除。

返回当前实例。

const emitter = new Emitter();

const fn = () => console.log('bar');

emitter.on('foo', fn);
emitter.on('foo', () => console.log('baz'));
emitter.on('foo', () => console.log(42));

emitter.emit('foo');
// bar
// baz
// 42

emitter.off('foo', fn); // 取消 foo 的监听方法 fn
emitter.emit('foo');
// bar
// 42

emitter.off('foo'); // 取消 foo 的全部监听方法
emitter.emit('foo'); // 什么都没发生

once(eventName: string|symbol, listener: F, context?: object | null)

仅触发一次的监听方法。使用方法同 on

返回当前实例。

const emitter = new Emitter();

// 注册监听方法
emitter.on('foo', () => console.log('bar'));
emitter.once('foo', () => console.log(42));

emitter.emit('foo');
// bar
// 42

emitter.emit('foo');
// bar

offAll()

取消全部事件名称的监听方法。

返回当前实例。

const emitter = new Emitter();

const fn = () => console.log('bar');
emitter.on('test', fn);
emitter.on('test', () => console.log('baz'));
emitter.on('test', () => console.log(42));

emitter.on('other', fn);
emitter.on('other', () => console.log('baz'));

emitter.offAll(); // 取消全部监听方法

emitter.emit('test'); // 什么都没发生
emitter.emit('other'); // 什么都没发生

eventNames()

获取全部事件名称。

返回事件名称数组。

const emitter = new Emitter();

const fn = () => console.log('bar');
emitter.on('test', fn);
emitter.on('other', fn);

console.log(emitter.eventNames()); // ["test", "other"]

listeners(eventName: string|symbol)

获取事件名称的全部监听方法(如通过 once 方法注册,返回的是包装方法)。

返回事监听方法数组。

const emitter = new Emitter();

const fn1 = () => console.log('bar');
const fn2 = () => console.log('baz');
emitter.on('test', fn1);
emitter.once('test', fn2);

console.log(emitter.listeners('test')); // [fn1, wrapFn2]

rawListeners(eventName: string|symbol)

获取事件名称的全部监听方法(原始方法,未经过包装处理)。

返回对应事件名称的监听方法数组。

const emitter = new Emitter();

const fn1 = () => console.log('bar');
const fn2 = () => console.log('baz');
emitter.on('test', fn1);
emitter.once('test', fn2);

console.log(emitter.rawListeners('test')); // [fn1, fn2]

hasListener(eventName: string|symbol, listener: F)

判断监听方法是否存在。

内部使用 rawListeners 获取原始方法进行判断。

const emitter = new Emitter();

const fn1 = () => console.log('bar');
const fn2 = () => console.log('baz');
emitter.on('test', fn1);
emitter.once('test', fn2);

emitter.hasListener('test', fn1); // true
emitter.hasListener('test', fn2); // true

emitter.emit('test');

emitter.hasListener('test', fn1); // true
emitter.hasListener('test', fn2); // false

prependListener(eventName: string|symbol, listener: F, context?: object | null)

注册监听方法。同 on 方法,只是将监听方法添加到最前面(事件触发是按顺序执行)。

返回当前实例。

const emitter = new Emitter();

// 注册监听方法
emitter.on('foo', () => console.log('bar'));
emitter.prependListener('foo', () => console.log(42));

emitter.emit('foo');
// 42
// bar

prependOnceListener(eventName: string|symbol, listener: F, context?: object | null)

仅触发一次的监听方法。同 once 方法,只是添加到最前面(事件触发是按顺序执行)。

返回当前实例。

const emitter = new Emitter();

// 注册监听方法
emitter.on('foo', () => console.log('bar'));
emitter.prependOnceListener('foo', () => console.log(42));

emitter.emit('foo');
// 42
// bar

emitter.emit('foo');
// bar