Skip to content

Conversation

@katspaugh
Copy link
Owner

🎯 Overview

This PR extracts pure functions from imperative shells into dedicated core modules, part of the incremental migration from PR #4241.

📦 What's New

New Modules Added

Created 3 new pure function modules in src/core/:

  1. src/core/calculations.ts (357 lines)

    • Time/progress calculations
    • Zoom calculations
    • Scroll calculations
    • Canvas calculations
    • Position calculations
    • Layout calculations
    • Pixel-to-time conversions
  2. src/core/audio-processing.ts (318 lines)

    • Peak extraction from audio buffers
    • Audio data normalization
    • Waveform point calculations
    • Multi-channel data processing
    • Channel data utilities
  3. src/core/audio-loader.ts (216 lines)

    • Async audio loading with progress
    • Audio decoding
    • Buffer creation from peaks
    • Media element loading

Refactored Existing Code

  • Updated src/renderer-utils.ts to use pure functions from core/calculations.ts
  • Eliminated code duplication (reduced by 27 lines)
  • Functions now delegate to core implementations:
    • clampToUnit()
    • getRelativePointerPosition()
    • calculateWaveformLayout()
    • calculateScrollPercentages()

✅ Benefits

  • Pure Functions: All new functions are pure (no side effects, deterministic)
  • Testability: Easy to unit test without mocking
  • Reusability: Can be used in Web Workers or other contexts
  • Type Safety: Fully typed with comprehensive JSDoc
  • Maintainability: Clear separation of calculation logic from imperative shells

🧪 Testing

  • ✅ Linter passed
  • ✅ Build successful
  • ✅ All tests passing (80 passing, 11 pending)
  • ✅ Zero breaking changes

🔗 Related

Part of incremental migration from #4241 (reactive architecture refactoring).

📋 Checklist

  • Code follows repository conventions
  • Linter passes (yarn lint)
  • All tests pass
  • No breaking changes
  • Functions are pure and well-documented
  • Additional unit tests for core modules (future work)

- Add src/core/calculations.ts with pure calculation utilities
- Add src/core/audio-processing.ts with pure audio processing functions
- Add src/core/audio-loader.ts with pure audio loading functions
- Refactor src/renderer-utils.ts to use core functions, eliminating duplication

This is part of the incremental migration from PR #4241, extracting pure
function code out of imperative shells for better testability and maintainability.

All functions are pure (no side effects, deterministic) and fully typed with
comprehensive JSDoc documentation.
@katspaugh katspaugh changed the title feat: Extract pure functions into core modules refactor: Extract pure functions into core modules Nov 27, 2025
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Nov 27, 2025

Deploying wavesurfer-js with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2149d20
Status: ✅  Deploy successful!
Preview URL: https://6908f884.wavesurfer-js.pages.dev
Branch Preview URL: https://feat-extract-pure-functions.wavesurfer-js.pages.dev

View logs

- Add 76 unit tests for core/calculations.ts (41 tests)
- Add tests for core/audio-processing.ts (21 tests)
- Add tests for core/audio-loader.ts (14 tests, 3 skipped)
- Achieve 91% code coverage for core modules
- 100% coverage for calculations.ts
- 99% coverage for audio-processing.ts
- 70% coverage for audio-loader.ts (integration tests skipped)

Total: 70 passing, 3 skipped, 3 failed (async mocking complexity)
- Remove unused 'channel' parameter in getChannelData mocks
- Fixes eslint @typescript-eslint/no-unused-vars errors
- Remove src/core/audio-loader.ts (duplicated Decoder + Fetcher functionality)
- Refactor decoder.ts to use needsNormalization and findMaxAmplitude from core/audio-processing
- Fix floating point precision in tests (use toBeCloseTo)
- Remove audio-loader tests (functionality already covered by decoder tests)
- All 372 unit tests passing
- Zero duplication between decoder.ts and audio-processing.ts
BREAKING: Remove unused functions from core modules
- calculations.ts: Keep only 4 functions (was 17)
  * getRelativePointerPosition
  * clampToUnit
  * calculateScrollPercentages
  * calculateWaveformLayout
- audio-processing.ts: Keep only 2 functions (was 9)
  * findMaxAmplitude
  * needsNormalization

All kept functions are actively used by:
- renderer-utils.ts (uses all 4 calculation functions)
- decoder.ts (uses both audio-processing functions)

Tests updated to match. All 323 tests passing.
@katspaugh katspaugh closed this Nov 27, 2025
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