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

eventbus-cjs

v1.0.0

Published

基于JavaScript装饰器(Decorator)实现的通信库, 支持Vue / React等常用框架

Downloads

5

Readme

eventbus-cjs

eventbus-cjs 是一个基于JavaScript装饰器(Decorator)实现的通信库, 支持Vue / React等常用框架, 支持node.js tests npm npm npm

使用方法

1. 安装

npm i eventbus-cjs --save

Babel 转码器的支持

安装 babel-plugin-transform-decorators-legacy

npm i babel-plugin-transform-decorators-legacy -D

配置 .babelrc 文件

"plugins": ["transform-decorators-legacy"]

如果是使用 create-react-app 创建的项目,需要先弹出 webpack 配置

npm run eject

安装 babel-plugin-transform-decorators-legacy,在 package.json 中配置 babel

"babel": {
    "presets": [
        "react-app"
    ],
    "plugins": [
        "transform-decorators-legacy"
    ]
  }

vue-cli 3.x 以默认支持 Decorator。

2. 引入 eventbus-cjs

import { on, once, emit } from 'eventbus-cjs';

修饰器

属性方法修饰器
类修饰器

3. 使用

@emit

发送消息

@emit('event_a')
send(msg) {
    return 'send ' + msg;
};

@on

监听消息, 注意在Vue属性方法上监听,vue.methods中的方法无法监听

@on('event_a')
onMessageA(msg) {
   
};
// vue
@register
created(){
    
},
@on('event_a')
onMessage(msg){
    // 和生命周期函数相似的 this
}

@once

监听消息,只执行一次, 注意在Vue属性方法上监听,vue.methods中的方法无法监听

@once('event_once')
onmessage(msg) {
    console.log(msg);
};

@remove

移除当前对象监听的事件

@remove('event_a', 'event_b')('onMessageA')
// @remove('event_a', 'event_b')('onMessageA', 'onMessageB')
// @remove('event_a', 'event_b')(['onMessageA1', 'onMessageA2'])
// @remove('event_a', 'event_b')(['onMessageA1', 'onMessageA2'], 'onMessageB')
// @remove('event_a', 'event_b')()
// @remove()()  //释放掉当前对象所监听的所有事件,等同于 @unregister
componentWillUnmount() {
    
}
// vue
@remove()()
beforeDestory(){

}

@register

@register 和 @unregisster 最好同时配置

Vue中必须进行注册才能正确监听消息,需在生命周期方法上注册,越早越好

// react
@register
class MyComponent extends Component{

}

// vue
export default {
    @register
    created() {

    }
}

通过@register修饰的对象可以直接通过 this.$emitter 对象发送、接收消息

this.$emiiter.emit(eventName, msg);
this.$emiiter.on(eventName, function(){
    // receive messages
})

@unregister

释放掉当前对象所监听的所有事件

@unregister
componentWillUnmount() {
    
}
// vue
@unregister
beforeDestory(){

}