ProtoViewLogic: post()
受け取ったメッセージを処理します。
この関数は非同期関数です。
構文
protoViewLogic.post(msg) => Promise<boolean>
引数
-
msg
:object
処理するメッセージオブジェクトです。通常は
message()
で得られるメッセージと同じ意味のものです。
返値: Promise<boolean>
依頼されたメッセージ処理が消費されたかどうかを返すプロミスです。true
ならメッセージが処理されたことを示し、false
なら未処理であることを示します。
例外
TypeError
- 引数
msg
が非null
のオブジェクトでなかった場合。
- 引数
例
const sender = new class Sender extends ProtoViewLogic {
async messageHandler(msg) {
if (msg.id === "greet") {
console.log("Do-mo!");
return msg.origin.post(this.message("handshake", null, null));
}
if (msg.id === "handshake") {
console.log("Nice to meet you, too.");
return true;
}
return false;
}
};
const recipient = new class Recipient extends ProtoViewLogic {
async messageHandler(msg) {
if (msg.id === "greet") {
console.log("Cześć!");
return msg.origin.post(this.message("greet", null, null));
}
if (msg.id === "handshake") {
console.log("Nice to meet you.");
return msg.origin.post(this.message("handshake", null, null));
}
return false;
}
};
recipient.post(sender.message("greet", null, null));
// ==> 以下の順にログが表示される:
// "Cześć!"
// "Do-mo!"
// "Nice to meet you."
// "Nice to meet you, too."
解説
post()
を呼び出すと対象の ProtoViewLogic
の messageHandler()
が呼び出されます。
メッセージは、messageHandler()
で処理されなかった場合に親がいれば、つまりプロパティ parent
が null
でなかったら、親の post()
を呼び出します。この呼び出しは messageHandler()
で処理されるか親がいなくなるまで繰り返されます。