Save to disk when needed. Coalesce writing, so it doesn’t happen over and over during a refresh session.

This commit is contained in:
Brent Simmons 2017-10-07 18:15:42 -07:00
parent bed43dd3d6
commit ade76d6405

View File

@ -44,6 +44,24 @@ public final class Account: DisplayNameProvider, Hashable {
var topLevelObjects = [AnyObject]()
var feedIDDictionary = [String: Feed]()
var username: String?
var saveTimer: Timer?
private var dirty = false {
didSet {
if dirty {
saveTimer?.rs_invalidateIfValid()
saveTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { (timer) in
self.saveToDiskIfNeeded()
timer.rs_invalidateIfValid()
self.saveTimer = nil
}
}
else if !dirty {
saveTimer?.rs_invalidateIfValid()
saveTimer = nil
}
}
}
var refreshInProgress = false {
didSet {
@ -237,6 +255,44 @@ extension Account {
}
return Folder(account: self, dictionary: diskObject)
}
func saveToDiskIfNeeded() {
if dirty {
saveToDisk()
}
}
private func diskDictionary() -> NSDictionary {
let diskObjects = topLevelObjects.flatMap { (object) -> [String: Any]? in
if let folder = object as? Folder {
return folder.dictionary
}
else if let feed = object as? Feed {
return feed.dictionary
}
return nil
}
var d = [String: Any]()
d[Key.children] = diskObjects as NSArray
return d as NSDictionary
}
func saveToDisk() {
let d = diskDictionary()
do {
try RSPlist.write(d, filePath: settingsFile)
}
catch let error as NSError {
NSApplication.shared.presentError(error)
}
dirty = false
}
}
// MARK: - Private