Files
swift-inotify/Tests/InotifyIntegrationTests/Utilities/getEventsForTrigger.swift
T. R. Bernstein 134e4e152d
Some checks failed
Docs / docs (push) Has been cancelled
Docs / deploy (push) Has been cancelled
Implement watch exclusion lists
Allow exclusion of directories when watching recursively.
2026-03-15 22:46:59 +01:00

42 lines
1.0 KiB
Swift

import Inotify
enum RecursivKind {
case nonrecursive
case recursive
case withAutomaticSubtreeWatching
}
func getEventsForTrigger(
in dir: String,
mask: InotifyEventMask,
recursive: RecursivKind = .nonrecursive,
exclude: [String] = [],
trigger: @escaping (String) async throws -> Void,
) async throws -> [InotifyEvent] {
let watcher = try Inotify()
await watcher.exclude(names: exclude)
switch recursive {
case .nonrecursive:
try await watcher.addWatch(path: dir, mask: mask)
case .recursive:
try await watcher.addRecursiveWatch(forDirectory: dir, mask: mask)
case .withAutomaticSubtreeWatching:
try await watcher.addWatchWithAutomaticSubtreeWatching(forDirectory: dir, mask: mask)
}
let eventTask = Task {
var events: [InotifyEvent] = []
for await event in await watcher.events {
events.append(event)
}
return events
}
try await Task.sleep(for: .milliseconds(100))
try await trigger(dir)
try await Task.sleep(for: .milliseconds(500))
eventTask.cancel()
return await eventTask.value
}