Nobody's Perfect, Man

    topic
    TypeScript icon, indicating that this package has built-in type declarations

    3.0.2 • Public • Published

    轻量级事件同步收发管理

    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');

    Install

    npm i topic

    DownloadsWeekly Downloads

    57

    Version

    3.0.2

    License

    MIT

    Unpacked Size

    29.2 kB

    Total Files

    10

    Last publish

    Collaborators

    • fwh1990