From 8d89fc8ab9f5e56dd19595f68921adb8fc779fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C5=BD=C3=A1ra?= Date: Thu, 7 May 2020 18:45:33 +0200 Subject: [PATCH] idle cleanup --- app/cyp.js | 61 +++++++++++++++++++++++++++------------------------ app/js/mpd.js | 61 +++++++++++++++++++++++++++------------------------ 2 files changed, 64 insertions(+), 58 deletions(-) diff --git a/app/cyp.js b/app/cyp.js index e3f4439..a9dba85 100644 --- a/app/cyp.js +++ b/app/cyp.js @@ -242,21 +242,6 @@ let commandQueue = []; let current; let canTerminateIdle = false; -function onMessage(e) { - if (!current) { return; } - - let lines = JSON.parse(e.data); - let last = lines.pop(); - if (last.startsWith("OK")) { - current.resolve(lines); - } else { - console.warn(last); - current.reject(last); - } - current = null; - setTimeout(processQueue, 0); // after other potential commands are enqueued -} - function onError(e) { console.error(e); current && current.reject(e); @@ -269,24 +254,36 @@ function onClose(e) { ws = null; // fixme } -function processQueue() { - if (commandQueue.length == 0) { - if (!current) { idle(); } // nothing to do - } else if (current) { // stuff waiting in queue but there is a command under way - if (canTerminateIdle) { - ws.send("noidle"); - canTerminateIdle = false; - } - } else { // advance to next command - current = commandQueue.shift(); - ws.send(current.cmd); +function onMessage(e) { + if (!current) { return; } + + let lines = JSON.parse(e.data); + let last = lines.pop(); + if (last.startsWith("OK")) { + current.resolve(lines); + } else { + console.warn(last); + current.reject(last); + } + current = null; + + if (commandQueue.length > 0) { + advanceQueue(); + } else { + setTimeout(idle, 0); // only after resolution callbacks } } +function advanceQueue(){ + current = commandQueue.shift(); + ws.send(current.cmd); +} + async function idle() { - let promise = command("idle stored_playlist playlist player options"); + if (current) { return; } + canTerminateIdle = true; - let lines = await promise; + let lines = await command("idle stored_playlist playlist player options"); canTerminateIdle = false; let changed = linesToStruct(lines).changed || []; changed = [].concat(changed); @@ -301,7 +298,13 @@ async function command(cmd) { return new Promise((resolve, reject) => { commandQueue.push({cmd, resolve, reject}); - processQueue(); + + if (!current) { + advanceQueue(); + } else if (canTerminateIdle) { + ws.send("noidle"); + canTerminateIdle = false; + } }); } diff --git a/app/js/mpd.js b/app/js/mpd.js index 841dec2..7f4dce2 100644 --- a/app/js/mpd.js +++ b/app/js/mpd.js @@ -5,21 +5,6 @@ let commandQueue = []; let current; let canTerminateIdle = false; -function onMessage(e) { - if (!current) { return; } - - let lines = JSON.parse(e.data); - let last = lines.pop(); - if (last.startsWith("OK")) { - current.resolve(lines); - } else { - console.warn(last); - current.reject(last); - } - current = null; - setTimeout(processQueue, 0); // after other potential commands are enqueued -} - function onError(e) { console.error(e); current && current.reject(e); @@ -32,24 +17,36 @@ function onClose(e) { ws = null; // fixme } -function processQueue() { - if (commandQueue.length == 0) { - if (!current) { idle(); } // nothing to do - } else if (current) { // stuff waiting in queue but there is a command under way - if (canTerminateIdle) { - ws.send("noidle"); - canTerminateIdle = false; - } - } else { // advance to next command - current = commandQueue.shift(); - ws.send(current.cmd); +function onMessage(e) { + if (!current) { return; } + + let lines = JSON.parse(e.data); + let last = lines.pop(); + if (last.startsWith("OK")) { + current.resolve(lines); + } else { + console.warn(last); + current.reject(last); + } + current = null; + + if (commandQueue.length > 0) { + advanceQueue(); + } else { + setTimeout(idle, 0); // only after resolution callbacks } } +function advanceQueue(){ + current = commandQueue.shift(); + ws.send(current.cmd); +} + async function idle() { - let promise = command("idle stored_playlist playlist player options"); + if (current) { return; } + canTerminateIdle = true; - let lines = await promise; + let lines = await command("idle stored_playlist playlist player options"); canTerminateIdle = false; let changed = parser.linesToStruct(lines).changed || []; changed = [].concat(changed); @@ -64,7 +61,13 @@ export async function command(cmd) { return new Promise((resolve, reject) => { commandQueue.push({cmd, resolve, reject}); - processQueue(); + + if (!current) { + advanceQueue(); + } else if (canTerminateIdle) { + ws.send("noidle"); + canTerminateIdle = false; + } }); }