Use a coalescing queue (instead of a Timer) in Account for saving to disk.
This commit is contained in:
parent
c8fa1b71d9
commit
64647c73c6
|
@ -56,23 +56,12 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
|
||||||
let database: Database
|
let database: Database
|
||||||
let delegate: AccountDelegate
|
let delegate: AccountDelegate
|
||||||
var username: String?
|
var username: String?
|
||||||
var saveTimer: Timer?
|
static let saveQueue = CoalescingQueue(name: "Account Save Queue", interval: 1.0)
|
||||||
|
|
||||||
public var dirty = false {
|
public var dirty = false {
|
||||||
didSet {
|
didSet {
|
||||||
|
|
||||||
if refreshInProgress {
|
|
||||||
if let _ = saveTimer {
|
|
||||||
removeSaveTimer()
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if dirty {
|
if dirty {
|
||||||
resetSaveTimer()
|
Account.saveQueue.add(self, #selector(saveToDiskIfNeeded))
|
||||||
}
|
|
||||||
else {
|
|
||||||
removeSaveTimer()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,9 +82,6 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
NotificationCenter.default.post(name: .AccountRefreshDidFinish, object: self)
|
NotificationCenter.default.post(name: .AccountRefreshDidFinish, object: self)
|
||||||
if dirty {
|
|
||||||
resetSaveTimer()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -470,6 +456,15 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@objc func saveToDiskIfNeeded() {
|
||||||
|
|
||||||
|
guard dirty else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
saveToDisk()
|
||||||
|
dirty = false
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Equatable
|
// MARK: - Equatable
|
||||||
|
|
||||||
public class func ==(lhs: Account, rhs: Account) -> Bool {
|
public class func ==(lhs: Account, rhs: Account) -> Bool {
|
||||||
|
@ -553,21 +548,6 @@ private extension Account {
|
||||||
return d as NSDictionary
|
return d as NSDictionary
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveToDiskIfNeeded() {
|
|
||||||
|
|
||||||
if !dirty {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if refreshInProgress {
|
|
||||||
resetSaveTimer()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
saveToDisk()
|
|
||||||
dirty = false
|
|
||||||
}
|
|
||||||
|
|
||||||
func saveToDisk() {
|
func saveToDisk() {
|
||||||
|
|
||||||
let d = diskDictionary()
|
let d = diskDictionary()
|
||||||
|
@ -578,21 +558,6 @@ private extension Account {
|
||||||
NSApplication.shared.presentError(error)
|
NSApplication.shared.presentError(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func resetSaveTimer() {
|
|
||||||
|
|
||||||
saveTimer?.rs_invalidateIfValid()
|
|
||||||
|
|
||||||
saveTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { (timer) in
|
|
||||||
self.saveToDiskIfNeeded()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func removeSaveTimer() {
|
|
||||||
|
|
||||||
saveTimer?.rs_invalidateIfValid()
|
|
||||||
saveTimer = nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Private
|
// MARK: - Private
|
||||||
|
|
Loading…
Reference in New Issue