Repository: CodeEditApp/CodeEdit
File: CodeEditModules/Modules/WorkspaceClient/src/Live.swift (Swift)

Code before:
                    newFileItem.parent = fileItem
                    flattenedFileItems[newFileItem.id] = newFileItem
                    newChildren?.append(newFileItem)
                }
            }

            newChildren?.forEach({
                try? rebuildFiles(fromItem: $0)
                flattenedFileItems[$0.id] = $0
            })

            fileItem.children = newChildren
            return
        }

        var sources: [String: DispatchSourceFileSystemObject] = [:]

        /// Function to apply listeners that rebuild the file index when the file system is changed.
        /// Optimised so that it only deletes/creates the needed listeners instead of replacing every one.
        func startListeningToDirectory() {
            // iterate over every item, checking if its a directory first
            for item in flattenedFileItems.values {
                // check if it actually exists, doesn't have a listener, and is a folder
                guard item.isFolder else { continue }
                guard !sources.keys.contains(item.id) else { continue }
                guard FileItem.fileManger.fileExists(atPath: item.url.path) else { continue }

                // open the folder to listen for changes
                let descriptor = open(item.url.path, O_EVTONLY)

                let source = DispatchSource.makeFileSystemObjectSource(
                    fileDescriptor: descriptor,
                    eventMask: .write,
                    queue: DispatchQueue.global()
                )

                source.setEventHandler {
                    // Something has changed inside the directory
                    // We should reload the files.
                    flattenedFileItems = [:]
                    flattenedFileItems[workspaceItem.id] = workspaceItem
                    try? rebuildFiles(fromItem: workspaceItem)
                    subject.send(workspaceItem.children ?? [])
                    startListeningToDirectory()
                    // reload data in outline view controller through the main thread
                    DispatchQueue.main.async { onRefresh() }
                }

                source.setCancelHandler {
                    close(descriptor)
                }

Review comment (suggestion):
Since you are not throwing any errors, i think you can better chain this guard.

```swift
guard item.isFolder,
           !sources.keys.contains(item.id),
           FileItem.fileManger.fileExists(atPath: item.url.path) else { continue }
```

Code after:
                    fileItem.children?.append(newFileItem)
                    didChangeSomething = true
                }
            }

            fileItem.children?.forEach({
                if $0.isFolder {
                    let childChanged = try? rebuildFiles(fromItem: $0)
                    didChangeSomething = (childChanged ?? false) ? true : didChangeSomething
                }
                flattenedFileItems[$0.id] = $0
            })

            return didChangeSomething
        }

        var sources: [String: DispatchSourceFileSystemObject] = [:]

        /// Function to apply listeners that rebuild the file index when the file system is changed.
        /// Optimised so that it only deletes/creates the needed listeners instead of replacing every one.
        func startListeningToDirectory() {
            // iterate over every item, checking if its a directory first
            for item in flattenedFileItems.values {
                // check if it actually exists, doesn't have a listener, and is a folder
                guard item.isFolder &&
                        !sources.keys.contains(item.id) &&
                        FileItem.fileManger.fileExists(atPath: item.url.path) else { continue }

                let descriptor = open(item.url.path, O_EVTONLY) // open the folder to listen for changes
                let source = DispatchSource.makeFileSystemObjectSource(
                    fileDescriptor: descriptor,
                    eventMask: .write,
                    queue: DispatchQueue.global()
                )

                source.setEventHandler {
                    // Something has changed inside the directory
                    // We should reload the files.
                    guard !isRunning else { // this runs when a file change is detected but is already running
                        anotherInstanceRan += 1
                        return
                    }
                    isRunning = true
                    flattenedFileItems = [workspaceItem.id: workspaceItem]
                    _ = try? rebuildFiles(fromItem: workspaceItem)
                    while anotherInstanceRan > 0 { // TODO: optimise
                        let somethingChanged = try? rebuildFiles(fromItem: workspaceItem)
                        anotherInstanceRan = !(somethingChanged ?? false) ? 0 : anotherInstanceRan - 1
                    }
                    subject.send(workspaceItem.children ?? [])
                    startListeningToDirectory()