Skip to content

Commit f48f578

Browse files
committed
feat: Refactor STFolderWatcher and fix warnings
Refactored the `checkFolderChanges` function in `STFolderWatcher.swift` to improve the logic for detecting file changes, additions, and deletions. This new implementation correctly handles file modifications and prevents them from being misidentified as deletions. Additionally, this commit resolves compiler warnings in `STFolderTests.swift` by removing unused variable assignments, ensuring a cleaner and more efficient test suite.
1 parent f8d9ba3 commit f48f578

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

Sources/STFilePath/STFolderWatcher.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -147,30 +147,30 @@ public class STFolderWatcher {
147147
_stream = nil
148148
}
149149

150-
// Private Method to Check Folder Changes
151150
private func checkFolderChanges() {
152151
do {
153-
let files = try folder.allSubFilePaths().compactMap(\.asFile)
154-
155-
for file in files {
156-
if let modificationDate = previousContents[file] {
157-
if file.attributes.modificationDate != modificationDate {
158-
let change = Changed(kind: .changed, file: file)
159-
continuation?.yield(change)
152+
let currentFiles = try folder.allSubFilePaths().compactMap(\.asFile)
153+
let currentFilesSet = Set(currentFiles)
154+
let previousFilesSet = Set(previousContents.keys)
155+
156+
// Check for deletions
157+
for file in previousFilesSet.subtracting(currentFilesSet) {
158+
continuation?.yield(Changed(kind: .deleted, file: file))
159+
previousContents.removeValue(forKey: file)
160+
}
161+
162+
// Check for additions and modifications
163+
for file in currentFiles {
164+
if let oldModificationDate = previousContents[file] {
165+
if file.attributes.modificationDate != oldModificationDate {
166+
continuation?.yield(Changed(kind: .changed, file: file))
160167
previousContents[file] = file.attributes.modificationDate
161168
}
162169
} else {
163-
let change = Changed(kind: .added, file: file)
164-
continuation?.yield(change)
170+
continuation?.yield(Changed(kind: .added, file: file))
165171
previousContents[file] = file.attributes.modificationDate
166172
}
167173
}
168-
169-
Set(previousContents.keys).subtracting(files).forEach { file in
170-
let change = Changed(kind: .deleted, file: file)
171-
continuation?.yield(change)
172-
previousContents[file] = nil
173-
}
174174
} catch {
175175
continuation?.finish(throwing: error)
176176
}

Tests/STFilePathTests/STFolderTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ struct STFolderTests {
3636
let testFolder = try createTestFolder()
3737
try testFolder.create()
3838
let subfolder = try testFolder.create(folder: "subfolder")
39-
let file1 = try testFolder.create(file: "file1.txt")
40-
let file2 = try subfolder.create(file: "file2.txt")
39+
try testFolder.create(file: "file1.txt")
40+
try subfolder.create(file: "file2.txt")
4141
try testFolder.create(file: ".hidden.txt")
4242

4343
let allPaths = try testFolder.allSubFilePaths()

0 commit comments

Comments
 (0)