I just want to start off by saying thanks for the hard work in making this API. It is truly taking the NIO/Swift Concurrency bridge closer to a happier place.
I started trying to implement this API into my Channel Handler in order to keep messages in order and receive them one at a time to feed to my other Swift Concurrent handlers. This particular Handler is designer to take IRC protocol lines and parse them before feeding them to be handled else where. It becomes sticky while spawning tasks with in an event loop and also working with the context to write and flush. So this API really is perfect to use in the given situation as coded bellow. I am not sure if any other Libraries have adopted it yet, but here is a first pass at my implementation working with the Test Case code.
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
self.logger.info("AsyncMessageChannelHandler Read")
var buffer = self.unwrapInboundIn(data)
guard let lines = buffer.readString(length: buffer.readableBytes) else { return }
guard !lines.isEmpty else { return }
let messages = lines.components(separatedBy: "\n")
.map { $0.replacingOccurrences(of: "\r", with: "") }
.filter{ $0 != ""}
_ = self.source.yield(contentsOf: messages)
_ = context.eventLoop.executeAsync {
func runIterator() async throws {
guard let result = try await self.iterator?.next() else { throw NeedleTailError.parsingError }
let parsedMessage = try await AsyncMessageTask.parseMessageTask(task: result, messageParser: self.parser)
try await self.writer.yield(parsedMessage)
}
try await runIterator()
if self.delegate.produceMoreCallCount != 0 {
try await runIterator()
}
}
channelRead(context: context)
}
private func channelRead(context: ChannelHandlerContext) {
let promise = context.eventLoop.makePromise(of: Deque<IRCMessage>.self)
self.writerDelegate.didYieldHandler = { deq in
promise.succeed(deq)
}
promise.futureResult.whenSuccess { messages in
for message in messages {
let wioValue = self.wrapInboundOut(message)
context.fireChannelRead(wioValue)
}
}
}
I am curious to know in the use case as mentioned above, do I need to be concerned about handling the yield result or something else that I may seem to not understand about the API?
Thank you for your expertise.
Cole