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

multi-events

v1.3.2

Published

A multiple events tool

Downloads

5

Readme

multi-events

NPM version build status Test coverage

简介

用来处理多个订阅事件和方法的订阅发布模块

安装

npm install multi-events

NPM

用法

初始化

const MultiEvent = require('multi-events');

const event = new MultiEvent(option);

option

修改默认event别名对象,可以不传递参数,按照默认配置。详情

属性

|名称|场景|注意| |:-:|:-:|:-:|
|eventsCount|获取当前实例订阅事件的数量|只读 |eventKeyCount|获取当前实例订阅事件名的数量|只读

方法

emit

触发单个或多个订阅事件
第一个参数是订阅事件名,当是多个事件订阅时,可以传入数组。

event.emit(event, params)  
event.emit([ 'event1', 'event2' ], params)  

on

订阅多个事件、监听触发多个方法,返回当前监听id,用于卸载方法。
注意,传入是单个订阅事件时,返回的是个Symbol对象;当传入多个订阅事件,返回的则是个对象。

const id1 = event.on([ 'event1', 'event2' ], fn); // 返回 { event1: Symbol(event1), event2: Symbol(event2) }  

const id2 = event.on('event1', [fn1, fn2]); // Symbol(event1);  

const id3 = event.on([ 'event1', 'event2' ], [fn1, fn2]); // {   event1: Symbol(event1), event2: Symbol(event2) }  

once

订阅事件只会触发一次就会被移除,后面再次emit将不再触发 具体使用方法和on方法一致

removeEvent

卸载当前订阅事件的所有方法,传入订阅事件名
如果仅仅是需要移除某个方法时候,可以考虑用removeEventFunction方法

event.removeEvent('event');  // true or false  
event.removeEvent([ 'event1', 'event2' ]);  // [ true, true ]  

removeEventFunction

卸载单个订阅的方法,传入订阅事件的唯一id
在用于卸载的方法时候,需要用到on方法的返回值,虽然它会返回单个symbol或object类型,但是如果仅仅是需要移除当前on的订阅事件的话,直接将它传入该方法即可,不用关心它的类型

event.removeEventFunction(id1);  // 移除整个id1.event1、id1.event2监听事件  返回[ true, true ],内部会解析为event.removeEventFunction([ id1.event1, id1.event2 ])  
event.removeEventFunction([ id1 ]);  // 返回[ true, true ],和上面表示一致  
event.removeEventFunction([ id1, id2, 'undefined' ]);  // 返回[ true, true, false ]  
event.removeEventFunction([ id1.event1, id2 ]);  // 只用传id2即可,因为本身就是symbol类型,返回 [ true, true ]  
event.removeEventFunction([ id1.event1, id3.event1 ]);  // 返回[ true, true ]  

event

通过emit函数调用,和普通订阅事件用法一致。

|名称|场景|callback| |:-:|:-:|:-:|
|trigger|当触发emit的时候触发,相同的事件只会触发一次|Function(eventName, triggerParams) |remove|当事件或方法被移除的时候触发,包括:removeEvent,removeEventFunction|Function(eventName, [ EventSub ])

可以通过在初始化的MultiEvent类的时候修改这些字段,例如:

const event = new MultiEvent({
  trigger: 'emitFn'
})
event.on('myEvent', () => {
    console.log(1);
})

event.on('emitFn', (eventName, params) => {
  console.log(eventName, params); // myEvent, [ 'params1' ]
})

event.emit('myEvent', 'params1');