Async
Async copied to clipboard
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
- Use
@Async. First, Define@Asyncwith state type. The code example below refers to aString. Next,asynccall as function with asynchronous function. The code example below refers to arunfunction and can chain to access async state for switching decide view for each async state. This method also allows access toasync.stateand the use ofalert(isPresented:error:actions)modifier withasync.error. SeeExamplefor more information.
struct ContentView: View {
@Async<String, Error> 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()
}
}
}
- Use
AsyncView. Pass async function directly toAsyncViewinitializer, and define three states view viawhen.
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.