How chvin/react-tetris works
Project Type and Purpose
• This is a single-page client application: a React + Redux + Immutable.js implementation of the classic Tetris game.
• It includes build tooling (Webpack, Babel, ESLint), a minimal static file server (“server/”), and a “docs/” folder for published assets.
• There is no remote backend or database; instead state persistence uses localStorage and game-audio uses the browser’s Web Audio API.
File Structure Analysis
• Top-level build/config:
• webpack.config.js, webpack.production.config.js — build pipelines for dev and prod
• .wconfig.js, .babelrc, .eslintrc.js — linting/transpile rules
• package.json — dependencies (react, redux, immutable, webpack, etc.)
• Static assets & site:
• server/ — template index.html for serving the built app
• docs/ — generated assets published to GitHub Pages
• i18n.json — multi-language configuration
• Source code (“src/”):
• actions/ — Redux action creators
• components/ — React UI components (keyboard, matrix, guide, etc.) with dedicated styling (.less)
• containers/ — connected top-level components
• control/ — imperative game-state flows & key/touch handling
• reducers/ — Redux reducers split per feature (matrix, next, points, speed, focus, etc.)
• unit/ — utility modules (event throttling, Web Audio API handling, constants)
• store/ — store creation with redux-immutable combineReducers
Key Architectural Patterns & Design Principles
• Unidirectional data flow (Flux/Redux): UI dispatches actions → store → reducers → new immutable state → UI update
• Immutability: Redux store is an Immutable.js Map, ensuring cheap “===” comparisons for shouldComponentUpdate
• Separation of concerns:
→ UI layer (components)
→ State management (actions, reducers, store)
→ Side-effects/flow control (control/)
→ Utilities (unit/music.js for WebAudio, unit/event.js for input throttling)
• Responsive input: Keyboard and touch handlers co-exist, listening to keydown/touchstart, routing into the same action pipeline
• Persistence: store.subscribe → serialize full Redux state → localStorage → rehydration on init
• Build & Deployment: Webpack bundles JS & CSS, creates “docs/” for static hosting; separate dev server for hot reload
Main Components
A. Developer Tooling & Build Pipeline
• ESLint → Babel → Webpack (dev vs prod configs) → “docs/” output
• webpack-dev-server for local development
B. Static File Server
• server/index.html (template)
• serves JS/CSS bundle + assets
C. Client Application (Browser)
1. UI Layer (React Components)
– Presentational components under src/components (matrix display, keyboard, score, controls)
– Container components under src/containers connect to store via react-redux
2. State Management (Redux + Immutable.js)
– src/store/index.js creates store with redux-immutable combineReducers
– src/actions …… dispatch actions (e.g., MOVE_LEFT, DROP)
– src/reducers …… update slices of state immutably
– store.subscribe → localStorage persistence
3. Side-Effects & Utilities
– src/control/ for orchestrating multi-step state changes or timed loops (game loop, speed control)
– src/unit/event.js for input throttling (differentiated horizontal vs vertical rate)
– src/unit/music.js uses Web Audio API to load/play audio with low latency
4. Browser APIs & External Layers
– localStorage (state persistence)
– Web Audio API (AudioContext, bufferSource nodes)
– Visibility API (document.visibilitychange) → dispatch PAUSE/RESUME actions
– Touch/Mouse events → unify input handlers
Component Relationships & Data Flow
1. UI Component → dispatch(Action) → Redux Store
2. Store → pass(state) → Container (via mapStateToProps) → UI rendering (immutable props)
3. store.subscribe → serialize → localStorage
4. Control modules → dispatch sequences of actions (e.g., game start, drop timer)
5. Unit.music → Web Audio → AudioContext → Output
6. Unit.event → DOM events throttle → dispatch actions
• Unidirectional flow; separate side-effects modules off the main loop