Created
May 29, 2022 04:19
-
-
Save boraseoksoon/339ef589e682ef86c8712d647b866608 to your computer and use it in GitHub Desktop.
A basic Async Sequence in Swift for HTML Parsing
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
import SwiftUI | |
// today coding: async sequence in Swift | |
struct URLSequence: AsyncSequence { | |
typealias AsyncIterator = URLIterator | |
typealias Element = Data | |
let urls: [URL] | |
func makeAsyncIterator() -> URLIterator { | |
URLIterator(urls: urls) | |
} | |
} | |
struct URLIterator: AsyncIteratorProtocol { | |
typealias Element = Data | |
let urls: [URL] | |
fileprivate var index: Int = 0 | |
private let urlSession = URLSession.shared | |
mutating func next() async throws -> Data? { | |
guard urls.count - 1 >= index, | |
case let url = urls[index] | |
else { return nil } | |
defer { | |
index+=1 | |
} | |
// main here | |
let (data, _) = try await urlSession.data(from: url) | |
return data | |
} | |
} | |
let urls = [ | |
URL(string:"https://swift.org"), | |
URL(string:"https://clojure.org"), | |
URL(string:"https://apple.com") | |
].compactMap { $0 } | |
Task { | |
for try await urlData in URLSequence(urls: urls) { | |
if let rawHTML = String(data: urlData, encoding: .utf8) { | |
print("rawHTML : \(rawHTML)") | |
} | |
} | |
} | |
// done! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment