Giter VIP home page Giter VIP logo

triptrap's Introduction

tripTrap

轻,但很实用的事件发布订阅器

安装

npm i triptrap

使用

  • 第一种方式,使用class 实例化
const Trapper = require('triptrap')
const trapper=new Trapper()
// 常规定义
trapper.trap('foo',(...args)=>{
    console.log('我是常规监听器',args)
})
trapper.trip('foo','bar')
// 正则匹配
trapper.trap(/system\..+/,(event)=>{
    console.log('我是系统事件监听器',event)
})
trapper.trip('system.login',{type:'online'})
// 使用filter
// 定义过滤器
const user=(...userIds)=>{
    return (eventName,userInfo)=>{
        return userIds.includes(userInfo.user_id)
    }
}
// 订阅user_id为foo或bar的事件
trapper.trap(user('foo','bar'),(userInfo)=>{
    console.log('你是foo还是bar',userInfo)
})
// 发布私聊事件
trapper.trip('message.private',{user_id:'foo',user_name:'小菊'})
  • 第二种方式,使用defineTripTrap
const {defineTripTrap} = require('triptrap')
const {trip,trap}=defineTripTrap

// 常规定义
trap('foo',(...args)=>{
    console.log('我是常规监听器',args)
})
trip('foo','bar')
// 正则匹配
trap(/system\..+/,(event)=>{
    console.log('我是系统事件监听器',event)
})
trip('system.login',{type:'online'})
// 使用filter
// 定义过滤器
const user=(...userIds)=>{
    return (eventName,userInfo)=>{
        return userIds.includes(userInfo.user_id)
    }
}
// 订阅user_id为foo或bar的事件
trap(user('foo','bar'),(userInfo)=>{
    console.log('你是foo还是bar',userInfo)
})
// 发布私聊事件
trip('message.private',{user_id:'foo',user_name:'小菊'})

如何取消监听?

1. 通过返回函数取消监听

const Trapper = require('triptrap')
const trapper=new Trapper()
// 常规定义
const dispose=trapper.trap('foo',(...args)=>{
    console.log('我是常规监听器',args)
})
// 在不需要时,调用返回函数,即可取消监听
dispose()

2. 通过offTrap取消监听

const Trapper = require('triptrap')
const trapper=new Trapper()
// 常规定义
const listener=(...args)=>{
    console.log('我是常规监听器',args)
}
trapper.trap('foo',listener)
// 在不需要时,使用offTrap取消监听
trapper.offTrap('foo',listener)

triptrap's People

Contributors

14kay avatar lc-cn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.