-
-
Notifications
You must be signed in to change notification settings - Fork 368
Concurrency
maxence-charriere edited this page Jun 28, 2020
·
4 revisions
go-app and all event handlers registered from HTML elements are executed on the main goroutine (Also refered as the UI goroutine).
Apps often require to perform requests to an API. Those requests are blocking operation and can take seconds.
Performing blocking operations directly in event handlers may result in unresponsiveness in the UI. Those should be performed in another goroutine.
func (c *myCompo) OnClick(ctx app.Context, e app.Event) {
go func() {
// Blocking operations...
}()
}
Once you performed a blocking operation on another goroutine. The thing you want to do next is to update components back on the main goroutine.
This is done by calling the Dispatch function.
func (c *myCompo) OnClick(s app.Value, e app.Event) {
go func() {
// Blocking operations...
app.Dispatch(func() {
// Update component fields...
c.Update()
})
}()
}