ホーム 全セッション S06

保存後のイベントの受け渡し

8 / 20 目安 1分

ページ一覧

事前知識: port の store の成功の値は void であるため、保存の後もイベントを次の処理へ渡す andThrough を使います。

前のページの port の store の成功の値は void です。このため、store を andThen で連結すると、保存の後の map へ、保存したイベントを渡せません。最後の map は event.aggregateState を取り出すため、保存の後もイベントが必要です。図の startExaminationEvent は ExaminationStarted を生成する処理です。startExaminationEvent と store の実装は、後のページで確認します。

  1. andThen で実装すると: andThen は、store が返した値を次の行へ渡します。store が返す値は void であるため、次の行へ渡されるのは、保存したイベントではなく void です。

  2. andThen で実装すると: 次の行の event は void です。void に aggregateState はないため、型検査のときに報告されます。

  3. andThrough で実装すると: andThrough は、store の成功を確認したあと、その結果を破棄します。次の行へは、保存したイベントがそのまま渡されます。

  4. andThrough で実装すると: 次の行の event は ExaminationStarted です。event.aggregateState を取り出して、画面に新しい予約の状態 InExamination を返せます。保存の後もイベントが必要なのは、このためです。

andThen で実装すると andThrough で実装すると 右の札は、その行から次の行へ渡される値です。✗ の札は値ではなく、型のエラーです。 .andThen の行は説明用(教材のコードにはありません)。実物: examples/session-06-solution/src/useCase/startExamination.ts:88-90 examples/session-06-solution/src/useCase/startExamination.ts:88-90
.map(startExaminationEvent(deps, input.veterinarianId))
ExaminationStarted
.andThen(deps.examinationStartedStore.store)
.andThrough(deps.examinationStartedStore.store)
void(値なし) ExaminationStarted
.map((event) => event.aggregateState);
✗ 型のエラー void に aggregateState はありません InExamination

andThrough は S05 で説明した関数です。渡した処理が Ok を返すと元の値を次へ渡し、Err を返すとその Err を結果にします。