Coder Social home page Coder Social logo

message-center's Introduction

MessageCenter 一个简易的消息中心

Install and test

# install
npm install message-center.js

Example

const messageCenter = new MessageCenter()

messageCenter.on("aaa|bbb|ccc", (a,b,c) => {
  //...
})

messageCenter.emit('aaa', 0, 1, 2)

messageCenter.un('aaa')

API

on(eventName, handler, context?, weight?)

为指定事件注册一个监听器,接受一个字符串 eventName 和一个 回调函数。

messageCenter.on('aaa|bbb|ccc', () => {
  //do something
});

// 等价于
     messageCenter.on('aaa', () => {
       //do something
     });
     
     messageCenter.on('bbb', () => {
       //do something
     });
     
     messageCenter.on('ccc', () => {
       //do something
     });

once(eventName, handler, context?, weight?)

为指定事件注册一个单次监听器,监听器最多只会触发一次,触发后立刻解除该监听器。

emit(eventName, ...args)

按参数的顺序执行每个监听器

un(eventName, handler?)

解除监听器 如没有传入handler,将会解除eventName名下所有监听器

clear()

重置 messageCenter

listenersLength(eventName)

计算监听器的数量

watch(eventName, handler, ...args)

与invoke方法一起使用, 能够返回watch方法的handler可以收到invoke方法的计算结果 同时invoke也能够收到watch方法的返回结果

invoke(eventName, ...args)

    const messageCenter = new MessageCenter()
    
    messageCenter.watch("aaa", (a,b,c) => {
      // a === 0
      // b === 1
      // c === 2
      return { a: 2, b: 3, c: 4}
    })
    
    messageCenter.invoke('aaa', 0, 1, 2).then(data => {
      // data === { a: 2, b: 3, c: 4}
    })

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.