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

ProtoViewLogic: post()

受け取ったメッセージを処理します。

この関数は非同期関数です。

構文

protoViewLogic.post(message) => Promise<boolean>
protoViewLogic.post(message, options) => Promise<boolean>

引数

  • message: object

    送信するメッセージです。 このオブジェクトは以下のプロパティを持ちます:

    • id: string | null

      メッセージの識別子です。

    • code: string | null

      メッセージの追加の識別子です。

    • param: any | null

      メッセージが持つパラメータです。

    • origin: object | null

      メッセージの生成元です。

  • oprions: object (省略可)

    送信するメッセージに関するオプションです。 このオブジェクトは以下のプロパティを持ちます:

    • discardDuplicates: boolean

      メッセージの滞留制御のフラグです。 true の場合、 idcode が同一の処理中のメッセージがある場合は message を受け取りません。 デフォルトは false です。

返値: Promise<boolean>

送信したメッセージが消費されたかどうかを解決時に示すプロミスです。 true として解決された場合、メッセージが消費されたことを示し、false なら消費されなかったことを示します。

例外

  • TypeError
    • 引数 message が非 null のオブジェクトでなかった場合。

const sender = new class Sender extends ProtoViewLogic {
    async messageHandler(message) {
        if (message.id === "greet") {
            console.log("Do-mo!");
            return message.origin.post(this.message("handshake", null, null));
        }
        if (message.id === "handshake") {
            console.log("Nice to meet you, too.");
            return true;
        }
        return false;
    }
};
const recipient = new class Recipient extends ProtoViewLogic {
    async messageHandler(message) {
        if (message.id === "greet") {
            console.log("Cześć!");
            return message.origin.post(this.message("greet", null, null));
        }
        if (message.id === "handshake") {
            console.log("Nice to meet you.");
            return message.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."

recipient.post(sender.message("greet", null, null), { discardDuplicates: true });
// ==> "greet"メッセージが処理中のためログは表示されない

解説

post() を呼び出すと対象の ProtoViewLogicmessageHandler() が呼び出されます。

メッセージは、messageHandler() で処理されなかった場合に親がいれば、つまりプロパティ parentnull でなかったら、親の post() を呼び出します。この呼び出しは messageHandler() で処理されるか親がいなくなるまで繰り返されます。

引数 options のプロパティ discardDuplicatestrue の場合は追加のメッセージを受け取りません。idcode が同一のメッセージを処理中の場合はメッセージを受け取らず、親の post() を呼び出して伝播することもありません。結果は false が返ります。

::: note[メッセージの滞留制御について]

data-active-events 属性の設定されたエレメントが ProtoViewLogic に関連付けられると、data-active-events の値として列挙された各イベント種別について、対応するイベントリスナーがそのエレメントに設定されます。 これらのイベントリスナーは、関連付けられている ProtoViewLogic を対象に、post() 関数を呼び出します。

メッセージの滞留が抑制されるのは ProtoViewLogic に関連付けられたエレメントが duplicate-filter 属性の値に "discard" 設定されている場合です。この場合、イベントリスナーが post() 関数を呼び出す際に options のプロパティ discardDuplicatestrue が渡されます。

:::