Giter Club home page Giter Club logo

event-bus-ts's Introduction

event-bus-ts

🍰 不到1k, 一个mini的事件管理器, 希望能在您的代码中做一块砖.

方法

on(eventName, listener)

绑定事件

名称 类型 数据类型 是否必填 说明
eventName 参数 String/Symbol 事件名称
listener 参数 Function 对应的回调函数
emitter 返回值 EventEmitter --- 实例

off(eventName, listener)

解除绑定, 如果不填写listener, 那么eventName对应的listener都会被移除.

名称 类型 数据类型 是否必填 说明
eventName 参数 String/Symbol 事件名称
listener 参数 Function 对应的回调函数
emitter 返回值 EventEmitter --- 实例
const callback = data=>{
    alert(data)
};
emitter.on('add', callback);
// 解除绑定
emitter.off('add', callback);
// add事件不会触发
emitter.emit('add', 1);

once(eventName, listener)

绑定事件, 只触发一次

名称 类型 数据类型 是否必填 说明
eventName 参数 String/Symbol 事件名称
listener 参数 Function 对应的回调函数
emitter 返回值 EventEmitter --- 实例
const callback = data=>{
    alert(data)
};
emitter.once('add', callback);
// add事件触发
emitter.emit('add', 1);
// add事件不会触发
emitter.emit('add', 1);

emit(eventName [, ...args])

触发事件, 支持任意数量参数

名称 类型 数据类型 是否必填 说明
eventName 参数 String/Symbol 事件名称
...args 参数 Any 对应的回调函数
emitter 返回值 Boolean --- 实例
const callback = (a,b,c,d)=>{
    console(a,b,c,d); // 1,2,3,4
};
emitter.once('add', callback);
// add事件触发
emitter.emit('add', 1,2,3,4);

has(eventName)

是否绑定了指定事件

名称 类型 数据类型 是否必填 说明
eventName 参数 String/Symbol 事件名称
emitter.on('add', callback);

emitter.has('add'; // true

destroy()

销毁实例

const callback = (a,b,c,d)=>{
    console(a,b,c,d); // 1,2,3,4
};
emitter.once('add', callback);
emitter.destroy();

// add事件不会触发
emitter.emit('add', 1,2,3,4);

event-bus-ts's People

Contributors

okbexx avatar

Watchers

James Cloos avatar  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.