Traffic Light State Machine
State machines are a powerful tool for modeling systems with distinct states and transitions between them. This challenge asks you to implement a traffic light state machine in JavaScript, simulating the sequence of colors and their durations. This is a common pattern in software development for managing complex workflows and reactive systems.
Problem Description
You are tasked with creating a JavaScript class called TrafficLight that represents a traffic light. The traffic light should have three states: "red", "yellow", and "green". Each state should have a defined duration. The state machine should automatically transition between states after the specified duration.
Key Requirements:
- States: The traffic light must have three states: "red", "yellow", and "green".
- Durations: Each state should have a configurable duration in milliseconds.
- Automatic Transitions: The state machine should automatically transition to the next state after the duration of the current state expires. The order of transitions should be: red -> green -> yellow -> red.
- Current State: The class should provide a method to retrieve the current state of the traffic light.
- Start/Stop: The class should have methods to start and stop the state machine. When stopped, the traffic light should remain in its current state.
- Error Handling: Handle invalid state names gracefully.
Expected Behavior:
- When
start()is called, the traffic light should begin in the "red" state and transition through the states according to the defined durations. - When
stop()is called, the traffic light should cease transitioning and remain in its current state. - The
getCurrentState()method should return the current state as a string ("red", "yellow", or "green"). - Invalid state names passed to the constructor should result in an error.
Edge Cases to Consider:
- What happens if the durations are zero or negative? (Consider throwing an error or defaulting to a reasonable value).
- What happens if
start()is called multiple times? (Consider resetting the timer). - What happens if
stop()is called multiple times? (No adverse effects).
Examples
Example 1:
Input:
const light = new TrafficLight({ red: 5000, yellow: 2000, green: 7000 });
light.start();
// Wait 5 seconds (red)
light.getCurrentState();
Output: "green"
Explanation: After 5 seconds, the light transitions to green.
Example 2:
Input:
const light = new TrafficLight({ red: 3000, yellow: 1000, green: 4000 });
light.start();
// Wait 2 seconds
light.stop();
light.getCurrentState();
Output: "yellow"
Explanation: The light transitions to yellow, and then is stopped before completing the yellow duration.
Example 3: (Edge Case)
Input:
const light = new TrafficLight({ red: -1000, yellow: 2000, green: 3000 });
Output: Error: Durations must be positive numbers.
Explanation: Negative durations are invalid and should be handled.
Constraints
- Durations: All durations must be positive numbers (in milliseconds).
- State Names: State names must be strings: "red", "yellow", or "green".
- Performance: The state machine should be efficient and not consume excessive resources. Avoid unnecessary computations.
- Error Handling: The constructor should throw an error if invalid state names or durations are provided.
Notes
- Consider using
setIntervalorsetTimeoutto manage the timing of state transitions. - Think about how to encapsulate the state machine logic within the
TrafficLightclass. - A clean and well-documented code structure is highly encouraged.
- Focus on creating a robust and reliable state machine implementation.