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

Only capture the absolute necessary values inside closures, to avoid improperly caching or reusing of core bluetooth objects #33

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,24 @@ public struct CentralManager: Sendable {
}

public func connect(_ peripheral: Peripheral, options: PeripheralConnectionOptions? = nil) -> AnyPublisher<Peripheral, Error> {
Publishers.Merge(
didConnectPeripheral
.filter { $0 == peripheral }
.setFailureType(to: Error.self),
didFailToConnectPeripheral
.filter { p, _ in p == peripheral }
.tryMap { _, error in
throw error ?? CentralManagerError.unknownConnectionFailure
}
)
.prefix(1)
.handleEvents(receiveSubscription: { _ in
_connectToPeripheral(peripheral, options)
}, receiveCancel: {
_cancelPeripheralConnection(peripheral)
})
.shareCurrentValue()
.eraseToAnyPublisher()
Publishers.Merge(
didConnectPeripheral
.filter { [id = peripheral.id] p in p.id == id }
.setFailureType(to: Error.self),
didFailToConnectPeripheral
.filter { [id = peripheral.id] p, _ in p.id == id }
.tryMap { _, error in
throw error ?? CentralManagerError.unknownConnectionFailure
}
)
.prefix(1)
.handleEvents(receiveSubscription: { _ in
_connectToPeripheral(peripheral, options)
}, receiveCancel: {
_cancelPeripheralConnection(peripheral)
})
.shareCurrentValue()
.eraseToAnyPublisher()
}

public func cancelPeripheralConnection(_ peripheral: Peripheral) {
Expand All @@ -117,10 +117,10 @@ public struct CentralManager: Sendable {
public func monitorConnection(for peripheral: Peripheral) -> AnyPublisher<Bool, Never> {
Publishers.Merge(
didConnectPeripheral
.filter { p in p == peripheral }
.filter { [id = peripheral.id] p in p.id == id }
.map { _ in true },
didDisconnectPeripheral
.filter { (p, error) in p == peripheral }
.filter { [id = peripheral.id] p, _ in p.id == id }
.map { _ in false }
)
.eraseToAnyPublisher()
Expand Down
42 changes: 21 additions & 21 deletions Sources/CombineCoreBluetooth/Peripheral/Interface+Peripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,20 @@ public struct Peripheral: Sendable {

private func writeValueWithResponse(_ value: Data, for characteristic: CBCharacteristic) -> AnyPublisher<Void, Error> {
didWriteValueForCharacteristic
.filterFirstValueOrThrow(where: {
$0.uuid == characteristic.uuid
})
.map { _ in }
.handleEvents(receiveSubscription: { [_writeValueForCharacteristic] _ in
_writeValueForCharacteristic(value, characteristic, .withResponse)
})
.shareCurrentValue()
.filterFirstValueOrThrow(where: { [uuid = characteristic.uuid] in
$0.uuid == uuid
})
.map { _ in }
.handleEvents(receiveSubscription: { [_writeValueForCharacteristic] _ in
_writeValueForCharacteristic(value, characteristic, .withResponse)
})
.shareCurrentValue()
}

public func setNotifyValue(_ enabled: Bool, for characteristic: CBCharacteristic) -> AnyPublisher<Void, Error> {
didUpdateNotificationState
.filterFirstValueOrThrow(where: {
$0.uuid == characteristic.uuid
.filterFirstValueOrThrow(where: { [uuid = characteristic.uuid] in
$0.uuid == uuid
})
.map { _ in }
.handleEvents(receiveSubscription: { [_setNotifyValue] _ in
Expand All @@ -214,8 +214,8 @@ public struct Peripheral: Sendable {

public func discoverDescriptors(for characteristic: CBCharacteristic) -> AnyPublisher<[CBDescriptor]?, Error> {
didDiscoverDescriptorsForCharacteristic
.filterFirstValueOrThrow(where: {
$0.uuid == characteristic.uuid
.filterFirstValueOrThrow(where: { [uuid = characteristic.uuid] in
$0.uuid == uuid
})
.map(\.descriptors)
.handleEvents(receiveSubscription: { [_discoverDescriptors] _ in
Expand All @@ -226,8 +226,8 @@ public struct Peripheral: Sendable {

public func readValue(for descriptor: CBDescriptor) -> AnyPublisher<Any?, Error> {
didUpdateValueForDescriptor
.filterFirstValueOrThrow(where: {
$0.uuid == descriptor.uuid
.filterFirstValueOrThrow(where: { [uuid = descriptor.uuid] in
$0.uuid == uuid
})
.map(\.value)
.handleEvents(receiveSubscription: { [_readValueForDescriptor] _ in
Expand All @@ -238,8 +238,8 @@ public struct Peripheral: Sendable {

public func writeValue(_ value: Data, for descriptor: CBDescriptor) -> AnyPublisher<Void, Error> {
didWriteValueForDescriptor
.filterFirstValueOrThrow(where: {
$0.uuid == descriptor.uuid
.filterFirstValueOrThrow(where: { [uuid = descriptor.uuid] in
$0.uuid == uuid
})
.map { _ in }
.handleEvents(receiveSubscription: { [_writeValueForDescriptor] _ in
Expand Down Expand Up @@ -292,7 +292,7 @@ public struct Peripheral: Sendable {
discoverCharacteristics(withUUIDs: [characteristicUUID], inServiceWithUUID: serviceUUID)
.tryMap { characteristics in
// assume core bluetooth won't send us a characteristic list without the characteristic we expect
guard let characteristic = characteristics.first(where: { characteristic in characteristic.uuid == characteristicUUID }) else {
guard let characteristic = characteristics.first(where: { c in c.uuid == characteristicUUID }) else {
throw PeripheralError.characteristicNotFound(characteristicUUID)
}
return characteristic
Expand Down Expand Up @@ -405,8 +405,8 @@ public struct Peripheral: Sendable {
public func listenForUpdates(on characteristic: CBCharacteristic) -> AnyPublisher<Data?, Error> {
didUpdateValueForCharacteristic
// not limiting to `.first()` here as callers may want long-lived listening for value changes
.filter({ (readCharacteristic, error) -> Bool in
return readCharacteristic.uuid == characteristic.uuid
.filter({ [uuid = characteristic.uuid] (readCharacteristic, error) -> Bool in
readCharacteristic.uuid == uuid
})
.selectValueOrThrowError()
.map(\.value)
Expand Down Expand Up @@ -438,8 +438,8 @@ public struct Peripheral: Sendable {
self.listenForUpdates(on: characteristic),

didUpdateNotificationState
.filterFirstValueOrThrow(where: {
$0.uuid == characteristic.uuid
.filterFirstValueOrThrow(where: { [uuid = characteristic.uuid] in
$0.uuid == uuid
})
.ignoreOutput(setOutputType: Data?.self)
)
Expand Down
Loading