Skip to content

Dynamic branching by callback function

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

case assumption

Since 1.1.0

You want to play 2 scenarios by time condition.

  1. Normal mode (usual working days): SceneA -> SceneB -> SceneC -> SceneA -> SceneB, ...
  2. Special mode (Sat/Sunday morning): 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 mode, go forward to SceneD, otherwise to SceneB
    },
    previous: () => {
      return (window.SceneMode === "Special") ? "SceneE" : "SceneC" // If it's a special mode, go backward to SceneE, otherwise to SceneC
    },
    ...
  },
  { name: "SceneB", ... }, // 2nd scene in the Normal scenario
  { name: "SceneC", next: "SceneA", ... }, // last scene in the Normal scenario (Return to SceneA)
  { name: "ScneeD", ... }, // 2nd scene in the Special scenario
  { name: "SceneE", next: "SceneA", ... }, // last scene in the Special scenario (Return to SceneA)
],

Clone this wiki locally