Skip to content

Commit

Permalink
Reverting TaskQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
sebaland committed Jan 10, 2024
1 parent 01835f5 commit ebc4ca6
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 40 deletions.
42 changes: 6 additions & 36 deletions Amplify/Core/Support/TaskQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,34 @@
import Foundation

/// A helper for executing asynchronous work serially.
public actor TaskQueue<Success, Failure> where Failure: Error {
private var previousTask: Task<Success, Failure>?
public actor TaskQueue<Success> {
private var previousTask: Task<Success, Error>?

public init() {}
}

public extension TaskQueue where Failure == any Error {
/// Serializes asynchronous requests made from an async context
///
/// Given an invocation like
/// ```swift
/// let tq = TaskQueue<Int, Error>()
/// let tq = TaskQueue<Int>()
/// let v1 = try await tq.sync { try await doAsync1() }
/// let v2 = try await tq.sync { try await doAsync2() }
/// let v3 = try await tq.sync { try await doAsync3() }
/// ```
/// TaskQueue serializes this work so that `doAsync1` is performed before `doAsync2`,
/// which is performed before `doAsync3`.
func sync(block: @Sendable @escaping () async throws -> Success) async throws -> Success {
let currentTask: Task<Success, Failure> = Task { [previousTask] in
public func sync(block: @Sendable @escaping () async throws -> Success) async throws -> Success {
let currentTask: Task<Success, Error> = Task { [previousTask] in
_ = await previousTask?.result
return try await block()
}
previousTask = currentTask
return try await currentTask.value
}

nonisolated func async(block: @Sendable @escaping () async throws -> Success) rethrows {
public nonisolated func async(block: @Sendable @escaping () async throws -> Success) rethrows {
Task {
try await sync(block: block)
}
}
}

public extension TaskQueue where Failure == Never {
/// Serializes asynchronous requests made from an async context
///
/// Given an invocation like
/// ```swift
/// let tq = TaskQueue<Int, Never>()
/// let v1 = await tq.sync { await doAsync1() }
/// let v2 = await tq.sync { await doAsync2() }
/// let v3 = await tq.sync { await doAsync3() }
/// ```
/// TaskQueue serializes this work so that `doAsync1` is performed before `doAsync2`,
/// which is performed before `doAsync3`.
func sync(block: @Sendable @escaping () async -> Success) async -> Success {
let currentTask: Task<Success, Failure> = Task { [previousTask] in
_ = await previousTask?.result
return await block()
}
previousTask = currentTask
return await currentTask.value
}

nonisolated func async(block: @Sendable @escaping () async -> Success) {
Task {
await sync(block: block)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class AWSCognitoAuthPlugin: AWSCognitoAuthPluginBehavior {

var analyticsHandler: UserPoolAnalyticsBehavior!

var taskQueue: TaskQueue<Any, Error>!
var taskQueue: TaskQueue<Any>!

var httpClientEngineProxy: HttpClientEngineProxy?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CredentialStoreOperationClient: CredentialStoreStateBehavior {

// Task queue is being used to manage CRUD operations to the credential store synchronously
// This will help us keeping the CRUD methods atomic
private let taskQueue = TaskQueue<CredentialStoreData?, Error>()
private let taskQueue = TaskQueue<CredentialStoreData?>()

init(credentialStoreStateMachine: CredentialStoreStateMachine) {
self.credentialStoreStateMachine = credentialStoreStateMachine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class RemoteSyncEngine: RemoteSyncEngineBehavior {
}

/// Synchronizes startup operations
let taskQueue = TaskQueue<Void, Error>()
let taskQueue = TaskQueue<Void>()

// Assigned at `setUpCloudSubscriptions`
var reconciliationQueue: IncomingEventReconciliationQueue?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SessionClient: SessionClientBehaviour {
private let configuration: SessionClientConfiguration
private let sessionClientQueue = DispatchQueue(label: Constants.queue,
attributes: .concurrent)
private let analyticsTaskQueue = TaskQueue<Void, Never>()
private let analyticsTaskQueue = TaskQueue<Void>()
private let userDefaults: UserDefaultsBehaviour
private var sessionBackgroundTimeout: TimeInterval = .zero

Expand Down

0 comments on commit ebc4ca6

Please sign in to comment.