Skip to content
Open
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
17 changes: 16 additions & 1 deletion Sources/SwiftBluetooth/Async/AsyncSubscriptionQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal final class AsyncSubscriptionQueue<Value> {
return item
}

func recieve(_ value: Value) {
func receive(_ value: Value) {
dispatchQueue.async {
for item in self.items.reversed() {
item.block(value, item.cancel)
Expand All @@ -39,3 +39,18 @@ internal final class AsyncSubscriptionQueue<Value> {
}
}
}

// MARK: Deprecated

extension AsyncSubscriptionQueue {

@available(*, deprecated, renamed: "receive")
func recieve(_ value: Value) {
dispatchQueue.async {
for item in self.items.reversed() {
item.block(value, item.cancel)
}
}
}

}
17 changes: 16 additions & 1 deletion Sources/SwiftBluetooth/Async/AsyncSubscriptionQueueMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,26 @@ internal final class AsyncSubscriptionQueueMap<Key, Value> where Key: Hashable {
return item.queue(block: block, completion: completion)
}

func receive(key: Key, withValue value: Value) {
dispatchQueue.async {
guard let queue = self.items[key] else { return }

queue.receive(value)
}
}
}

// MARK: Deprecated

extension AsyncSubscriptionQueueMap {

@available(*, deprecated, renamed: "receive")
func recieve(key: Key, withValue value: Value) {
dispatchQueue.async {
guard let queue = self.items[key] else { return }

queue.recieve(value)
queue.receive(value)
}
}

}
2 changes: 1 addition & 1 deletion Sources/SwiftBluetooth/CentralManager/CentralManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public extension CentralManager {
}

func stopScan() {
eventSubscriptions.recieve(.stopScan)
eventSubscriptions.receive(.stopScan)
centralManager.stopScan()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CentralManagerDelegateWrapper: NSObject, CBCentralManagerDelegate {
parent.delegate?.centralManagerDidUpdateState(parent)

parent.eventQueue.async {
parent.eventSubscriptions.recieve(.stateUpdated(parent.state))
parent.eventSubscriptions.receive(.stateUpdated(parent.state))
}
}

Expand All @@ -27,7 +27,7 @@ class CentralManagerDelegateWrapper: NSObject, CBCentralManagerDelegate {
parent.delegate?.centralManager(parent, didConnect: peripheral)

parent.eventQueue.async {
parent.eventSubscriptions.recieve(.connected(peripheral))
parent.eventSubscriptions.receive(.connected(peripheral))
}
}

Expand All @@ -45,8 +45,8 @@ class CentralManagerDelegateWrapper: NSObject, CBCentralManagerDelegate {
// parent.removePeripheral(peripheral.cbPeripheral)

parent.eventQueue.async {
parent.eventSubscriptions.recieve(.disconnected(peripheral, error))
peripheral.eventSubscriptions.recieve(.didDisconnect(error))
parent.eventSubscriptions.receive(.disconnected(peripheral, error))
peripheral.eventSubscriptions.receive(.didDisconnect(error))
}
}

Expand All @@ -57,7 +57,7 @@ class CentralManagerDelegateWrapper: NSObject, CBCentralManagerDelegate {
parent.delegate?.centralManager(parent, didFailToConnect: peripheral, error: error)

parent.eventQueue.async {
parent.eventSubscriptions.recieve(.failToConnect(peripheral, error))
parent.eventSubscriptions.receive(.failToConnect(peripheral, error))
}
}

Expand All @@ -69,7 +69,7 @@ class CentralManagerDelegateWrapper: NSObject, CBCentralManagerDelegate {
parent.delegate?.centralManager(parent, didDiscover: peripheral, advertisementData: advertisementData, rssi: RSSI)

parent.eventQueue.async {
parent.eventSubscriptions.recieve(.discovered(peripheral, advertisementData, RSSI))
parent.eventSubscriptions.receive(.discovered(peripheral, advertisementData, RSSI))
}
}

Expand All @@ -79,7 +79,7 @@ class CentralManagerDelegateWrapper: NSObject, CBCentralManagerDelegate {
parent.delegate?.centralManager(parent, willRestoreState: dict)

parent.eventQueue.async {
parent.eventSubscriptions.recieve(.restoreState(dict))
parent.eventSubscriptions.receive(.restoreState(dict))
}
}

Expand Down
22 changes: 11 additions & 11 deletions Sources/SwiftBluetooth/Peripheral/PeripheralDelegateWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal final class PeripheralDelegateWrapper: NSObject, CBPeripheralDelegate {

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
guard let parent = parent else { return }
parent.eventSubscriptions.recieve(.discoveredServices(parent.services ?? [], error))
parent.eventSubscriptions.receive(.discoveredServices(parent.services ?? [], error))
parent.delegate?.peripheral(parent, didDiscoverServices: error)
}

Expand All @@ -29,23 +29,23 @@ internal final class PeripheralDelegateWrapper: NSObject, CBPeripheralDelegate {
parent.knownCharacteristics[characteristic.uuid] = characteristic
}

parent.eventSubscriptions.recieve(.discoveredCharacteristics(service, service.characteristics ?? [], error))
parent.eventSubscriptions.receive(.discoveredCharacteristics(service, service.characteristics ?? [], error))
parent.delegate?.peripheral(parent, didDiscoverCharacteristicsFor: service, error: error)
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: Error?) {
guard let parent = parent else { return }
parent.eventSubscriptions.recieve(.discoveredDescriptors(characteristic, characteristic.descriptors ?? [], error))
parent.eventSubscriptions.receive(.discoveredDescriptors(characteristic, characteristic.descriptors ?? [], error))
parent.delegate?.peripheral(parent, didDiscoverDescriptorsFor: characteristic, error: error)
}

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
guard let parent = parent else { return }

if let error {
parent.responseMap.recieve(key: characteristic.uuid, withValue: .failure(error))
parent.responseMap.receive(key: characteristic.uuid, withValue: .failure(error))
} else if let value = characteristic.value {
parent.responseMap.recieve(key: characteristic.uuid, withValue: .success(value))
parent.responseMap.receive(key: characteristic.uuid, withValue: .success(value))
}

parent.delegate?.peripheral(parent, didUpdateValueFor: characteristic, error: error)
Expand All @@ -55,9 +55,9 @@ internal final class PeripheralDelegateWrapper: NSObject, CBPeripheralDelegate {
guard let parent = parent else { return }

if let error {
parent.descriptorMap.recieve(key: descriptor.uuid, withValue: .failure(error))
parent.descriptorMap.receive(key: descriptor.uuid, withValue: .failure(error))
} else {
parent.descriptorMap.recieve(key: descriptor.uuid, withValue: .success(descriptor.value))
parent.descriptorMap.receive(key: descriptor.uuid, withValue: .success(descriptor.value))
}

parent.delegate?.peripheral(parent, didUpdateValueFor: descriptor, error: error)
Expand All @@ -66,26 +66,26 @@ internal final class PeripheralDelegateWrapper: NSObject, CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
guard let parent = parent else { return }

parent.writeMap.recieve(key: characteristic.uuid, withValue: error)
parent.writeMap.receive(key: characteristic.uuid, withValue: error)
parent.delegate?.peripheral(parent, didWriteValueFor: characteristic, error: error)
}

func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor: CBDescriptor, error: Error?) {
guard let parent = parent else { return }

parent.writeMap.recieve(key: descriptor.uuid, withValue: error)
parent.writeMap.receive(key: descriptor.uuid, withValue: error)
parent.delegate?.peripheral(parent, didWriteValueFor: descriptor, error: error)
}

