AWS Amplify Task Queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Copyright Amazon.com Inc. or its affiliates. | |
// All Rights Reserved. | |
// | |
// SPDX-License-Identifier: Apache-2.0 | |
// | |
import Foundation | |
public actor TaskQueue<Success> { | |
private var previousTask: Task<Success, Error>? | |
public init() {} | |
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 | |
} | |
public nonisolated func async(block: @Sendable @escaping () async throws -> Success) rethrows { | |
Task { | |
try await sync(block: block) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment