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

@tb-app/pub-sub

v0.5.0

Published

提供发布订阅模式的库

Downloads

3

Readme

@tb-app/pub-sub

简介

这是一个基于 JavaScript 的发布订阅库。

安装

yarn add @tb-app/pub-sub
// 或
npm i @tb-app/pub-sub

特点

  • 零依赖
  • 支持 TypeScript
  • 支持 ES 模块

API

subscribe(name,callback)

订阅名称为 name 的消息,并返回字符串 token。token 可以用来取消产生此 token 的订阅

import { subscribe, unsubscribe } from '@tb-app/pub-sub';
const callback = function (name, data) {
  console.log(name, data);
};
const token = subscribe('say', callback);
// 可以通过 token 取消订阅
unsubscribe(token);

publish(name,data)

发布名称为 name 的消息,并返回布尔值。true, 表示成功触发指定消息的回调。 false, 要触发的消息不存在或消息没有回调。

import { subscribe, publish } from '@tb-app/pub-sub';
const callback = function (name, data) {
  console.log(name, data);
};
const token = subscribe('say', callback);

setTimeout(() => {
  publish('say', 'hello !');
}, 1000);

clearSubscriptions(name)

清除指定名称的消息订阅

import { subscribe, clearSubscriptions } from '@tb-app/pub-sub';
const callback = function (name, data) {
  console.log(name, data);
};
subscribe('say', callback);

setTimeout(() => clearSubscriptions('say'), 1000);

unsubscribe(name)

清除指定的名称或 token 或函数对应的订阅。返回结果为布尔值,true 表示成功取消订阅。false,表示取消失败(订阅不存在)。

import { subscribe, unsubscribe } from '@tb-app/pub-sub';
const callback = function (name, data) {
  console.log(name, data);
};
const token = subscribe('say', callback);

setTimeout(() => {
  // 通过消息名称取消订阅
  unsubscribe('say');
  //  通过 token 取消订阅
  //  unsubscribe(token);
  // 通过函数取消订阅
  unsubscribe(callback);
}, 1000);

clearAllSubscriptions

清除所有订阅

clearAllSubscriptions();

getSubscriptions(name)

得到指定名称消息的所有回调函数数组

import { subscribe, unsubscribe } from '@tb-app/pub-sub';
const callback = function (name, data) {
  console.log(name, data);
};
subscribe('say', callback);
// 返回 [callback]
getSubscriptions('say');

subscribeOnlyOne(name,callback)

订阅消息,但是相同的消息名称,只能订阅一次

import { subscribeOnlyOne } from '@tb-app/pub-sub';
const callback = function (name, data) {
  console.log(name, data);
};
subscribeOnlyOne('say', callback);
// 下面代码将会报错
// subscribeOnlyOne('say', callback);

subscribeOnce

订阅消息,但是回调只会被调用一次,即调用后会立即取消订阅

import { subscribeOnce } from '@tb-app/pub-sub';
const callback = function (name, data) {
  console.log(name, data);
};
subscribeOnce('say', callback);
setTimeout(() => {
  publish('say', 'hello !');
  // 第二次 publish 不会触发 callback
  publish('say', 'hello !');
}, 1000);