mirror of
https://github.com/danbee/persephone
synced 2025-03-04 08:39:11 +00:00
Address PR feedback
* Use the OperationQueue's `operationCount` function instead of keeping count ourselves. This is reliable now each command is entirely self contained. * Rename `queueCommand` to `enqueueCommand` * Move the command Enum into its own model file * Move the `enqueueCommand` function into MPDClient+Command
This commit is contained in:
parent
9714aabb10
commit
11be238788
@ -53,6 +53,7 @@
|
||||
E450ADA12229E7C90091BED3 /* PMKFoundation.framework.dSYM in Resources */ = {isa = PBXBuildFile; fileRef = E450AD9F2229E7C90091BED3 /* PMKFoundation.framework.dSYM */; };
|
||||
E450ADA32229E7E00091BED3 /* PMKFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E450ADA02229E7C90091BED3 /* PMKFoundation.framework */; };
|
||||
E450ADA42229E7E00091BED3 /* PMKFoundation.framework in Embed Libraries */ = {isa = PBXBuildFile; fileRef = E450ADA02229E7C90091BED3 /* PMKFoundation.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
|
||||
E45962C62241A78500FC1A1E /* Command.swift in Sources */ = {isa = PBXBuildFile; fileRef = E45962C52241A78500FC1A1E /* Command.swift */; };
|
||||
E465049A21E94DF500A70F4C /* WindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E465049921E94DF500A70F4C /* WindowController.swift */; };
|
||||
E47E2FCC2220573500F747E6 /* MediaKeyTap.framework.dSYM in Resources */ = {isa = PBXBuildFile; fileRef = E47E2FCB2220573500F747E6 /* MediaKeyTap.framework.dSYM */; };
|
||||
E47E2FD122205C4600F747E6 /* MainSplitViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E47E2FD022205C4600F747E6 /* MainSplitViewController.swift */; };
|
||||
@ -210,6 +211,7 @@
|
||||
E450AD9E2229B9BC0091BED3 /* PersephoneBridgingHeader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PersephoneBridgingHeader.h; sourceTree = "<group>"; };
|
||||
E450AD9F2229E7C90091BED3 /* PMKFoundation.framework.dSYM */ = {isa = PBXFileReference; lastKnownFileType = wrapper.dsym; name = PMKFoundation.framework.dSYM; path = Carthage/Build/Mac/PMKFoundation.framework.dSYM; sourceTree = "<group>"; };
|
||||
E450ADA02229E7C90091BED3 /* PMKFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = PMKFoundation.framework; path = Carthage/Build/Mac/PMKFoundation.framework; sourceTree = "<group>"; };
|
||||
E45962C52241A78500FC1A1E /* Command.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Command.swift; sourceTree = "<group>"; };
|
||||
E465049921E94DF500A70F4C /* WindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowController.swift; sourceTree = "<group>"; };
|
||||
E47E2FCB2220573500F747E6 /* MediaKeyTap.framework.dSYM */ = {isa = PBXFileReference; lastKnownFileType = wrapper.dsym; name = MediaKeyTap.framework.dSYM; path = Carthage/Build/Mac/MediaKeyTap.framework.dSYM; sourceTree = "<group>"; };
|
||||
E47E2FD022205C4600F747E6 /* MainSplitViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainSplitViewController.swift; sourceTree = "<group>"; };
|
||||
@ -512,6 +514,7 @@
|
||||
E4EB2378220F10B8008C70C0 /* Pair.swift */,
|
||||
E4EB237A220F7CF1008C70C0 /* Album.swift */,
|
||||
E4C8B53D22349002009A20F3 /* Idle.swift */,
|
||||
E45962C52241A78500FC1A1E /* Command.swift */,
|
||||
);
|
||||
path = Models;
|
||||
sourceTree = "<group>";
|
||||
@ -753,6 +756,7 @@
|
||||
E450AD7E222620A10091BED3 /* AlbumItem.swift in Sources */,
|
||||
E4E8CC942206097F0024217A /* NotificationsController.swift in Sources */,
|
||||
E408D3B6220DD8970006D9BE /* Notification.swift in Sources */,
|
||||
E45962C62241A78500FC1A1E /* Command.swift in Sources */,
|
||||
E408D3B9220DE98F0006D9BE /* NSUserInterfaceItemIdentifier.swift in Sources */,
|
||||
E4EB2379220F10B8008C70C0 /* Pair.swift in Sources */,
|
||||
E4F6B463221E125900ACF42A /* SongItem.swift in Sources */,
|
||||
|
||||
@ -11,15 +11,15 @@ import mpdclient
|
||||
|
||||
extension MPDClient {
|
||||
func fetchAllAlbums() {
|
||||
queueCommand(command: .fetchAllAlbums)
|
||||
enqueueCommand(command: .fetchAllAlbums)
|
||||
}
|
||||
|
||||
func playAlbum(_ album: Album) {
|
||||
queueCommand(command: .playAlbum, userData: ["album": album])
|
||||
enqueueCommand(command: .playAlbum, userData: ["album": album])
|
||||
}
|
||||
|
||||
func getAlbumURI(for album: Album, callback: @escaping (String?) -> Void) {
|
||||
queueCommand(
|
||||
enqueueCommand(
|
||||
command: .getAlbumURI,
|
||||
priority: .low,
|
||||
userData: ["album": album, "callback": callback]
|
||||
|
||||
@ -54,4 +54,22 @@ extension MPDClient {
|
||||
albumURI(for: album, callback: callback)
|
||||
}
|
||||
}
|
||||
|
||||
func enqueueCommand(
|
||||
command: Command,
|
||||
priority: BlockOperation.QueuePriority = .normal,
|
||||
userData: Dictionary<String, Any> = [:]
|
||||
) {
|
||||
guard isConnected else { return }
|
||||
|
||||
noIdle()
|
||||
|
||||
let commandOperation = BlockOperation() { [unowned self] in
|
||||
self.sendCommand(command: command, userData: userData)
|
||||
|
||||
self.idle()
|
||||
}
|
||||
commandOperation.queuePriority = priority
|
||||
commandQueue.addOperation(commandOperation)
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@ extension MPDClient {
|
||||
}
|
||||
|
||||
func idle() {
|
||||
if !self.isIdle && self.commandsQueued == 0 {
|
||||
if !self.isIdle && self.commandQueue.operationCount == 1 {
|
||||
mpd_send_idle(self.connection)
|
||||
self.isIdle = true
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@ extension MPDClient {
|
||||
}
|
||||
|
||||
func playTrack(at queuePos: Int) {
|
||||
queueCommand(command: .playTrack, userData: ["queuePos": queuePos])
|
||||
enqueueCommand(command: .playTrack, userData: ["queuePos": queuePos])
|
||||
}
|
||||
|
||||
func sendPlayTrack(at queuePos: Int) {
|
||||
|
||||
@ -11,23 +11,23 @@ import mpdclient
|
||||
|
||||
extension MPDClient {
|
||||
func playPause() {
|
||||
queueCommand(command: .playPause)
|
||||
enqueueCommand(command: .playPause)
|
||||
}
|
||||
|
||||
func stop() {
|
||||
queueCommand(command: .stop)
|
||||
enqueueCommand(command: .stop)
|
||||
}
|
||||
|
||||
func prevTrack() {
|
||||
queueCommand(command: .prevTrack)
|
||||
enqueueCommand(command: .prevTrack)
|
||||
}
|
||||
|
||||
func nextTrack() {
|
||||
queueCommand(command: .nextTrack)
|
||||
enqueueCommand(command: .nextTrack)
|
||||
}
|
||||
|
||||
func seekCurrentSong(timeInSeconds: Float) {
|
||||
queueCommand(
|
||||
enqueueCommand(
|
||||
command: .seekCurrentSong,
|
||||
userData: ["timeInSeconds": timeInSeconds]
|
||||
)
|
||||
|
||||
@ -19,36 +19,9 @@ class MPDClient {
|
||||
var queue: [Song] = []
|
||||
|
||||
let commandQueue = OperationQueue()
|
||||
var commandsQueued: UInt = 0
|
||||
|
||||
enum Command {
|
||||
case prevTrack, nextTrack, playPause, stop, seekCurrentSong,
|
||||
fetchStatus, fetchQueue, playTrack, fetchAllAlbums,
|
||||
playAlbum, getAlbumURI
|
||||
}
|
||||
|
||||
init(withDelegate delegate: MPDClientDelegate?) {
|
||||
commandQueue.maxConcurrentOperationCount = 1
|
||||
self.delegate = delegate
|
||||
}
|
||||
|
||||
func queueCommand(
|
||||
command: Command,
|
||||
priority: BlockOperation.QueuePriority = .normal,
|
||||
userData: Dictionary<String, Any> = [:]
|
||||
) {
|
||||
guard isConnected else { return }
|
||||
|
||||
noIdle()
|
||||
|
||||
let commandOperation = BlockOperation() { [unowned self] in
|
||||
self.commandsQueued -= 1
|
||||
self.sendCommand(command: command, userData: userData)
|
||||
|
||||
self.idle()
|
||||
}
|
||||
commandOperation.queuePriority = priority
|
||||
commandsQueued += 1
|
||||
commandQueue.addOperation(commandOperation)
|
||||
}
|
||||
}
|
||||
|
||||
32
Persephone/MPDClient/Models/Command.swift
Normal file
32
Persephone/MPDClient/Models/Command.swift
Normal file
@ -0,0 +1,32 @@
|
||||
//
|
||||
// Command.swift
|
||||
// Persephone
|
||||
//
|
||||
// Created by Daniel Barber on 2019/3/19.
|
||||
// Copyright © 2019 Dan Barber. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension MPDClient {
|
||||
enum Command {
|
||||
// Transport commands
|
||||
case prevTrack
|
||||
case nextTrack
|
||||
case playPause
|
||||
case stop
|
||||
case seekCurrentSong
|
||||
|
||||
// Status commands
|
||||
case fetchStatus
|
||||
|
||||
// Queue commands
|
||||
case fetchQueue
|
||||
case playTrack
|
||||
|
||||
// Album commands
|
||||
case fetchAllAlbums
|
||||
case playAlbum
|
||||
case getAlbumURI
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user