Created
December 20, 2022 17:07
-
-
Save shanecowherd/4e0b205c976ad75d658768d46a0524ba to your computer and use it in GitHub Desktop.
AWS Amplify Task Queue
This file contains hidden or 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