Files

51 lines
1.6 KiB
Markdown
Raw Permalink Normal View History

2023-09-24 08:57:22 +10:00
# Main Thread Functions
2023-09-25 20:56:29 +10:00
These methods are utility functions to run code on the main thread. This is
required when you want to run custom code on the UI thread.
2023-09-24 08:57:22 +10:00
### InvokeSync
API: `InvokeSync(fn func())`
2023-09-25 20:56:29 +10:00
This function runs the passed function (`fn`) synchronously. It uses a WaitGroup
(`wg`) to ensure that the main thread waits for the `fn` function to finish
before it continues. If a panic occurs inside `fn`, it will be passed to the
handler function `PanicHandler`, defined in the application options.
2023-09-24 08:57:22 +10:00
### InvokeSyncWithResult
API: `InvokeSyncWithResult[T any](fn func() T) (res T)`
2023-09-25 20:56:29 +10:00
This function works similarly to `InvokeSync(fn func())`, however, it yields a
result. Use this for calling any function with a single return.
2023-09-24 08:57:22 +10:00
### InvokeSyncWithError
API: `InvokeSyncWithError(fn func() error) (err error)`
2023-09-25 20:56:29 +10:00
This function runs `fn` synchronously and returns any error that `fn` produces.
Note that this function will recover from a panic if one occurs during `fn`'s
execution.
2023-09-24 08:57:22 +10:00
### InvokeSyncWithResultAndError
2023-09-25 20:56:29 +10:00
API:
`InvokeSyncWithResultAndError[T any](fn func() (T, error)) (res T, err error)`
2023-09-24 08:57:22 +10:00
2023-09-25 20:56:29 +10:00
This function runs `fn` synchronously and returns both a result of type `T` and
an error.
2023-09-24 08:57:22 +10:00
### InvokeAsync
API: `InvokeAsync(fn func())`
2023-09-25 20:56:29 +10:00
This function runs `fn` asynchronously. It runs the given function on the main
thread. If a panic occurs inside `fn`, it will be passed to the handler function
`PanicHandler`, defined in the application options.
2023-09-24 08:57:22 +10:00
---
2023-09-25 20:56:29 +10:00
_Note_: These functions will block execution until `fn` has finished. It's
critical to ensure that `fn` doesn't block. If you need to run a function that
blocks, use `InvokeAsync` instead.