Skip to content

Commit

Permalink
Some clean-ups.
Browse files Browse the repository at this point in the history
  • Loading branch information
luosheng committed Jun 22, 2016
1 parent ae67613 commit b612d92
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
20 changes: 6 additions & 14 deletions OpenSim/DirectoryWatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class DirectoryWatcher {

enum Error: ErrorProtocol {
case cannotOpenPath
case cannotCreateSource
}

public typealias CompletionCallback = () -> ()
Expand All @@ -25,8 +24,8 @@ public class DirectoryWatcher {
private var directoryChanging = false
private var oldDirectoryInfo = [FileInfo?]()

init(URL: Foundation.URL, eventMask: DispatchSource.FileSystemEvent = .write) {
watchedURL = URL
init(in watchedURL: URL, eventMask: DispatchSource.FileSystemEvent = .write) {
self.watchedURL = watchedURL
self.eventMask = eventMask
}

Expand All @@ -48,35 +47,28 @@ public class DirectoryWatcher {
throw Error.cannotOpenPath
}

let cleanUp: () -> () = {
close(fd)
}

source = DispatchSource.fileSystemObject(fileDescriptor: fd, eventMask: eventMask, queue: queue)

source?.setEventHandler { [weak self] in
self?.waitForDirectoryToFinishChanging()
}

source?.setCancelHandler(handler: cleanUp)
source?.setCancelHandler {
close(fd)
}

source?.resume()
}

public func stop() {
guard let src = source else {
return
}

src.cancel()
source?.cancel()
}

private func waitForDirectoryToFinishChanging() {
if (!directoryChanging) {
directoryChanging = true

oldDirectoryInfo = self.directoryInfo()
// print(oldDirectoryInfo)

let timer = Timer(timeInterval: 0.5, target: self, selector: #selector(checkDirectoryInfo(_:)), userInfo: nil, repeats: true)
RunLoop.main().add(timer, forMode: RunLoopMode.commonModes)
Expand Down
4 changes: 2 additions & 2 deletions OpenSim/MenuManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protocol MenuManagerDelegate {
}

private func buildWatcher() {
watcher = DirectoryWatcher(URL: URLHelper.deviceURL)
watcher = DirectoryWatcher(in: URLHelper.deviceURL)
watcher.completionCallback = {
self.reloadWhenReady()
self.buildSubWatchers()
Expand All @@ -121,7 +121,7 @@ protocol MenuManagerDelegate {
guard let info = FileInfo(URL: URL) where info.isDirectory else {
return nil
}
let watcher = DirectoryWatcher(URL: URL)
let watcher = DirectoryWatcher(in: URL)
watcher.completionCallback = { [weak self] in
self?.reloadWhenReady()
}
Expand Down

0 comments on commit b612d92

Please sign in to comment.