Skip to content

Refactor main.js into modular architecture - #110

Draft
ScriptSmith with Copilot wants to merge 5 commits into
mainfrom
copilot/refactor-main-js-into-modules-again
Draft

ScriptSmith with Copilot wants to merge 5 commits into
mainfrom
copilot/refactor-main-js-into-modules-again

Conversation

Copilot AI commented Nov 12, 2025 •

Copy link
Copy Markdown
Contributor

Decomposed the monolithic 3,438-line main.js into focused modules to improve maintainability and separation of concerns.

Changes

New Modules Created

  • constants.js (312 lines) - All application constants (URLs, thresholds, colors, map styles)
  • state.js (211 lines) - Centralized state management with getter/setter API
  • worker-client.js (134 lines) - Promise-based worker communication interface
  • map.js (313 lines) - MapLibre GL operations (initialization, layers, camera controls, GeoJSON utilities)
  • ui.js (324 lines) - DOM references and UI functions (status/debug logging, stats display, UI lock/unlock)

Refactored Entry Point

  • main.js - Now imports modules via ES6 syntax, orchestrates initialization
  • index.html - Updated script tag to type="module"

Architecture

// main.js becomes lightweight orchestrator
import * as constants from './constants.js';
import * as state from './state.js';
import { initializeWorker, requestFromWorker } from './worker-client.js';
import * as mapModule from './map.js';
import { updateStatus, logDebug, lockUI, unlockUI } from './ui.js';

State access now encapsulated:

// Before: Direct global access
showVehicles = false;

// After: Via state module
state.setShowVehicles(false);

Worker requests centralized:

// Worker communication abstracted
const shapes = await requestFromWorker('routeShapes', { routeIds });

Result

  • Removed ~100 lines of duplicate constant definitions
  • Clear module boundaries with single responsibilities
  • No build tools required - pure ES6 modules
  • All existing functionality preserved

Application with modular architecture

Original prompt

Refactor main.js into modules.
state.js: A module to manage the application's state. This would hold all the "dirty flags," filter settings, and display options. Functions in other
modules would call state-updating functions here, which could in turn trigger UI updates. This centralizes state management.
map.js: All MapLibre-specific logic. It would initialize the map, add layers, update sources, and handle map-related interactions. It would read from
the state.js module to configure its display.
worker-client.js: A dedicated API client for interacting with the web worker. The requestFromWorker function would live here, simplifying communication
from other modules.
ui.js: Manages all DOM event listeners and UI updates (panels, buttons, cards). It would import functions from state.js to modify the application state
and from map.js to trigger map actions (e.g., flyTo).
main.js: Becomes a lightweight entry point that imports the other modules and orchestrates the initial setup.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 4 commits November 12, 2025 11:59
Co-authored-by: ScriptSmith <17246877+ScriptSmith@users.noreply.github.com>
…-client, map, and ui modules

Co-authored-by: ScriptSmith <17246877+ScriptSmith@users.noreply.github.com>
…tion now loads successfully

Co-authored-by: ScriptSmith <17246877+ScriptSmith@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor main.js into separate modules for better structure Refactor main.js into modular architecture Nov 12, 2025
Copilot AI requested a review from ScriptSmith November 12, 2025 12:15

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants