Skip to content

Commit 7fa6109

Browse files
committed
Added L2CAPSocket.event
1 parent 7747bec commit 7fa6109

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

Sources/BluetoothLinux/L2CAP/L2CAPSocket.swift

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public final class L2CAPSocket: Bluetooth.L2CAPSocket {
2525
/// L2CAP Socket address
2626
public let address: BluetoothAddress
2727

28+
public lazy var event: L2CAPSocketEventStream = { [unowned self] in
29+
let stream = self.socket.event
30+
var iterator = stream.makeAsyncIterator()
31+
return L2CAPSocketEventStream(unfolding: {
32+
await iterator
33+
.next()
34+
.map { L2CAPSocketEvent($0) }
35+
})
36+
}()
37+
2838
// MARK: - Initialization
2939

3040
deinit {
@@ -154,19 +164,6 @@ public final class L2CAPSocket: Bluetooth.L2CAPSocket {
154164

155165
// MARK: - Methods
156166

157-
/// Set an event handler for the socket.
158-
public func setEvent(_ eventHandler: @escaping ((L2CAPSocketEvent) async -> ())) async {
159-
do {
160-
try await socket.setEvent { socketEvent in
161-
let event = L2CAPSocketEvent(socketEvent)
162-
Task { await eventHandler(event) }
163-
}
164-
}
165-
catch {
166-
assertionFailure("Unable to set event")
167-
}
168-
}
169-
170167
/// Attempt to accept an incoming connection.
171168
public func accept() async throws -> L2CAPSocket {
172169
let (clientFileDescriptor, clientAddress) = try await socket.fileDescriptor.accept(L2CAPSocketAddress.self, sleep: 100_000_000)

0 commit comments

Comments
 (0)