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

topic

v3.0.2

Published

轻量级事件同步收发管理

Downloads

263

Readme

轻量级事件同步收发管理

GitHub Workflow Status (branch) Codecov npm npm bundle size (version) GitHub top language License

安装

yarn add topic

方法

publish

发布事件

import { topic } from 'topic';

topic.publish('name', 'message');

subscribe

订阅事件

topic.subscribe('who', (name, age) => {
  console.log('Hello' + name + ', are you ' + age + 'old?');
});

topic.publish('who', 'Tom', 12);

subscribeOnce

订阅事件,接收一次后立即取消

topic.subscribeOnce('who', (name) => {
  console.log('Hello ' + name);
});

// 有一个订阅者收到消息
topic.publish('who', 'Tom');
// 没有订阅者了
topic.publish('who', 'Tom');

unsubscribe

取消订阅事件

const handle = topic.subscribe('who', (name) => {
  console.log('Hello ' + name);
});

// 有一个订阅者收到消息
topic.publish('who', 'Tom');
// 有一个订阅者收到消息
topic.publish('who', 'John');

handle.unsubscribe();
// 没有订阅者了
topic.publish('who', 'Tom');

keep + release

持续发布事件

const sub1 = topic.subscribe('who', (name) => {
  console.log('Hello ' + name);
});

// sub1 收到消息
const handle = topic.keep('who', true, 'Tom');

// sub2 立即收到消息
const sub2 = topic.subscribe('who', (name) => {});

// sub3 立即收到消息
const sub3 = topic.subscribe('who', (name) => {});

handle.release();

// sub3 没有收到消息
const sub3 = topic.subscribe('who', (name) => {});

使用 TS 泛型自定义事件

实例后的 topic 都是互不干扰的,适用于全局业务或者局部业务

import { Topic } from 'topic';

const customTopic = new Topic<{
  foo: [name: string];
  bar: [name: string, age: number];
  // 如果你想传入更多不可控的事件名称,则取消下面这行注释
  // [more: string]: any[];
}>();

// 现在编辑器能提示出name的类型了
customTopic.subscribe('foo', (name) => {});
// 现在编辑器知道第二个参数应该传什么类型了
customTopic.publish('foo', 'Tom');