diff --git a/RxFileMonitor.xcodeproj/project.pbxproj b/RxFileMonitor.xcodeproj/project.pbxproj index c32bedd..f8b183f 100644 --- a/RxFileMonitor.xcodeproj/project.pbxproj +++ b/RxFileMonitor.xcodeproj/project.pbxproj @@ -20,7 +20,6 @@ 50E835641DD1B28400783B62 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50E835631DD1B28400783B62 /* AppDelegate.swift */; }; 50E835661DD1B28400783B62 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 50E835651DD1B28400783B62 /* Assets.xcassets */; }; 50E835691DD1B28400783B62 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 50E835671DD1B28400783B62 /* MainMenu.xib */; }; - 50E8356F1DD1BAEE00783B62 /* FileMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50E8356E1DD1BAEE00783B62 /* FileMonitor.swift */; }; 50E835701DD1BE2700783B62 /* RxFileMonitor.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 50E835431DD1B26900783B62 /* RxFileMonitor.framework */; }; 50E835711DD1BE2700783B62 /* RxFileMonitor.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 50E835431DD1B26900783B62 /* RxFileMonitor.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 50E835761DD1D1C200783B62 /* FolderContentMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50E835751DD1D1C200783B62 /* FolderContentMonitor.swift */; }; @@ -76,7 +75,6 @@ 50E835651DD1B28400783B62 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 50E835681DD1B28400783B62 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/MainMenu.xib; sourceTree = ""; }; 50E8356A1DD1B28400783B62 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 50E8356E1DD1BAEE00783B62 /* FileMonitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileMonitor.swift; sourceTree = ""; }; 50E835751DD1D1C200783B62 /* FolderContentMonitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FolderContentMonitor.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -113,7 +111,6 @@ 5096B2BD1DD222D000076058 /* Monitors */ = { isa = PBXGroup; children = ( - 50E8356E1DD1BAEE00783B62 /* FileMonitor.swift */, 50E835751DD1D1C200783B62 /* FolderContentMonitor.swift */, 5096B2B41DD21D0B00076058 /* Change.swift */, 5096B2C01DD22D6000076058 /* FolderContentChangeEvent.swift */, @@ -326,7 +323,6 @@ 50E835761DD1D1C200783B62 /* FolderContentMonitor.swift in Sources */, 5096B2C11DD22D6000076058 /* FolderContentChangeEvent.swift in Sources */, 5096B2BC1DD221D700076058 /* RxFileMonitor.swift in Sources */, - 50E8356F1DD1BAEE00783B62 /* FileMonitor.swift in Sources */, 5096B2B51DD21D0B00076058 /* Change.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/RxFileMonitor/FileMonitor.swift b/RxFileMonitor/FileMonitor.swift deleted file mode 100644 index 05aeddf..0000000 --- a/RxFileMonitor/FileMonitor.swift +++ /dev/null @@ -1,93 +0,0 @@ -// -// FileMonitor.swift -// RxFileMonitor -// -// Created by Christian Tietze on 08/11/16. -// Copyright © 2016 RxSwiftCommunity https://github.com/RxSwiftCommunity -// - -import Foundation - -/// Monitor for a particular file or folder. Change events -/// will fire when the contents of the URL changes: -/// -/// - If it's a folder, it will fire when you add/remove/rename files. -/// - If it's a file, it will fire when you change its contents, -/// remove, or rename it. -/// -/// The URL will not update if you rename the file, though. -public class FileMonitor { - - public struct ChangeEvent { - public let url: URL - } - - public static let monitorQueue = DispatchQueue(label: "org.rxswift.rxfilemonitor.monitorqueue", qos: .background, attributes: [.concurrent]) - - var monitoredFileDescriptor: Int32? - var monitorSource: DispatchSourceFileSystemObject? - - let url: URL - let callback: (ChangeEvent) -> Void - - /// Set up a new monitor. You have to call `start()` first - /// to make it work. - /// - /// - parameter url: File or folder to monitor for changes. - /// - parameter callback: Will be called when a change event fires. - /// - /// If it's a folder, it will fire when you add/remove/rename files. - /// - /// If it's a file, it will fire when you change its contents, - /// remove, or rename it. - /// - /// - note: Renaming files will not fire events with the - /// new URL. - public init(url: URL, callback: @escaping (ChangeEvent) -> Void) { - - self.url = url - self.callback = callback - } - - deinit { - stop() - } - - public func start() { - - guard self.monitorSource == nil && self.monitoredFileDescriptor == nil else { - assertionFailure("Starting monitor twice is not supported") - return - } - - let fileDescriptor = open(url.path, O_EVTONLY) - let monitorSource = DispatchSource.makeFileSystemObjectSource( - fileDescriptor: fileDescriptor, - eventMask: DispatchSource.FileSystemEvent.all, - queue: FileMonitor.monitorQueue) - - monitorSource.setEventHandler { [weak self] in - self?.didObserveChange() - } - - monitorSource.setCancelHandler { - self.monitoredFileDescriptor = nil - self.monitorSource = nil - } - - self.monitorSource = monitorSource - self.monitoredFileDescriptor = fileDescriptor - - monitorSource.resume() - } - - private func didObserveChange() { - - self.callback(ChangeEvent(url: self.url)) - } - - public func stop() { - - monitorSource?.cancel() - } -} diff --git a/RxFileMonitor/FolderContentMonitor.swift b/RxFileMonitor/FolderContentMonitor.swift index 2b88334..296ff01 100644 --- a/RxFileMonitor/FolderContentMonitor.swift +++ b/RxFileMonitor/FolderContentMonitor.swift @@ -8,6 +8,12 @@ import Foundation +/// Monitor for a particular file or folder. Change events +/// will fire when the contents of the URL changes: +/// +/// If it's a folder, it will fire when you add/remove/rename files or folders +/// below the reference paths. See `Change` for an incomprehensive list of +/// events details that will be reported. public class FolderContentMonitor { var callback: ((FolderContentChangeEvent) -> Void)?