Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves #170 - Return error on monitor disconnection #175

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions Source/BluetoothManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ public class BluetoothManager {
/// - parameter peripheral: The `Peripheral` to which the `BluetoothManager` is either trying to
/// connect or has already connected.
/// - returns: `Single` which emits next event when peripheral successfully cancelled connection.
public func cancelPeripheralConnection(_ peripheral: Peripheral) -> Single<Peripheral> {
let observable = Observable<Peripheral>.create { [weak self] observer in
public func cancelPeripheralConnection(_ peripheral: Peripheral) -> Single<(Peripheral)> {
let observable = Observable<(Peripheral, DisconnectionReason?)>.create { [weak self] observer in
guard let strongSelf = self else {
observer.onError(BluetoothError.destroyed)
return Disposables.create()
Expand All @@ -265,7 +265,9 @@ public class BluetoothManager {
strongSelf.centralManager.cancelPeripheralConnection(peripheral.peripheral)
return disposable
}
return ensure(.poweredOn, observable: observable).asSingle()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for dropping the Single here? Can we emit multiple events now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Already repairing it.

return ensure(.poweredOn, observable: observable)
.asSingle()
.map { $0.0 }
}

// MARK: Retrieving Lists of Peripherals
Expand Down Expand Up @@ -342,22 +344,22 @@ public class BluetoothManager {
/// - Parameter peripheral: `Peripheral` which is monitored for connection.
/// - Returns: Observable which emits next events when `peripheral` was connected.
public func monitorConnection(for peripheral: Peripheral) -> Observable<Peripheral> {
return monitorPeripheral(on: delegateWrapper.rx_didConnectPeripheral, peripheral: peripheral)
let observable = delegateWrapper.rx_didConnectPeripheral
.filter { $0 == peripheral.peripheral }
.map { _ in peripheral }
return ensure(.poweredOn, observable: observable)
}

public typealias DisconnectionReason = Error
/// Emits `Peripheral` instance when it's disconnected.
/// - Parameter peripheral: `Peripheral` which is monitored for disconnection.
/// - Returns: Observable which emits next events when `peripheral` was disconnected.
public func monitorDisconnection(for peripheral: Peripheral) -> Observable<Peripheral> {
return monitorPeripheral(on: delegateWrapper.rx_didDisconnectPeripheral.map { $0.0 }, peripheral: peripheral)
}

func monitorPeripheral(on peripheralAction: Observable<CBPeripheral>, peripheral: Peripheral)
-> Observable<Peripheral> {
let observable =
peripheralAction
.filter { $0 == peripheral.peripheral }
.map { _ in peripheral }
/// - Returns: Observable which emits next events when `Peripheral` instance was disconnected.
/// It provides optional error which may contain more information about the cause of the disconnection
/// if it wasn't the `cancelConnection` call
public func monitorDisconnection(for peripheral: Peripheral) -> Observable<(Peripheral, DisconnectionReason?)> {
let observable = delegateWrapper.rx_didDisconnectPeripheral
.filter { $0.0 == peripheral.peripheral }
.map { (_, error) -> (Peripheral, DisconnectionReason?) in (peripheral, error) }
return ensure(.poweredOn, observable: observable)
}

Expand Down