-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add objective-c interoperability (#19)
* Add objective-c interoperability * Add parameter to delegates method. * Fix comment
- Loading branch information
1 parent
7b0ecf6
commit 8545c9b
Showing
7 changed files
with
247 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// FileWatcherProtocols.swift | ||
// KZFileWatchers | ||
// | ||
// Created by Andrey Morozov on 18/12/2018. | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
Protocol for essence, that responsible for file refresh processing. | ||
*/ | ||
@objc(FileWatcherDelegate) | ||
public protocol FileWatcherOutput: NSObjectProtocol { | ||
|
||
/// Fires, when monitoring file is updated. | ||
/// | ||
/// - Parameters: | ||
/// - fileWatcher: The file watcher, which handles file refreshing. | ||
/// - type: Status of refresh result. | ||
/// - data: Content from file. | ||
func refreshDidOccur(from fileWatcher: FileWatcherInput, type: RefreshResultType, data: Data?) | ||
} | ||
|
||
/** | ||
Protocol for File Watchers. | ||
*/ | ||
@objc(FileWatcherProtocol) | ||
public protocol FileWatcherInput: NSObjectProtocol { | ||
|
||
/// Responsible for file refresh processing. | ||
var delegate: FileWatcherOutput? { get set } | ||
|
||
/** | ||
Starts observing file changes, a file watcher can only have one callback. | ||
*/ | ||
@objc(startWithError:) | ||
func start() throws | ||
|
||
/** | ||
Stops observing file changes. | ||
*/ | ||
@objc(stopWithError:) | ||
func stop() throws | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// LocalFileWatcher.swift | ||
// KZFileWatchers | ||
// | ||
// Created by Andrey Morozov on 18/12/2018. | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
Watcher for local files, it uses content diffing. | ||
*/ | ||
@objc | ||
public final class LocalFileWatcher: NSObject { | ||
|
||
/// Original local FileWatcher, that does all work. | ||
private let localWatcher: FileWatcher.Local | ||
|
||
public weak var delegate: FileWatcherOutput? | ||
|
||
/** | ||
Initializes watcher to specified path. | ||
- parameter path: Path of file to observe. | ||
- note: By default it throttles to 60 FPS, some editors can generate stupid multiple saves that mess with file system e.g. Sublime with AutoSave plugin is a mess and generates different file sizes, this will limit wasted time trying to load faster than 60 FPS, and no one should even notice it's throttled. | ||
*/ | ||
@objc | ||
public convenience init(path: String) { | ||
self.init(path: path, refreshInterval: 1/60, queue: DispatchQueue.main) | ||
} | ||
|
||
/** | ||
Initializes watcher to specified path. | ||
- parameter path: Path of file to observe. | ||
- parameter refreshInterval: Refresh interval to use for updates. | ||
- parameter queue: Queue to use for firing `onChange` callback. | ||
*/ | ||
@objc | ||
public init(path: String, refreshInterval: TimeInterval, queue: DispatchQueue) { | ||
localWatcher = FileWatcher.Local(path: path, refreshInterval: refreshInterval, queue: queue) | ||
} | ||
|
||
@objc | ||
public func refresh() { | ||
localWatcher.refresh() | ||
} | ||
} | ||
|
||
extension LocalFileWatcher: FileWatcherInput { | ||
|
||
public func start() throws { | ||
try localWatcher.start { (result) in | ||
switch result { | ||
case .noChanges: | ||
self.delegate?.refreshDidOccur(from: self, type: .noChanges, data: nil) | ||
case .updated(let data): | ||
self.delegate?.refreshDidOccur(from: self, type: .updated, data: data) | ||
} | ||
} | ||
} | ||
|
||
public func stop() throws { | ||
try localWatcher.stop() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// RefreshResultType.swift | ||
// KZFileWatchers | ||
// | ||
// Created by Andrey Morozov on 18/12/2018. | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
Enum that contains status of refresh result. | ||
*/ | ||
@objc(RefreshResult) | ||
public enum RefreshResultType: Int { | ||
/** | ||
Watched file didn't change since last update. | ||
*/ | ||
case noChanges | ||
|
||
/** | ||
Watched file did change. | ||
*/ | ||
case updated | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// RemoteFileWatcher.swift | ||
// KZFileWatchers | ||
// | ||
// Created by Andrey Morozov on 18/12/2018. | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
Watcher for remote files, it supports both ETag and Last-Modified HTTP header tags. | ||
*/ | ||
@objc | ||
public final class RemoteFileWatcher: NSObject { | ||
|
||
/// Original remote FileWatcher, that does all work. | ||
private let remoteWatcher: FileWatcher.Remote | ||
|
||
public weak var delegate: FileWatcherOutput? | ||
|
||
/** | ||
Creates a new watcher using given URL and refreshInterval. | ||
- parameter url: URL to observe. | ||
- note: By default, it uses refreshInterval equal to 1. | ||
*/ | ||
@objc | ||
public convenience init(url: URL) { | ||
self.init(url: url, refreshInterval: 1) | ||
} | ||
|
||
/** | ||
Creates a new watcher using given URL and refreshInterval. | ||
- parameter url: URL to observe. | ||
- parameter refreshInterval: Minimal refresh interval between queries. | ||
*/ | ||
@objc | ||
public init(url: URL, refreshInterval: TimeInterval) { | ||
remoteWatcher = FileWatcher.Remote(url: url, refreshInterval: refreshInterval) | ||
} | ||
|
||
@objc | ||
public func refresh() throws { | ||
try remoteWatcher.refresh() | ||
} | ||
} | ||
|
||
extension RemoteFileWatcher: FileWatcherInput { | ||
|
||
public func start() throws { | ||
try remoteWatcher.start { (result) in | ||
switch result { | ||
case .noChanges: | ||
self.delegate?.refreshDidOccur(from: self, type: .noChanges, data: nil) | ||
case .updated(let data): | ||
self.delegate?.refreshDidOccur(from: self, type: .updated, data: data) | ||
} | ||
} | ||
} | ||
|
||
public func stop() throws { | ||
try remoteWatcher.stop() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters