mirror of
https://github.com/maxgoedjen/secretive.git
synced 2025-12-14 21:10:56 +00:00
[refactor]: use sequence- rather than iterator-based iteration
This commit is contained in:
parent
8ed9f275af
commit
e25f2af224
@ -37,15 +37,13 @@ public struct SocketController {
|
|||||||
port = SocketPort(path: path)
|
port = SocketPort(path: path)
|
||||||
fileHandle = FileHandle(fileDescriptor: port.socket, closeOnDealloc: true)
|
fileHandle = FileHandle(fileDescriptor: port.socket, closeOnDealloc: true)
|
||||||
Task { @MainActor [fileHandle, sessionsContinuation, logger] in
|
Task { @MainActor [fileHandle, sessionsContinuation, logger] in
|
||||||
// Create the iterator before triggering the notification to
|
// Create the sequence before triggering the notification to
|
||||||
// ensure it will not be missed.
|
// ensure it will not be missed.
|
||||||
let connectionAvailableIterator = NotificationCenter.default
|
let connectionAcceptedNotifications = NotificationCenter.default.notifications(named: .NSFileHandleConnectionAccepted)
|
||||||
.notifications(named: .NSFileHandleConnectionAccepted)
|
|
||||||
.makeAsyncIterator()
|
|
||||||
|
|
||||||
fileHandle.acceptConnectionInBackgroundAndNotify()
|
fileHandle.acceptConnectionInBackgroundAndNotify()
|
||||||
|
|
||||||
while let notification = await connectionAvailableIterator.next() {
|
for await notification in connectionAcceptedNotifications {
|
||||||
logger.debug("Socket controller accepted connection")
|
logger.debug("Socket controller accepted connection")
|
||||||
guard let new = notification.userInfo?[NSFileHandleNotificationFileHandleItem] as? FileHandle else { continue }
|
guard let new = notification.userInfo?[NSFileHandleNotificationFileHandleItem] as? FileHandle else { continue }
|
||||||
let session = Session(fileHandle: new)
|
let session = Session(fileHandle: new)
|
||||||
@ -85,15 +83,13 @@ extension SocketController {
|
|||||||
provenance = SigningRequestTracer().provenance(from: fileHandle)
|
provenance = SigningRequestTracer().provenance(from: fileHandle)
|
||||||
(messages, messagesContinuation) = AsyncStream.makeStream()
|
(messages, messagesContinuation) = AsyncStream.makeStream()
|
||||||
Task { @MainActor [messagesContinuation, logger] in
|
Task { @MainActor [messagesContinuation, logger] in
|
||||||
// Create the iterator before triggering the notification to
|
// Create the sequence before triggering the notification to
|
||||||
// ensure it will not be missed.
|
// ensure it will not be missed.
|
||||||
let dataAvailableIterator = NotificationCenter.default
|
let dataAvailableNotifications = NotificationCenter.default.notifications(named: .NSFileHandleDataAvailable, object: fileHandle)
|
||||||
.notifications(named: .NSFileHandleDataAvailable, object: fileHandle)
|
|
||||||
.makeAsyncIterator()
|
|
||||||
|
|
||||||
fileHandle.waitForDataInBackgroundAndNotify()
|
fileHandle.waitForDataInBackgroundAndNotify()
|
||||||
|
|
||||||
while let _ = await dataAvailableIterator.next() {
|
for await _ in dataAvailableNotifications {
|
||||||
let data = fileHandle.availableData
|
let data = fileHandle.availableData
|
||||||
guard !data.isEmpty else {
|
guard !data.isEmpty else {
|
||||||
logger.debug("Socket controller received empty data, ending continuation.")
|
logger.debug("Socket controller received empty data, ending continuation.")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user