From 7004f483277a1c4609783c9b001f022b7578f2a4 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Wed, 13 Nov 2024 18:51:39 -0500 Subject: [PATCH] Fixed `AsyncCentralScan` --- Sources/GATT/AsyncStream.swift | 63 +++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/Sources/GATT/AsyncStream.swift b/Sources/GATT/AsyncStream.swift index d6ce1fa..84dbb96 100644 --- a/Sources/GATT/AsyncStream.swift +++ b/Sources/GATT/AsyncStream.swift @@ -6,31 +6,40 @@ // #if !hasFeature(Embedded) +import Foundation import Bluetooth -import AsyncAlgorithms public struct AsyncCentralScan : AsyncSequence { public typealias Element = ScanData - public typealias Channel = AsyncThrowingChannel + let stream: AsyncIndefiniteStream - let channel: Channel - - public init() { - self.channel = .init() + public init( + bufferSize: Int = 100, + _ build: @escaping @Sendable ((Element) -> ()) async throws -> () + ) { + self.stream = .init(bufferSize: bufferSize, build) } - public func append(_ element: Element) async { - await channel.send(element) + public init( + bufferSize: Int = 100, + onTermination: @escaping () -> (), + _ build: (AsyncIndefiniteStream.Continuation) -> () + ) { + self.stream = .init(bufferSize: bufferSize, onTermination: onTermination, build) } - public func makeAsyncIterator() -> Channel.AsyncIterator { - channel.makeAsyncIterator() + public func makeAsyncIterator() -> AsyncIndefiniteStream.AsyncIterator { + stream.makeAsyncIterator() } public func stop() { - channel.finish() + stream.stop() + } + + public var isScanning: Bool { + return stream.isExecuting } } @@ -47,27 +56,35 @@ public extension AsyncCentralScan { public struct AsyncCentralNotifications : AsyncSequence { - public typealias Element = Central.Data - - public typealias Channel = AsyncThrowingChannel + public typealias Element = Data - let channel: Channel + let stream: AsyncIndefiniteStream - public init() { - self.channel = .init() + public init( + bufferSize: Int = 100, + _ build: @escaping @Sendable ((Element) -> ()) async throws -> () + ) { + self.stream = .init(bufferSize: bufferSize, build) } - public func append(_ element: Element) async { - await channel.send(element) + public init( + bufferSize: Int = 100, + onTermination: @escaping () -> (), + _ build: (AsyncIndefiniteStream.Continuation) -> () + ) { + self.stream = .init(bufferSize: bufferSize, onTermination: onTermination, build) } - public func makeAsyncIterator() -> Channel.AsyncIterator { - channel.makeAsyncIterator() + public func makeAsyncIterator() -> AsyncIndefiniteStream.AsyncIterator { + stream.makeAsyncIterator() } public func stop() { - channel.finish() + stream.stop() + } + + public var isNotifying: Bool { + return stream.isExecuting } } - #endif