generated from StanfordBDHG/SwiftPackageTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement onChange for @characteristic
- Loading branch information
Showing
13 changed files
with
212 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
Sources/SpeziBluetooth/Model/Characteristic/NotificationRegistrar.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
/// Tracking notification closure registrations for ``Characteristic`` when peripheral is not available yet. | ||
final class NotificationRegistrar { | ||
struct Entry<Value> { | ||
let closure: (Value) -> Void | ||
} | ||
|
||
// task local value ensures nobody is interfering here and resolves thread safety | ||
@TaskLocal static var instance: NotificationRegistrar? | ||
|
||
|
||
private var registrations: [ObjectIdentifier: Any] = [:] // TODO: figure out the type! | ||
|
||
init() {} | ||
|
||
func insert<Value>(for information: Characteristic<Value>.Information, closure: @escaping (Value) -> Void) { | ||
registrations[information.objectId] = Entry(closure: closure) | ||
} | ||
|
||
func retrieve<Value>(for information: Characteristic<Value>.Information) -> ((Value) -> Void)? { | ||
guard let optionalEntry = registrations[information.objectId], | ||
let entry = optionalEntry as? Entry<Value> else { | ||
return nil | ||
} | ||
return entry.closure | ||
} | ||
} | ||
|
||
|
||
extension Characteristic.Information { | ||
/// Memory address as an identifier for this Characteristic instance. | ||
fileprivate var objectId: ObjectIdentifier { | ||
ObjectIdentifier(self) | ||
} | ||
} |
Oops, something went wrong.