From ebc4ca64490f219cae662828102de1a6c27549e8 Mon Sep 17 00:00:00 2001 From: Sebastian Villena <97059974+ruisebas@users.noreply.github.com> Date: Wed, 10 Jan 2024 13:53:25 -0500 Subject: [PATCH] Reverting TaskQueue --- Amplify/Core/Support/TaskQueue.swift | 42 +++---------------- .../AWSCognitoAuthPlugin.swift | 2 +- .../CredentialStoreOperationClient.swift | 2 +- .../Sync/RemoteSyncEngine.swift | 2 +- .../Session/SessionClient.swift | 2 +- 5 files changed, 10 insertions(+), 40 deletions(-) diff --git a/Amplify/Core/Support/TaskQueue.swift b/Amplify/Core/Support/TaskQueue.swift index ffd4cd8097..f281c57e1c 100644 --- a/Amplify/Core/Support/TaskQueue.swift +++ b/Amplify/Core/Support/TaskQueue.swift @@ -8,26 +8,24 @@ import Foundation /// A helper for executing asynchronous work serially. -public actor TaskQueue where Failure: Error { - private var previousTask: Task? +public actor TaskQueue { + private var previousTask: Task? 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() + /// let tq = TaskQueue() /// 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 = Task { [previousTask] in + public func sync(block: @Sendable @escaping () async throws -> Success) async throws -> Success { + let currentTask: Task = Task { [previousTask] in _ = await previousTask?.result return try await block() } @@ -35,37 +33,9 @@ public extension TaskQueue where Failure == any Error { 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() - /// 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 = 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) - } - } -} diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin.swift index b0f8c94f2b..f32209ba9c 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin.swift @@ -28,7 +28,7 @@ public final class AWSCognitoAuthPlugin: AWSCognitoAuthPluginBehavior { var analyticsHandler: UserPoolAnalyticsBehavior! - var taskQueue: TaskQueue! + var taskQueue: TaskQueue! var httpClientEngineProxy: HttpClientEngineProxy? diff --git a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/CredentialStorage/CredentialStoreOperationClient.swift b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/CredentialStorage/CredentialStoreOperationClient.swift index 8b3134a5e4..d1a0bba2fd 100644 --- a/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/CredentialStorage/CredentialStoreOperationClient.swift +++ b/AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/CredentialStorage/CredentialStoreOperationClient.swift @@ -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() + private let taskQueue = TaskQueue() init(credentialStoreStateMachine: CredentialStoreStateMachine) { self.credentialStoreStateMachine = credentialStoreStateMachine diff --git a/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Sync/RemoteSyncEngine.swift b/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Sync/RemoteSyncEngine.swift index 1b7d8e9d15..26bb453571 100644 --- a/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Sync/RemoteSyncEngine.swift +++ b/AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Sync/RemoteSyncEngine.swift @@ -46,7 +46,7 @@ class RemoteSyncEngine: RemoteSyncEngineBehavior { } /// Synchronizes startup operations - let taskQueue = TaskQueue() + let taskQueue = TaskQueue() // Assigned at `setUpCloudSubscriptions` var reconciliationQueue: IncomingEventReconciliationQueue? diff --git a/AmplifyPlugins/Internal/Sources/InternalAWSPinpoint/Session/SessionClient.swift b/AmplifyPlugins/Internal/Sources/InternalAWSPinpoint/Session/SessionClient.swift index e022c79fb9..6d1fb27a03 100644 --- a/AmplifyPlugins/Internal/Sources/InternalAWSPinpoint/Session/SessionClient.swift +++ b/AmplifyPlugins/Internal/Sources/InternalAWSPinpoint/Session/SessionClient.swift @@ -34,7 +34,7 @@ class SessionClient: SessionClientBehaviour { private let configuration: SessionClientConfiguration private let sessionClientQueue = DispatchQueue(label: Constants.queue, attributes: .concurrent) - private let analyticsTaskQueue = TaskQueue() + private let analyticsTaskQueue = TaskQueue() private let userDefaults: UserDefaultsBehaviour private var sessionBackgroundTimeout: TimeInterval = .zero