Skip to content

Dynamic branching by callback function

Seongnoh Sean Yi edited this page Aug 9, 2024 · 9 revisions

case assumption

You want to play 2 scenarios by time condition.

  1. Workingday mode: SceneA -> SceneB -> SceneC -> SceneA -> SceneB, ...
  2. Holiday mode: SceneA -> SceneD -> SceneE -> SceneA -> SceneD, ...

This could be achieved by collaborating with MMM-ModuleScheduler or similar modules that can fire notifications according to a time schedule.

However, you can also make this conditional branching with this module alone.

scenario: [
  { 
    name: "SceneA", 
    next: () => {
      const now = new Date()
      const isWeekend = now.getDay() === 0 || now.getDay() === 6 // Saturday or Sunday
      const isSpecificHours = now.getHours() > 7 && now.getHours() < 9 // Between 7 o'clock and 9 o'clock
      window.SceneMode = (isWeekend && isSpecificHours) ? "Special" : "Normal"

      return (window.SceneMode === "Special") ? "SceneD" : "SceneB" // If it's a special scene, go to SceneD, otherwise go to SceneB
    },
    previous: () => {
      return (window.SceneMode === "Special") ? "SceneE" : "SceneC" // If it's a special scene, go to SceneE, otherwise go to SceneC
    }
  },
  { name: "SceneB", }, // 2nd scene in the Normal scenario
  { name: "SceneC", next: "SceneA" }, // last scene in the Normal scenario
  { name: "ScneeD", }, // 2nd scene in the Special scenario
  { name: "SceneE", next: "SceneA" }, // last scene in the Special scenario
],

Clone this wiki locally