Skip to content

fix: Currency API fetch works in Web Worker context (fixes #18)#41

Merged
konard merged 3 commits intomainfrom
issue-18-4e281bffdafb
Jan 26, 2026
Merged

fix: Currency API fetch works in Web Worker context (fixes #18)#41
konard merged 3 commits intomainfrom
issue-18-4e281bffdafb

Conversation

@konard
Copy link
Contributor

@konard konard commented Jan 26, 2026

Summary

This PR fixes the root cause of issue #18 where exchange rates were showing as "hardcoded" with "unknown" date instead of using real-time API data.

Root Cause Analysis

The currency exchange rate API fetch was silently failing because web_sys::window() returns None in Web Worker contexts. Web Workers don't have a window object - they use WorkerGlobalScope instead.

Before (broken):

let window = web_sys::window()
    .ok_or_else(|| CurrencyApiError::NetworkError("No window object available"))?;
// Always fails in Web Worker!

After (fixed):

let global = js_sys::global();
if let Some(window) = global.dyn_ref::<web_sys::Window>() {
    // Window context
} else if let Some(worker) = global.dyn_ref::<web_sys::WorkerGlobalScope>() {
    // Web Worker context
}

Changes

  1. src/currency_api.rs: Use js_sys::global() to detect and use either Window or WorkerGlobalScope context for fetch operations
  2. Cargo.toml: Add WorkerGlobalScope feature to web-sys dependency
  3. docs/case-studies/issue-18/README.md: Update documentation with root cause analysis
  4. changelog.d/: Add changelog fragment for the fix

Test Plan

  • cargo fmt --check passes
  • cargo clippy passes
  • cargo test passes (all 66+ tests)
  • Currency API fetch should now work in the Web Worker
  • Live site shows real exchange rates from API (pending CI/deploy)

Related


🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #18
@konard konard self-assigned this Jan 26, 2026
The currency exchange rate API fetch was silently failing because
`web_sys::window()` returns `None` in Web Worker contexts. Workers
don't have a `window` object - they use `WorkerGlobalScope` instead.

Changes:
- Use `js_sys::global()` to detect the execution context
- Support both `Window` and `WorkerGlobalScope` for fetch operations
- Add `WorkerGlobalScope` feature to web-sys dependency
- Update case study documentation with root cause analysis

This is the actual root cause of issue #18 - the previous fix in PR #38
added the rate application code but the API fetch was never working.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@konard konard changed the title [WIP] Проблема с выражением: 0 RUB + 1 USD fix: Currency API fetch works in Web Worker context (fixes #18) Jan 26, 2026
@konard konard marked this pull request as ready for review January 26, 2026 13:07
@konard
Copy link
Contributor Author

konard commented Jan 26, 2026

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $7.051922 USD
  • Calculated by Anthropic: $5.340953 USD
  • Difference: $-1.710970 (-24.26%)
    📎 Log file uploaded as Gist (976KB)
    🔗 View complete solution draft log

Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard konard merged commit d2c78ec into main Jan 26, 2026
12 checks passed
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.

Проблема с выражением: 0 RUB + 1 USD

1 participant