Socket
Socket
Sign inDemoInstall

c-pubsub

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    c-pubsub

simple publish/subscribe mode


Version published
Maintainers
1
Created

Readme

Source

c-pubsub

simple publish/subscribe mode. 一套发布订阅模式,可快速的订阅及发布事件。

Insatll

npm i -S c-pubsub

Usage

pubsub.on 订阅事件, pubsub.emit 发布事件

import pubsub from 'c-pubsub';

// 订阅事件
pubsub.on('event', (arg) => {
  console.log(arg);
});

// 发布事件
pubsub.emit('event', 'Hello');

// Hello

pubsub.one 也可以订阅一次性事件,监听到一次事件后就会自动卸载事件。

pubsub.one('event', (arg) => {
  console.log(arg);
});

pubsub.emit('event', 'Hello'); // Hello

pubsub.emit('event', 'Hello'); // 已经自动卸载过事件了,所不会有反应。

pubsub.off 可以卸载事件,会卸载该事件下的所有回调。

pubsub.on('event', (arg) => {
  console.log('1', arg);
});

pubsub.on('event', (arg) => {
  console.log('2', arg);
});

pubsub.one('event', (arg) => {
  console.log('3', arg);
});

pubsub.off('event'); // 卸载了 event 事件

pubsub.emit('event', 'Hello'); // 不会有任何反应,因为所有监听 event 的回调函数都被卸载了。

Keywords

FAQs

Last updated on 01 Feb 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc