Skip to content

Instantly share code, notes, and snippets.

@shanecowherd
Created December 20, 2022 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanecowherd/4e0b205c976ad75d658768d46a0524ba to your computer and use it in GitHub Desktop.
Save shanecowherd/4e0b205c976ad75d658768d46a0524ba to your computer and use it in GitHub Desktop.
AWS Amplify Task Queue
//
// 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