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

msg-pubsub

v0.2.0

Published

msg-pubsub is a simple events dispatcher publish / subscribe library (just 0.9kb). aysnc, simple and usefull.

Downloads

24

Readme

msg-pubsub

build status npm version npm downloads license

English | 简体中文

msg-pubsub is a simple js lib (only 0.9kb) for dispatching message by publish/subscribe pattern.

Can be used in:

  • simple message/events dispatcher.
  • Cross-component communication of React / Vue / Angular.
  • message subscribe and publish.

API Methods

1. pub(msg_name, data1, data2, ...)

Async subscribe the message named msg_name, and with datas as the input of callback function.

2. pubSync(msg_name, data1, data2, ...)

Sync subscribe the message named msg_name, and with datas as the input of callback function, blocking.

3. sub(msg_name, callback, context)

Subscribe the msg_name with callback. callback function will execute when the message named msg_name published.

This function will return the msgObj which can be used to cancel bind with the API un(msgObj).

4. subOnce(msg_name, callback, context)

Subscribe the msg_name with callback. It will be expired once Ttriggered.

Only can be triggered once, then it will be deleted automation.

5. unsub(msgObj / msg_name)

Cancel subscribe message. You can unsubscribe a message Object, or just unsubscribe a msg_name, or unsubscribe the callback function.

6. clear()

Clear all message/events.

Example

1. Import library

npm i msg-pubsub

You can import it with <script> tag, or use keyword require or import.

import msg from 'msg-pubsub';

// or

var msg = require("msg-pubsub");

2. Simple usage

Use pub method to publish message, use sub method to subscribe message, unsub to unsubscribe.

import msg from 'msg-pubsub';

function test_callback(data1, data2) {
  console.log('this is msg 1');
}

// publish message
msg.pub('test_msg', 'test_data1', 'test_data2');

// subscribe message
var msgObj = msg.sub('test_msg', test_callback);
var msgObj2 = msg.sub('test_msg', function(data1, data2) {
  console.log('this is msg 2');
});

// cancel message
msg.unsub(msgObj); // cancel message
msg.unsub('test_msg'); // cancel all messages called `test_msg`.
msg.unsub(test_callback); // cancel all `test_callback` methods.

License

MIT