no versions found for ApplicationFrameworkReferences / Components/View/ProtoViewLogic/protoviewlogic_message / en

ProtoViewLogic: message()

ProtoViewLogic へ送るメッセージを生成します。

構文

protoViewLogic.message(id, code, param) => object

引数

  • id: string | null

    メッセージの識別子です。HTML の id 属性などです。

  • code: string | null

    メッセージの追加の識別子です。HTML のイベント種別などです。

  • param: any

    メッセージに含めるパラメータです。

返値: object

引数と自身をまとめたメッセージオブジェクトです。

  • id: string | null
  • code: string | null
  • param: any
  • origin: this

メッセージ作成の例です。

import { ProtoViewLogic } from "/alier_sys/ProtoViewLogic.js";
const vl = new class MyProtoViewLogic extends ProtoViewLogic { /* ... */ }

vl.message("cat", null, null);
// => { id: "cat", code: null, param: null, origin: vl }
vl.message("dog", "bow", null);
// => { id: "dog", code: "bow", param: null, origin: vl }
vl.message("cat", null, { breed: "calico" });
// => { id: "cat", code: null, param: { breed: "calico" }, origin: vl }

メッセージ処理中に他のメッセージを送出する複雑な例です。

import { ProtoViewLogic } from "/alier_sys/ProtoViewLogic.js";
const flat = new class Flat extends ProtoViewLogic {
    async messageHandler(msg) {
        msg.deliver({
            tom: (msg) => {
                return msg.deliver({
                    chase: (msg) => {
                        console.log("Tom chases Jerry.");
                        return this.post(this.message("jerry", "run", null));
                    },
                    fall: (msg) => {
                        console.log("Tom falls onto the earth.");
                        return this.post(this.message("jerry", "farewell", null));
                    },
                });
            },
            jerry: (msg) => {
                return msg.deliver({
                    run: (msg) => {
                        console.log("Jerry runs away.");
                        return this.post(this.message("tom", "fall", null));
                    },
                    farewell: (msg) => {
                        console.log("Jerry casts a farewell to Tom.");
                    },
                });
            },
        });
    }
};
flat.post(flat.message("tom", "chase"));
// ==> 以下の順にログが表示される:
//  - "Tom chases ..."
//  - "Jerry runs ..."
//  - "Tom falls ..."
//  - "Jerry casts ..."

解説

生成したメッセージは post() の引数として利用される。post() の呼び出し先の ProtoViewLogic にメッセージ処理を依頼できます。