|
| 1 | +import { |
| 2 | + createPomodoroState, |
| 3 | + switchMode, |
| 4 | + tickTimer, |
| 5 | + getDurationForMode, |
| 6 | + formatTime, |
| 7 | +} from './core/timer.js'; |
| 8 | +import { updateModeButtons } from './ui/render.js'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Initializes the Pomodoro application by setting up the UI, state, and event listeners. |
| 12 | + * This function orchestrates the entire application, from DOM element selection to |
| 13 | + * timer management and task handling. It is designed to be self-contained and |
| 14 | + * can be safely run in both browser and server-side environments for testing. |
| 15 | + * |
| 16 | + * @param {Document} [doc=globalThis.document] - The document object to interact with, |
| 17 | + * allowing for dependency injection in non-browser environments. |
| 18 | + */ |
| 19 | +export function initializePomodoroApp( |
| 20 | + doc = globalThis.document === undefined ? null : globalThis.document |
| 21 | +) { |
| 22 | + // Abort initialization if the document or its core methods are unavailable. |
| 23 | + if (!doc?.getElementById) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + // --- DOM Element Selection --- |
| 28 | + // Retrieve all necessary DOM elements for the application to function. |
| 29 | + // If any critical element is missing, the function will exit early. |
| 30 | + const timerDisplay = doc.getElementById('timer-display'); |
| 31 | + const modeButtons = doc.querySelectorAll('[data-mode]'); |
| 32 | + const startButton = doc.getElementById('start-btn'); |
| 33 | + const pauseButton = doc.getElementById('pause-btn'); |
| 34 | + const resetButton = doc.getElementById('reset-btn'); |
| 35 | + const iterationCount = doc.getElementById('iteration-count'); |
| 36 | + |
| 37 | + // Ensure all critical UI components are present before proceeding. |
| 38 | + if ( |
| 39 | + !timerDisplay || |
| 40 | + !startButton || |
| 41 | + !pauseButton || |
| 42 | + !resetButton || |
| 43 | + !iterationCount |
| 44 | + ) { |
| 45 | + console.error('A critical UI element is missing from the DOM.'); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // --- State and Interval Management --- |
| 50 | + let state = createPomodoroState(); |
| 51 | + let intervalId = null; |
| 52 | + |
| 53 | + /** |
| 54 | + * Stops the main timer interval, preventing further ticks. |
| 55 | + * This function clears the active interval and updates the state to reflect |
| 56 | + * that the timer is no longer running. |
| 57 | + */ |
| 58 | + function stopTimer() { |
| 59 | + if (intervalId) { |
| 60 | + clearInterval(intervalId); |
| 61 | + intervalId = null; |
| 62 | + } |
| 63 | + state = { ...state, isRunning: false }; |
| 64 | + } |
| 65 | + |
| 66 | + // ========= START Live Coding: Render Pipeline ========= |
| 67 | + /** |
| 68 | + * Main render pipeline. |
| 69 | + * This function synchronizes the entire UI with the current application state. |
| 70 | + * It updates the timer display, pomodoro completion count, and task list. |
| 71 | + * It also ensures that UI elements like mode buttons reflect the current state. |
| 72 | + */ |
| 73 | + function render() { |
| 74 | + timerDisplay.textContent = formatTime(state.remainingSeconds); |
| 75 | + iterationCount.textContent = `${state.completedPomodoros}`; |
| 76 | + // Update the visual state of mode selection buttons. |
| 77 | + updateModeButtons(modeButtons, state.mode); |
| 78 | + } |
| 79 | + // ========= END Live Coding ========= |
| 80 | + |
| 81 | + // ========= START Live Coding: Timer Controls (Start/Pause/Reset) ========= |
| 82 | + /** |
| 83 | + * Starts the timer loop. |
| 84 | + * If the timer is not already running, it sets up a `setInterval` to tick |
| 85 | + * every second. On each tick, it updates the state and re-renders the UI. |
| 86 | + * If a pomodoro cycle completes, it automatically stops the timer. |
| 87 | + */ |
| 88 | + function startTimer() { |
| 89 | + if (intervalId) { |
| 90 | + return; // Prevent multiple intervals from running simultaneously. |
| 91 | + } |
| 92 | + state = { ...state, isRunning: true }; |
| 93 | + intervalId = setInterval(() => { |
| 94 | + const { nextState, completedCycle } = tickTimer(state); |
| 95 | + state = nextState; |
| 96 | + render(); // Update UI every second. |
| 97 | + |
| 98 | + // Stop the timer if the session (e.g., focus, break) has ended. |
| 99 | + if (completedCycle) { |
| 100 | + stopTimer(); |
| 101 | + } |
| 102 | + }, 1000); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Pauses the timer by stopping the interval. |
| 107 | + */ |
| 108 | + function pauseTimer() { |
| 109 | + stopTimer(); |
| 110 | + render(); // Ensure UI reflects the paused state. |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Resets the timer to the initial state for the 'focus' mode. |
| 115 | + * It stops any active timer and restores the default duration. |
| 116 | + */ |
| 117 | + function resetTimer() { |
| 118 | + stopTimer(); |
| 119 | + state = switchMode( |
| 120 | + { ...state, remainingSeconds: getDurationForMode('focus') }, |
| 121 | + 'focus' |
| 122 | + ); |
| 123 | + render(); |
| 124 | + } |
| 125 | + |
| 126 | + // --- Event Listener Attachments --- |
| 127 | + |
| 128 | + // Attach core timer controls. |
| 129 | + startButton.addEventListener('click', startTimer); |
| 130 | + pauseButton.addEventListener('click', pauseTimer); |
| 131 | + resetButton.addEventListener('click', resetTimer); |
| 132 | + // ========= END Live Coding ========= |
| 133 | + |
| 134 | + // ========= START Live Coding: Mode Switching Events ========= |
| 135 | + // Attach listeners for mode-switching buttons (Focus, Short Break, Long Break). |
| 136 | + for (const button of modeButtons) { |
| 137 | + button.addEventListener('click', () => { |
| 138 | + stopTimer(); |
| 139 | + const newMode = button.dataset.mode; |
| 140 | + state = switchMode(state, newMode); |
| 141 | + render(); |
| 142 | + }); |
| 143 | + } |
| 144 | + // ========= END Live Coding ========= |
| 145 | + |
| 146 | + // --- Initial Render --- |
| 147 | + // Perform an initial render to display the default state when the app loads. |
| 148 | + render(); |
| 149 | +} |
| 150 | + |
| 151 | +/** |
| 152 | + * Expose the application initialization function to the global scope for browser environments. |
| 153 | + * This allows the application to be started from an inline script tag or developer console. |
| 154 | + * The application is automatically initialized once the DOM is fully loaded. |
| 155 | + */ |
| 156 | +if (globalThis.window !== undefined) { |
| 157 | + globalThis.PomodoroApp = { |
| 158 | + initializePomodoroApp, |
| 159 | + }; |
| 160 | + globalThis.addEventListener('DOMContentLoaded', () => initializePomodoroApp()); |
| 161 | +} |
0 commit comments