https://github.com/bannzai/async
https://github.com/bannzai/async
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/bannzai/async
- Owner: bannzai
- License: mit
- Created: 2022-12-06T13:00:49.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-06T03:47:44.000Z (over 1 year ago)
- Last Synced: 2025-03-10T16:01:39.337Z (over 1 year ago)
- Language: Swift
- Homepage:
- Size: 86.9 KB
- Stars: 66
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Async
**Async** is a easily handle async state and simple state management library for `SwiftUI`.
## Features
In the **Async** philosophy the asynchronous state is seen as divided into three parts. One is `success`. The other is `failure`. Finally, the `loading` state before execute async function.
Async is provided two way for async state management named `@Async` property wrapper and `AsyncView`. These two structures are used separately and simplify the division of the three states. They essentially have the same function, but can be used in different usecase.
## Example
1. Use `@Async`.
First, Define `@Async` with state type. The code example below refers to a `String`.
Next, `async` call as function with asynchronous function. The code example below refers to a `run` function and can chain to access async state for switching decide view for each async state.
This method also allows access to `async.state` and the use of `alert(isPresented:error:actions)` modifier with `async.error`. See `Example` for more information.
```swift
struct ContentView: View {
@Async var async
var body: some View {
switch async(run).state {
case .success(let value):
Text("\(value)")
case .failure(let error):
Text(error.localizedDescription)
case .loading:
ProgressView()
}
}
}
```
2. Use `AsyncView`.
Pass async function directly to `AsyncView` initializer, and define three states view via `when`.
```swift
struct ContentView: View {
var body: some View {
AsyncView(run, when: (
success: { Text("\($0)") },
failure: { Text($0.localizedDescription) },
loading: { ProgressView() }
))
}
}
```
## LICENSE
**Async** is released under the MIT license. See LICENSE for details.