func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: Error?) {
guard let parent = parent else { return }
parent.eventSubscriptions.recieve(.updateNotificationState(characteristic, error))
parent.eventSubscriptions.receive(.updateNotificationState(characteristic, error))
parent.delegate?.peripheral(parent, didUpdateNotificationStateFor: characteristic, error: error)
}

func peripheral(_ peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: Error?) {
guard let parent = parent else { return }
parent.eventSubscriptions.recieve(.readRSSI(RSSI, error))
parent.eventSubscriptions.receive(.readRSSI(RSSI, error))
parent.delegate?.peripheral(parent, didReadRSSI: RSSI, error: error)
}

Expand Down
7 changes: 4 additions & 3 deletions Tests/SwiftBluetoothTests/LostConnectionPeripheralTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ final class LostConnectionPeripheralTests: CentralPeripheralTestCase {
}

@available(iOS 13, macOS 10.15, watchOS 6.0, tvOS 13.0, *)
func testPeripheralSimultaniousDisconnectAndRead() async throws {
func testPeripheralSimultaneousDisconnectAndRead() async throws {
try await withTimeout { [self] in
try await central.waitUntilReady()
peripheral = await central.scanForPeripherals().first!
Expand Down Expand Up @@ -196,8 +196,9 @@ final class LostConnectionPeripheralTests: CentralPeripheralTestCase {
XCTAssertEqual(0, ran)
ran += 1
}
peripheral.responseMap.recieve(key: characteristic.uuid, withValue: .success(.init([0xFF])))
peripheral.eventSubscriptions.recieve(.didDisconnect(nil))

peripheral.responseMap.receive(key: characteristic.uuid, withValue: .success(.init([0xFF])))
peripheral.eventSubscriptions.receive(.didDisconnect(nil))

#if swift(>=5.8)
await self.fulfillment(of: [exp])
Expand Down