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

Add handler for peripheral connection cancelled #42

Merged
merged 3 commits into from
Apr 11, 2022

Conversation

filip-zielinski
Copy link
Collaborator

Title

Add handler for peripheral connection cancelled.

Motivation

A project I'm working on needs to be notified when disconnecting a peripheral completes (so after calling disconnect(_:), when CoreBluetooth completes disconnecting).

Task Description

  • add public peripheralConnectionCancelledHandler(_:) settable property
  • refactor from .filter(_:).first to first(where:) for optimization
  • bump version to 1.0.6
  • add project change log

- add public `peripheralConnectionCancelledHandler(_:)` settable property
- refactor from `.filter(_:).first` to `first(where:)` for optimisation
- bump version to 1.0.6
- add project change log
@@ -76,7 +82,7 @@ extension ConnectionService {
/// Disconnects given device.
internal func disconnect(_ peripheral: CBPeripheral) {
if let index = peripherals.firstIndex(where: { $0.peripheral === peripheral }) {
peripherals.remove(at: index)
peripheralsToDisconnect.append(peripherals.remove(at: index))
Copy link

Choose a reason for hiding this comment

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

WDYT to extract peripherals.remove(at: index) to separate variable which could be more readable what is actually going on in here.

/// `error` is nil if disconnect resulted from a call to `cancelPeripheralConnection(_:)`.
/// SeeAlso: https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerdelegate/1518791-centralmanager
if error == nil,
let disconnectedPeripheral = peripheralsToDisconnect.first(where: { $0.peripheral === peripheral }) {
Copy link

Choose a reason for hiding this comment

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

peripheralsToDisconnect.first(where: { $0.peripheral === peripheral }) is called twice. WDYT to extract it to separate variable which cloud be used in both ifs ? :)

Filip Zieliński added 2 commits April 10, 2022 12:40
…nce with `peripheral` property identical to given `CBPeripheral` instance
@filip-zielinski filip-zielinski requested a review from dymb April 10, 2022 11:35
Copy link

@dymb dymb left a comment

Choose a reason for hiding this comment

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

👍 nice

@filip-zielinski filip-zielinski merged commit 3d8e5d5 into master Apr 11, 2022
@filip-zielinski filip-zielinski deleted the Add-handler-for-connection-cancelled-manually branch April 11, 2022 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants