Compare commits

...

3 Commits

Author SHA1 Message Date
Jørgen Henrichsen e6ca8c9b1e Update README.
Add part about using Combine for events.
2019-06-21 13:48:11 +02:00
Jørgen Henrichsen 83318d3ff0 Make all error enums public. Add EventError. 2019-06-21 13:28:51 +02:00
Jørgen Henrichsen 3c5f858a94 Add support for Combine in events.
Add an `EventPublisher` and a publisher property in the `Event`-class.
Can be used to subscribe to events with a `Subscriber`.
2019-06-21 13:22:09 +02:00
3 changed files with 48 additions and 5 deletions
+12
View File
@@ -63,6 +63,18 @@ class MyCustomViewController: UIViewController {
}
```
If you want to use the [Combine](https://developer.apple.com/documentation/combine) framework for events, each event in the `AudioPlayer` has an `EventPublisher` that can be subscribed to:
```swift
let audioPlayer = AudioPlayer()
audioPlayer.event.stateChange.publisher.subscribe(subscriber: someSubscriber)
/// Using a Sink
audioPlayer.event.stateChange.publisher.sink { state in
/// Handle state change here.
}
```
**Important**: This requires iOS version equal to or later than 13.0. If an application needs to support older iOS versions it is recommended to use the regular `Event.addListener(listener:)` method.
#### QueuedAudioPlayer
The `QueuedAudioPlayer` is a subclass of `AudioPlayer` that maintains a queue of audio tracks.
```swift
+5 -3
View File
@@ -10,18 +10,20 @@ import Foundation
public struct APError {
enum LoadError: Error {
public enum LoadError: Error {
case invalidSourceUrl(String)
}
enum PlaybackError: Error {
public enum PlaybackError: Error {
case noLoadedItem
}
enum QueueError: Error {
public enum QueueError: Error {
case noPreviousItem
case noNextItem
case invalidIndex(index: Int, message: String)
}
public enum EventError: Error {}
}
+31 -2
View File
@@ -6,6 +6,7 @@
//
import Foundation
import Combine
extension AudioPlayer {
@@ -69,7 +70,7 @@ extension AudioPlayer {
class Invoker<EventData> {
// Signals false if the listener object is nil
/// Signals false if the listener object is nil. If `false` is signaled, the invoker should not be retained.
let invoke: (EventData) -> Bool
weak var listener: AnyObject?
@@ -114,7 +115,7 @@ extension AudioPlayer {
self.invokersSemaphore.signal()
}
}
func emit(data: EventData) {
eventQueue.async {
self.invokersSemaphore.wait()
@@ -125,6 +126,34 @@ extension AudioPlayer {
}
}
/**
The publisher for this event. Use this for subscription through the Combine framework.
*/
@available(iOS 13.0, *)
public lazy var publisher: EventPublisher<EventData> = EventPublisher(event: self)
}
}
@available(iOS 13.0, *)
extension AudioPlayer {
public class EventPublisher<EventData>: Publisher {
public typealias Output = EventData
public typealias Failure = APError.EventError
private weak var event: Event<EventData>?
init(event: Event<EventData>) {
self.event = event
}
public func receive<S>(subscriber: S) where S : Subscriber, Failure == S.Failure, Output == S.Input {
event?.addListener(self) { (data) in
let _ = subscriber.receive(data)
}
}
}
}