読者です 読者をやめる 読者になる 読者になる

latest log

酩酊状態で書いたエンジニアポエムです。酩酊状態で読んでください。

疎結合を加速させるWebModule Message.js を作成しました

WebModule Message.js を作成しました。いわゆるメッセージパッシングです。

これは、Postal.jsリファクタリングし、名前を変更したものです。

Postal.js で14個ほどあったメソッドが、Message.js ではわずか2個(new Message, Message#post)に集約されました。KISS ですね。

Message over WorkerThread も実装しているため、UI Thread ⇔ UI Thread と UI Thread ⇔ Worker Thread のメッセージングをシームレスかつシンプルに実装できます。

// Postal.js の API

function Postal() { // @help: Postal
                    // @desc:  Message delivery utility (Observer pattern implementation).
    this["_receiver"] = {}; // Object: receiver hash table. { id: receiver, ... }
}
Postal["repository"] = "https://github.com/uupaa/Postal.js";

Postal["prototype"] = {
    "constructor":  Postal,
    "id":           _id,        // Postal#id(receiver:ReceiverObject):ReceiverIDString
    "register":     register,   // Postal#register(receiver:ReceiverObject):this
    "unregister":   unregister, // Postal#unregister(receiver:ReceiverObject = undefined):this
    "to":           to,         // Postal#to(receiver:ReceiverObject = undefined):Envelope
    "omit":         omit,       // Postal#omit(receiver:ReceiverObject):Envelope
    "send":         send,       // Postal#send(message:String, param:Mix = undefined, ...):Object
    "post":         post        // Postal#post(message:String, param:Mix = undefined, ...):Object
};

function Envelope(hash,         // @arg receiverHashTableObject: registered receiver. { id: object, ... }
                  to) {         // @arg StringArray: delivery id. ["id", ...]
    this["_receiver"] = hash;   // Object: receiver hash table. { id: receiver, ... }
    this["_to"] = to;           // StringArray: ["id", ...]
}
Envelope["prototype"] = {
    "constructor":  Envelope,
    "to":           to,         // Envelope#to(receiver:ReceiverObject):Envelope
    "omit":         omit,       // Envelope#omit(receiver:ReceiverObject):Envelope
    "list":         list,       // Envelope#list():ReceiverIDStringArray
    "send":         send,       // Envelope#send(message:String, param:Mix = undefined, ...):Object
    "post":         post        // Envelope#post(message:String, param:Mix = undefined, ...):Object
};

// Message.js の API

function Message(address,  // @arg Object - to address. { name: Object, ... }
                 method) { // @arg String = "inbox" - callback method name.
                           // @desc: MessagePassing implementation.
    this._address = address;
    this._method = method || "inbox";

//{@dev
    $valid($type(address, "Object"),      Message, "address");
    $valid($type(method,  "String|omit"), Message, "method");

    for (var name in address) {
        $valid($type(address[name][this._method], "Function"), Message, "address"); // object has not method.
    }
//}@dev
}

Message["repository"] = "https://github.com/uupaa/Message.js"; // GitHub repository URL. http://git.io/Help
Message["prototype"] = {
    "constructor":  Message,        // new Message(address:Object, method:String = "inbox")
    "post":         Message_post    // Message#post(data:Object, callback:Function = null):void
};