Skip to content

heibor/lightMsgHandler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 

Repository files navigation

Name

lightMsgHandler - A micro message handler used in the context of JavaScript.

Description

A lightly JavaScript handler used for register the message handler or send message to its handler. It can decouple your systems to some extent.

Synopsis

    function msg1Handler(p1, p2) {
        console.log('this is handler for message1', p1, p2);
    }

    lightMsgHandler.registerMsgHandler('msg1', msg1Handler);
    lightMsgHandler.sendMsg('msg1', 'p1', 'p2');
    lightMsgHandler.removeMsgHander('msg1', msg1Handler);

Methods

    /**
     * @param {String} msg the message name
     * @param {Function} handler the message handler
     * @param {Object} [caller] this object for the message handler
     * @param {Number} [pri] the message primary level
     */
    lightMsgHandler.registerMsgHandler(msg, handler, caller, pri);

This method register a function as the message handler for a message. A message can have many handlers, so you can register many different handlers for a message. These handlers will be called in the order of their privileges. If not specify a handler’s privilege, its default value will be set to 0.

    /**
     * @param {String} [msg] the message name
     */
    lightMsgHandler.sendMsg(msg [, ]);

This method will send a message, all handlers of the message will be called in the order of their privileges. You can put other additional parameters at the end of ‘msg’, all parameters will be passed to these handlers.

    /**
     * @param {String} [msg] the message name
     * @param {Function} [handler] the message handler
     */
    lightMsgHandler.unregisterMsgHandler(msg, handler);

This method will remove a handler for a message. If the handler was not registered before, this method will do nothing. When you do not want to a handler to handle the message, you can call this method to remove the handler. When your lifetime for your handler has gone over, you should call this method.

About

A light message handler writen with JavaScript.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published