Skip to content

Blinky Spec customization - #167

Open
philips77 wants to merge 2 commits into
mainfrom
improvement/specs
Open

philips77 wants to merge 2 commits into
mainfrom
improvement/specs

Conversation

@philips77

Copy link
Copy Markdown
Contributor

This PR adds a bit of more flexibility to the sample Blinky spec, added in #165.

It also replaces the mock implementation in MockPeripherals.swift to use the one from the library.

@philips77
philips77 requested a review from dinesharjani May 26, 2026 12:51
@dinesharjani
dinesharjani self-requested a review May 26, 2026 13:29
Comment on lines +61 to +62
///
/// Defaults to 1 second.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The 'default' comment here I think is not necessary. If the default is changed later, since it's not set here, we'll forget about it and have conflicting comments.

self.pressedDuration = pressedDuration
}

public func blinky(_ blinky: Blinky, didChangeLedState isOn: Bool) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be marked as open instead of public. The difference is, open will allow subclasses to override from outside the module it's defined in, so users of CoreBluetooth-Mock library. I think this is the intended behaviour.

Source: Swift Documentation


public func blinky(_ blinky: Blinky, didChangeButtonSubscriptionState isSubscribed: Bool) {
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

guard let self else { return }

Is enough.

// First fire after `period`, then repeat every `period`.
t.schedule(deadline: .now() + period, repeating: period)
t.setEventHandler { [weak self, weak blinky] in
guard let self = self, let blinky = blinky, self.timer != nil else { return }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

guard let self, let blinky, let timer else { return } (Perhaps let timer = self.timer might be needed instead)

Easier to read.

Comment on lines +118 to +121
guard let t = timer else { return }
t.setEventHandler {}
t.cancel()
timer = nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

defer {
   self.timer = nil
}
guard let timer else { return }
timer.setEventHandler {}
timer.cancel()

.build()

// Handle state changes.
weak let userDelegate = delegate

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be made a weak property instead of living on the stack.

Comment on lines +231 to +232
var ledStateDidChange: ((Bool) -> ())!
var buttonSubscriptionStateDidChange: ((Bool) -> ())!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please redesign:

  • These properties should not be forced optionals.
  • Alternate proposal: private protocol that Blinky class implements. Then have a weak pointer to this delegate in BlinkyImpl so the implementation stays private or internal.

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