1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00

Move the handle idle command into the command queue

My theory is that it's ok to send the `noidle` synchronously but the
subsequent handling of the idle response should be on the command queue.
My hope is that this fixes issues with less than perfect network
connections (and also very occasionally seen on good networks).
This commit is contained in:
Daniel Barber 2021-11-14 21:31:58 -06:00 committed by Daniel Barber
parent fdec6664d7
commit ff88aa8f5e
3 changed files with 20 additions and 35 deletions

View File

@ -13,49 +13,36 @@ extension MPDClient {
func noIdle() { func noIdle() {
guard isConnected else { return } guard isConnected else { return }
do { if isIdle {
idleLock.lock() mpd_send_noidle(connection)
defer { idleLock.unlock() }
if isIdle {
mpd_send_noidle(connection)
isIdle = false
}
} }
} }
func idle(_ force: Bool = false) { func idle(_ force: Bool = false) {
guard isConnected else { return } guard isConnected else { return }
let shouldIdle: Bool let shouldIdle: Bool = (!isIdle && commandQueue.operationCount == 1) || force
do {
idleLock.lock()
defer { idleLock.unlock() }
shouldIdle = (!isIdle && commandQueue.operationCount == 1) || force
if shouldIdle {
mpd_send_idle(connection)
self.isIdle = true
}
}
if shouldIdle { if shouldIdle {
let result = mpd_recv_idle(connection, true) self.isIdle = true
handleIdleResult(result) mpd_send_idle(connection)
let handleIdleOperation = BlockOperation() { [unowned self] in
self.handleIdleResult()
}
handleIdleOperation.queuePriority = .high
commandQueue.addOperation(handleIdleOperation)
} }
} }
func handleIdleResult(_ result: mpd_idle) { func handleIdleResult() {
let result = mpd_recv_idle(connection, true)
let mpdIdle = MPDIdle(rawValue: result.rawValue) let mpdIdle = MPDIdle(rawValue: result.rawValue)
let wasIdle: Bool
do { isIdle = false
idleLock.lock()
defer { idleLock.unlock() }
wasIdle = isIdle
isIdle = false
}
if checkError() && wasIdle { if checkError() {
if mpdIdle.contains(.database) { if mpdIdle.contains(.database) {
self.fetchAllAlbums() self.fetchAllAlbums()
} }

View File

@ -22,8 +22,6 @@ class MPDClient {
let commandQueue = OperationQueue() let commandQueue = OperationQueue()
let idleLock = NSLock()
init(withDelegate delegate: MPDClientDelegate?) { init(withDelegate delegate: MPDClientDelegate?) {
commandQueue.maxConcurrentOperationCount = 1 commandQueue.maxConcurrentOperationCount = 1
self.delegate = delegate self.delegate = delegate