uupaa.messagepassing.js

0.1.1 • Public • Published

MessagePassing.js Build Status

npm

Implementation of lightweight MessagePassing model.

This module made of WebModule.

Documentation

Browser, NW.js and Electron

<script src="<module-dir>/lib/WebModule.js"></script>
<script src="<module-dir>/lib/MessagePassing.js"></script>
<script>

    // ## inbox method
    function _inbox(selector, arg1, arg2) {
        switch (selector) {
        case "ping": return "pong" + arg1;
        }
        return selector.toUpperCase();
    }


    // ## SubscriberClass
    function A() {};
    A.prototype.inbox = _inbox;


    // ## SubscriberObject
    var B = { inbox: _inbox };


    // ## init MessagePassing
    var mp = new MessagePassing();
    var a = new A();
    var b = B;


    // ## regsiter subscriber and register interrested selector
    mp.register(a, "Hello").            // A is interrested in "Hello"
       register(b, ["Hello", "ping"]);  // B is interrested in "Hello" and "ping"


    // ## call A.inbox("Hello") and B.inbox("Hello")
    var result1 = mp.to(a, b).send("Hello");
    console.log( result1[0], result1[1] );       // -> "HELLO", "HELLO"


    // ## call B.inbox("ping", 123)
    mp.to(a, b).post("ping", 123, function(result2) {
        console.log(result2[0]);                 // -> "pong123";
        console.log(result2[ mp.id(b) ]);        // -> "pong123";
    });


    // ## destruct
    mp.unregisterAll();

</script>

WebWorkers

importScripts("<module-dir>/lib/WebModule.js");
importScripts("<module-dir>/lib/MessagePassing.js");

Node.js

require("<module-dir>/lib/WebModule.js");
require("<module-dir>/lib/MessagePassing.js");

Package Sidebar

Install

npm i uupaa.messagepassing.js

Weekly Downloads

8

Version

0.1.1

License

MIT

Last publish

Collaborators

  • uupaa