Skip to content

Commit

Permalink
Revert "1.7.10"
Browse files Browse the repository at this point in the history
This reverts commit f94dd2d.
  • Loading branch information
dankinsoid committed Apr 13, 2024
1 parent f94dd2d commit 4d6cd3b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "1.7.10")
.package(url: "https://github.com/dankinsoid/swift-api-client.git", from: "1.7.9")
],
targets: [
.target(
Expand Down
71 changes: 71 additions & 0 deletions Sources/SwiftAPIClient/Utils/RuntimeWarnings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import Foundation

@_transparent
@usableFromInline
@inline(__always)
func runtimeWarn(
_ message: @autoclosure () -> String,
category: String? = "swift-api-client"
) {
#if DEBUG
let message = message()
let category = category ?? "Runtime Warning"
if _XCTIsTesting {
#if canImport(XCTest)
XCTFail(message)
#endif
} else {
#if canImport(os)
os_log(
.fault,
dso: dso,
log: OSLog(subsystem: "com.apple.runtime-issues", category: category),
"%@",
message
)
#else
fputs("\(formatter.string(from: Date())) [\(category)] \(message)\n", stderr)
#endif
}
#endif
}

#if DEBUG
#if canImport(XCTest)
import XCTest
#endif

#if canImport(os)
import os

// NB: Xcode runtime warnings offer a much better experience than traditional assertions and
// breakpoints, but Apple provides no means of creating custom runtime warnings ourselves.
// To work around this, we hook into SwiftUI's runtime issue delivery mechanism, instead.
//
// Feedback filed: https://gist.github.com/stephencelis/a8d06383ed6ccde3e5ef5d1b3ad52bbc
@usableFromInline
let dso = { () -> UnsafeMutableRawPointer in
let count = _dyld_image_count()
for i in 0 ..< count {
if let name = _dyld_get_image_name(i) {
let swiftString = String(cString: name)
if swiftString.hasSuffix("/SwiftUI") {
if let header = _dyld_get_image_header(i) {
return UnsafeMutableRawPointer(mutating: UnsafeRawPointer(header))
}
}
}
}
return UnsafeMutableRawPointer(mutating: #dsohandle)
}()
#else
import Foundation

@usableFromInline
let formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:MM:SS.sssZ"
return formatter
}()
#endif
#endif
26 changes: 13 additions & 13 deletions Sources/SwiftAPIClient/Utils/WithSynchronizedAccess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public func withThrowingSynchronizedAccess<T, ID: Hashable>(
} else if let task = cached as? Task<T, Never> {
return await task.value
} else {
// runtimeWarn("Unexpected task type found in the barrier.")
runtimeWarn("Unexpected task type found in the barrier.")
}
}
let task = Task(operation: task)
Expand Down Expand Up @@ -57,14 +57,14 @@ public func withSynchronizedAccess<T, ID: Hashable>(
) async -> T {
if let cached = await Barriers.shared.tasks[taskIdentifier] {
if let task = cached as? Task<T, Error> {
// logger("Attempted to access a throwing synchronized task from a non-throwing context.")
runtimeWarn("Attempted to access a throwing synchronized task from a non-throwing context.")
if let result = try? await task.value {
return result
}
} else if let task = cached as? Task<T, Never> {
return await task.value
} else {
// runtimeWarn("Unexpected task type found in the barrier.")
runtimeWarn("Unexpected task type found in the barrier.")
}
}
let task = Task(operation: task)
Expand Down Expand Up @@ -96,11 +96,11 @@ public func waitForThrowingSynchronizedAccess<ID: Hashable, T>(id taskIdentifier
guard let cached = await Barriers.shared.tasks[taskIdentifier] else {
return nil
}
return try await cached.wait() as? T
// if result == nil, !(type is Void.Type) {
// runtimeWarn("Unexpected task type found in the waitForThrowingSynchronizedAccess.")
// }
// return result
let result = try await cached.wait() as? T
if result == nil, !(type is Void.Type) {
runtimeWarn("Unexpected task type found in the waitForThrowingSynchronizedAccess.")
}
return result
}

/// Waits for the completion of all synchronized accesses associated with a specific identifier before proceeding.
Expand All @@ -124,11 +124,11 @@ public func waitForSynchronizedAccess<ID: Hashable, T>(id taskIdentifier: ID, of
return nil
}
do {
return try await cached.wait() as? T
// if result == nil, !(type is Void.Type) {
// runtimeWarn("Unexpected task type found in the waitForThrowingSynchronizedAccess.")
// }
// return result
let result = try await cached.wait() as? T
if result == nil, !(type is Void.Type) {
runtimeWarn("Unexpected task type found in the waitForThrowingSynchronizedAccess.")
}
return result
} catch {
return nil
}
Expand Down

0 comments on commit 4d6cd3b

Please sign in to comment.