Skip to content
This repository has been archived by the owner on Jun 15, 2024. It is now read-only.

Commit

Permalink
Update concurrency in parser (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
omaralbeik authored Apr 28, 2022
1 parent 4521ede commit 50b2b9e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ The [Swift Package Manager](https://swift.org/package-manager/) is a tool for ma

```swift
dependencies: [
.package(url: "https://github.com/omaralbeik/M3UKit.git", from: "0.5.2")
.package(url: "https://github.com/omaralbeik/M3UKit.git", from: "0.5.3")
]
```

Expand All @@ -121,15 +121,15 @@ $ swift build
To integrate M3UKit into your Xcode project using [CocoaPods](https://cocoapods.org), specify it in your Podfile:

```rb
pod 'M3UKit', :git => 'https://github.com/omaralbeik/M3UKit.git', :tag => '0.5.2'
pod 'M3UKit', :git => 'https://github.com/omaralbeik/M3UKit.git', :tag => '0.5.3'
```

### Carthage

To integrate M3UKit into your Xcode project using [Carthage](https://github.com/Carthage/Carthage), specify it in your Cartfile:

```
github "omaralbeik/M3UKit" ~> 0.5.2
github "omaralbeik/M3UKit" ~> 0.5.3
```

### Manually
Expand Down
26 changes: 16 additions & 10 deletions Sources/M3UKit/Parsers/PlaylistParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,23 @@ public final class PlaylistParser: Parser {
/// Parse a playlist on a queue with a completion handler.
/// - Parameters:
/// - input: source.
/// - queue: queue to perform parsing on. Defaults to `.global(qos: .userInitiated)`
/// - processingQueue: queue to perform parsing on. Defaults to `.global(qos: .background)`
/// - callbackQueue: queue to call callback on. Defaults to `.main`
/// - completion: completion handler to call with the result.
public func parse(
_ input: PlaylistSource,
queue: DispatchQueue = .global(qos: .userInitiated),
processingQueue: DispatchQueue = .global(qos: .background),
callbackQueue: DispatchQueue = .main,
completion: @escaping (Result<Playlist, Error>) -> Void
) {
queue.async {
processingQueue.async {
do {
let playlist = try self.parse(input)
DispatchQueue.main.async {
callbackQueue.async {
completion(.success(playlist))
}
} catch {
DispatchQueue.main.async {
callbackQueue.async {
completion(.failure(error))
}
}
Expand All @@ -148,13 +150,17 @@ public final class PlaylistParser: Parser {
@available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
/// Parse a playlist.
/// - Parameter input: source.
/// - Parameter priority: Processing task priority. Defaults to `.background`
/// - Returns: playlist.
public func parse(_ input: PlaylistSource) async throws -> Playlist {
return try await withCheckedThrowingContinuation { continuation in
self.parse(input) { result in
continuation.resume(with: result)
}
public func parse(
_ input: PlaylistSource,
priority: TaskPriority = .background
) async throws -> Playlist {
let processingTask = Task(priority: priority) { () -> Playlist in
let playlist = try self.parse(input)
return playlist
}
return try await processingTask.value
}

// MARK: - Helpers
Expand Down

0 comments on commit 50b2b9e

Please sign in to comment.