Skip to content

Commit

Permalink
Merge pull request #8 from teufelaudio/kvo-bypass
Browse files Browse the repository at this point in the history
Implements delegate kvo bypass
  • Loading branch information
LukasLiebl authored Jun 28, 2024
2 parents d97b424 + 98d685a commit 122b3e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import CoreBluetooth

extension CBCentralManager {
/// Returns an implementation of CentralManager, which observes it's CBCentralManager's delegate for changes.
/// In case the delegate gets changed to something other than this CentralManager implementation it'll end
/// publishers with a failure.
public var combine: any CentralManager {
// Disable the skip of central manager delegate observation as we know the central manager is an instance of CBCentralManager.
CoreBluetoothCentralManager(centralManager: self)
}

/// Returns an implementation of CentralManager, which *does not* observe it's CBCentralManager's delegate for changes.
/// In case of change of the delegate, publishers will not end with a failure.
public var combineWithoutDelegateObservation: any CentralManager {
CoreBluetoothCentralManager(centralManager: self, skipDelegateObservation: true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ class CoreBluetoothCentralManager: NSObject {
private var _didDisconnectPeripheral: PassthroughSubject<(peripheral: CBPeripheral, error: Error?), Never> = .init()

// MARK: Init
required public init(centralManager: CBCentralManager) {
required init(centralManager: CBCentralManager, skipDelegateObservation: Bool = false) {
self.centralManager = centralManager
super.init()
restoreDelegation()
restoreDelegation(skipDelegateObservation)
}

// MARK: Private funcs
private func restoreDelegation() {
private func restoreDelegation(_ skipDelegateObservation: Bool) {
_state = .init(centralManager.state)
_stateRestoration = .init()
_scanPublisher = .init()
centralManager.delegate = self

guard !skipDelegateObservation else { return }

kvoDelegate = centralManager
.publisher(for: \.delegate)
.sink { [weak self] value in
Expand Down

0 comments on commit 122b3e6

Please sign in to comment.