Skip to content

Commit

Permalink
1.0.5 more async methods
Browse files Browse the repository at this point in the history
  • Loading branch information
julianschiavo committed Dec 20, 2022
1 parent d170f83 commit dcff6ef
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# 📩 Loadability
### Powerful, modern networking and caching with SwiftUI support
#### v1.0.5

**This package has changed since the documentation was updated to support newer language features. To support older versions of operating systems, see the [deprecated branch](https://github.com/julianschiavo/Loadability/tree/deprecated).**
**This package has changed since the documentation was updated to add support for newer language features (async/await). To support older versions of operating systems, see the [deprecated branch](https://github.com/julianschiavo/Loadability/tree/deprecated).**

<br>

Expand Down
2 changes: 1 addition & 1 deletion Sources/Loadability/Networking/CachedLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public extension CachedLoader {

func refresh(key: Key) async {
guard task == nil else { return }
cancel()
await cancel()
try? await cache.removeValue(for: key)
object = nil
// await load(key: key)
Expand Down
12 changes: 6 additions & 6 deletions Sources/Loadability/Networking/Loader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ public protocol Loader: ObservableObject, ThrowsErrors {
/// - Parameters:
/// - key: The key identifying the object that was loaded.
/// - object: The loaded object.
func loadCompleted(key: Key, object: Object)
func loadCompleted(key: Key, object: Object) async

/// Cancels the current loading operation.
func cancel()
func cancel() async
}

public extension Loader {
@discardableResult func load(key: Key) async -> Object? {
let task = Task { () -> Object in
let object = try await loadData(key: key)
self.object = object
loadCompleted(key: key, object: object)
await loadCompleted(key: key, object: object)
return object
}
self.task = task
Expand All @@ -71,7 +71,7 @@ public extension Loader {

func refresh(key: Key) async {
guard task == nil else { return }
cancel()
await cancel()
object = nil
await load(key: key)
}
Expand Down Expand Up @@ -103,12 +103,12 @@ public extension Loader {
return nil
}

func loadCompleted(key: Key, object: Object) {
func loadCompleted(key: Key, object: Object) async {
// Default implementation does nothing. This is used by the more advanced loaders to allow for inserting cache events.
}

/// Cancels the ongoing load.
func cancel() {
func cancel() async {
task?.cancel()
task = nil

Expand Down
4 changes: 2 additions & 2 deletions Sources/Loadability/Networking/SimpleNetworkLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Foundation
public protocol SimpleNetworkLoader: Loader {
/// Creates a `URLRequest` for a network loading request.
/// - Parameter key: The key identifying the object to load.
func createRequest(for key: Key) -> URLRequest
func createRequest(for key: Key) async -> URLRequest

/// Decodes data received from a network request into the object.
/// - Parameters:
Expand All @@ -16,7 +16,7 @@ public protocol SimpleNetworkLoader: Loader {

public extension SimpleNetworkLoader {
func loadData(key: Key) async throws -> Object {
let request = createRequest(for: key)
let request = await createRequest(for: key)
let (data, _) = try await URLSession.shared.data(for: request)
return try await self.decode(data, key: key)
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/Loadability/UI/Load.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public struct Load<Loader: SomeLoader, Value, Content: View, PlaceholderContent:
await loader.load(key: key)
}
.onDisappear {
loader.cancel()
Task {
await loader.cancel()
}
}
}

Expand Down

0 comments on commit dcff6ef

Please sign in to comment.