Skip to content

Commit

Permalink
Remove async algorithms dependency (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-Keith-Thompson authored Oct 15, 2024
1 parent 884a950 commit 5593613
Show file tree
Hide file tree
Showing 105 changed files with 1,795 additions and 1,082 deletions.
70 changes: 70 additions & 0 deletions .swift-format.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"fileScopedDeclarationPrivacy" : {
"accessLevel" : "private"
},
"indentConditionalCompilationBlocks" : true,
"indentSwitchCaseLabels" : true,
"indentation" : {
"spaces" : 4
},
"lineBreakAroundMultilineExpressionChainComponents" : false,
"lineBreakBeforeControlFlowKeywords" : false,
"lineBreakBeforeEachArgument" : false,
"lineBreakBeforeEachGenericRequirement" : false,
"lineLength" : 100,
"maximumBlankLines" : 1,
"multiElementCollectionTrailingCommas" : true,
"noAssignmentInExpressions" : {
"allowedFunctions" : [
"XCTAssertNoThrow"
]
},
"prioritizeKeepingFunctionOutputTogether" : false,
"respectsExistingLineBreaks" : true,
"rules" : {
"AllPublicDeclarationsHaveDocumentation" : false,
"AlwaysUseLiteralForEmptyCollectionInit" : false,
"AlwaysUseLowerCamelCase" : true,
"AmbiguousTrailingClosureOverload" : true,
"BeginDocumentationCommentWithOneLineSummary" : false,
"DoNotUseSemicolons" : true,
"DontRepeatTypeInStaticProperties" : true,
"FileScopedDeclarationPrivacy" : true,
"FullyIndirectEnum" : true,
"GroupNumericLiterals" : true,
"IdentifiersMustBeASCII" : true,
"NeverForceUnwrap" : false,
"NeverUseForceTry" : false,
"NeverUseImplicitlyUnwrappedOptionals" : false,
"NoAccessLevelOnExtensionDeclaration" : true,
"NoAssignmentInExpressions" : true,
"NoBlockComments" : true,
"NoCasesWithOnlyFallthrough" : true,
"NoEmptyTrailingClosureParentheses" : true,
"NoLabelsInCasePatterns" : true,
"NoLeadingUnderscores" : false,
"NoParensAroundConditions" : true,
"NoPlaygroundLiterals" : true,
"NoVoidReturnOnFunctionSignature" : true,
"OmitExplicitReturns" : false,
"OneCasePerLine" : true,
"OneVariableDeclarationPerLine" : true,
"OnlyOneTrailingClosureArgument" : true,
"OrderedImports" : true,
"ReplaceForEachWithForLoop" : true,
"ReturnVoidInsteadOfEmptyTuple" : true,
"TypeNamesShouldBeCapitalized" : true,
"UseEarlyExits" : false,
"UseExplicitNilCheckInConditions" : true,
"UseLetInEveryBoundCaseVariable" : true,
"UseShorthandTypeNames" : true,
"UseSingleLinePropertyGetter" : true,
"UseSynthesizedInitializer" : true,
"UseTripleSlashForDocumentationComments" : true,
"UseWhereClausesInForLoops" : false,
"ValidateDocumentationComments" : false
},
"spacesAroundRangeFormationOperators" : false,
"tabWidth" : 8,
"version" : 1
}
3 changes: 0 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ let package = Package(name: "Afluent",
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.2.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/AliSoftware/OHHTTPStubs.git", from: "9.1.0"),
.package(url: "https://github.com/nicklockwood/SwiftFormat", from: "0.52.11"),
.package(url: "https://github.com/pointfreeco/swift-clocks.git", from: "1.0.2"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras.git", from: "1.1.0"),
.package(url: "https://github.com/apple/swift-async-algorithms.git", from: "1.0.0"),
],
targets: [
.target(name: "Afluent",
dependencies: [
.product(name: "Atomics", package: "swift-atomics"),
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency=complete"),
Expand Down
3 changes: 0 additions & 3 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ let package = Package(name: "Afluent",
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.2.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/AliSoftware/OHHTTPStubs.git", from: "9.1.0"),
.package(url: "https://github.com/nicklockwood/SwiftFormat", from: "0.52.11"),
.package(url: "https://github.com/pointfreeco/swift-clocks.git", from: "1.0.2"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras.git", from: "1.1.0"),
.package(url: "https://github.com/apple/swift-testing.git", from: "0.7.0"),
.package(url: "https://github.com/apple/swift-async-algorithms.git", from: "1.0.0"),
],
targets: [
.target(name: "Afluent",
dependencies: [
.product(name: "Atomics", package: "swift-atomics"),
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency=complete"),
Expand Down
3 changes: 0 additions & 3 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ let package = Package(name: "Afluent",
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.2.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/AliSoftware/OHHTTPStubs.git", from: "9.1.0"),
.package(url: "https://github.com/nicklockwood/SwiftFormat", from: "0.52.11"),
.package(url: "https://github.com/pointfreeco/swift-clocks.git", from: "1.0.2"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras.git", from: "1.1.0"),
.package(url: "https://github.com/apple/swift-async-algorithms.git", from: "1.0.0"),
],
targets: [
.target(name: "Afluent",
dependencies: [
.product(name: "Atomics", package: "swift-atomics"),
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency=complete"),
Expand Down
3 changes: 0 additions & 3 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ let package = Package(name: "Afluent",
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.2.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
.package(url: "https://github.com/AliSoftware/OHHTTPStubs.git", from: "9.1.0"),
.package(url: "https://github.com/nicklockwood/SwiftFormat", from: "0.52.11"),
.package(url: "https://github.com/pointfreeco/swift-clocks.git", from: "1.0.2"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras.git", from: "1.1.0"),
.package(url: "https://github.com/apple/swift-async-algorithms.git", from: "1.0.0"),
],
targets: [
.target(name: "Afluent",
dependencies: [
.product(name: "Atomics", package: "swift-atomics"),
.product(name: "AsyncAlgorithms", package: "swift-async-algorithms"),
],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency=complete"),
Expand Down
115 changes: 61 additions & 54 deletions Sources/Afluent/Additions/QueueExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,76 @@
import Foundation

#if swift(>=6)
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
public final class QueueExecutor: TaskExecutor, Sendable, CustomStringConvertible {
let queue: DispatchQueue
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
public final class QueueExecutor: TaskExecutor, Sendable, CustomStringConvertible {
let queue: DispatchQueue

init(queue: DispatchQueue) {
self.queue = queue
}

public func enqueue(_ job: consuming ExecutorJob) {
let job = UnownedJob(job)
queue.async {
job.runSynchronously(on: self.asUnownedTaskExecutor())
init(queue: DispatchQueue) {
self.queue = queue
}
}

public var description: String {
"\(Self.self)\(ObjectIdentifier(self))"
}
public func enqueue(_ job: consuming ExecutorJob) {
let job = UnownedJob(job)
queue.async {
job.runSynchronously(on: self.asUnownedTaskExecutor())
}
}

fileprivate static let main = QueueExecutor(queue: .main)
fileprivate static let globalBackground = QueueExecutor(queue: .global(qos: .background))
fileprivate static let globalUtility = QueueExecutor(queue: .global(qos: .utility))
fileprivate static let globalDefault = QueueExecutor(queue: .global(qos: .default))
fileprivate static let globalUserInitiated = QueueExecutor(queue: .global(qos: .userInitiated))
fileprivate static let globalUserInteractive = QueueExecutor(queue: .global(qos: .userInteractive))
fileprivate static let globalUnspecified = QueueExecutor(queue: .global(qos: .unspecified))
}
public var description: String {
"\(Self.self)\(ObjectIdentifier(self))"
}

@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
extension TaskExecutor where Self == QueueExecutor {
public static var mainQueue: QueueExecutor {
QueueExecutor.main
fileprivate static let main = QueueExecutor(queue: .main)
fileprivate static let globalBackground = QueueExecutor(queue: .global(qos: .background))
fileprivate static let globalUtility = QueueExecutor(queue: .global(qos: .utility))
fileprivate static let globalDefault = QueueExecutor(queue: .global(qos: .default))
fileprivate static let globalUserInitiated = QueueExecutor(
queue: .global(qos: .userInitiated))
fileprivate static let globalUserInteractive = QueueExecutor(
queue: .global(qos: .userInteractive))
fileprivate static let globalUnspecified = QueueExecutor(queue: .global(qos: .unspecified))
}

public static func globalQueue(qos: DispatchQoS.QoSClass = .default) -> QueueExecutor {
switch qos {
case .background:
return .globalBackground
case .utility:
return .globalUtility
case .default:
return .globalDefault
case .userInitiated:
return .globalUserInitiated
case .userInteractive:
return .globalUserInteractive
case .unspecified:
return .globalUnspecified
@unknown default:
return .init(queue: .global(qos: qos))
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
extension TaskExecutor where Self == QueueExecutor {
public static var mainQueue: QueueExecutor {
QueueExecutor.main
}
}

public static func queue(label: String,
qos: DispatchQoS = .unspecified,
attributes: DispatchQueue.Attributes = [],
autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency = .inherit,
target: DispatchQueue? = nil) -> QueueExecutor {
queue(DispatchQueue(label: label, qos: qos, attributes: attributes, autoreleaseFrequency: autoreleaseFrequency, target: target))
}
public static func globalQueue(qos: DispatchQoS.QoSClass = .default) -> QueueExecutor {
switch qos {
case .background:
return .globalBackground
case .utility:
return .globalUtility
case .default:
return .globalDefault
case .userInitiated:
return .globalUserInitiated
case .userInteractive:
return .globalUserInteractive
case .unspecified:
return .globalUnspecified
@unknown default:
return .init(queue: .global(qos: qos))
}
}

public static func queue(_ queue: DispatchQueue) -> QueueExecutor {
QueueExecutor(queue: queue)
public static func queue(
label: String,
qos: DispatchQoS = .unspecified,
attributes: DispatchQueue.Attributes = [],
autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency = .inherit,
target: DispatchQueue? = nil
) -> QueueExecutor {
queue(
DispatchQueue(
label: label, qos: qos, attributes: attributes,
autoreleaseFrequency: autoreleaseFrequency, target: target))
}

public static func queue(_ queue: DispatchQueue) -> QueueExecutor {
QueueExecutor(queue: queue)
}
}
}
#endif
16 changes: 10 additions & 6 deletions Sources/Afluent/Additions/URLSessionAdditions.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)
//
//
// URLSessionAdditions.swift
//
//
//
//
// Created by Tyler Thompson on 10/29/23.
//
//

import Foundation

extension URLSession {
/// Returns a deferred data task that wraps a URL session data task for a given URL.
public func deferredDataTask(from url: URL) -> some AsynchronousUnitOfWork<(data: Data, response: URLResponse)> {
public func deferredDataTask(from url: URL) -> some AsynchronousUnitOfWork<
(data: Data, response: URLResponse)
> {
DeferredTask {
try await data(from: url)
}
}

/// Returns a deferred data task that wraps a URL session data task for a given URL.
public func deferredDataTask(for urlRequest: URLRequest) -> some AsynchronousUnitOfWork<(data: Data, response: URLResponse)> {
public func deferredDataTask(for urlRequest: URLRequest) -> some AsynchronousUnitOfWork<
(data: Data, response: URLResponse)
> {
DeferredTask {
try await data(for: urlRequest)
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/Afluent/AsyncSequenceCache.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//
// AsyncSequenceCache.swift
// Afluent
//
// Created by Tyler Thompson on 10/13/24.
//
6 changes: 4 additions & 2 deletions Sources/Afluent/AsynchronousUnitOfWorkCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ public final class AsynchronousUnitOfWorkCache: @unchecked Sendable {
let lock = NSRecursiveLock()
var cache = [Int: any AsynchronousUnitOfWork & AnySendableReference]()

public init() { }
public init() {}

func retrieveOrCreate<A: AsynchronousUnitOfWork & AnySendableReference>(unitOfWork: A, keyedBy key: Int) -> A {
func retrieveOrCreate<A: AsynchronousUnitOfWork & AnySendableReference>(
unitOfWork: A, keyedBy key: Int
) -> A {
lock.lock()
if let fromCache: A = cache[key] as? A {
lock.unlock()
Expand Down
Loading

0 comments on commit 5593613

Please sign in to comment.