Add lofigui.Yield() for WASM-friendly cooperative scheduling #4

Open
opened 2026-08-07 14:23:34 +00:00 by hum3 · 0 comments
Owner

In WASM, runtime.Gosched() only yields between Go goroutines — it never returns control to the JavaScript event loop. This means tight goroutine loops (e.g. batch customer generation) freeze the browser UI even though they release the mutex each iteration.

time.Sleep(time.Millisecond) fixes this in WASM because it suspends the Go runtime and lets the browser run. On native builds it is unnecessary overhead.

Proposal: lofigui.Yield() that does the right thing per platform:

  • WASM (js/wasm): time.Sleep(time.Millisecond)
  • Native: runtime.Gosched()

App code in tight background loops calls lofigui.Yield() instead of choosing the mechanism directly. This keeps the browser responsive under heavy compute without penalising the native server build.

Discovered in gobank — AddCustomersBatch was calling runtime.Gosched() and the dashboard graphs froze until the batch completed.


Migrated from Codeberg: originally #4, opened 2026-03-24.

In WASM, `runtime.Gosched()` only yields between Go goroutines — it never returns control to the JavaScript event loop. This means tight goroutine loops (e.g. batch customer generation) freeze the browser UI even though they release the mutex each iteration. `time.Sleep(time.Millisecond)` fixes this in WASM because it suspends the Go runtime and lets the browser run. On native builds it is unnecessary overhead. Proposal: `lofigui.Yield()` that does the right thing per platform: - **WASM** (`js/wasm`): `time.Sleep(time.Millisecond)` - **Native**: `runtime.Gosched()` App code in tight background loops calls `lofigui.Yield()` instead of choosing the mechanism directly. This keeps the browser responsive under heavy compute without penalising the native server build. Discovered in gobank — `AddCustomersBatch` was calling `runtime.Gosched()` and the dashboard graphs froze until the batch completed. --- *Migrated from Codeberg: originally [#4](https://codeberg.org/hum3/lofigui/issues/4), opened 2026-03-24.*
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
hum3/lofigui#4
No description provided.