idle cleanup
This commit is contained in:
parent
11ffef158b
commit
8d89fc8ab9
2 changed files with 64 additions and 58 deletions
59
app/cyp.js
59
app/cyp.js
|
@ -242,21 +242,6 @@ let commandQueue = [];
|
||||||
let current;
|
let current;
|
||||||
let canTerminateIdle = false;
|
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) {
|
function onError(e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
current && current.reject(e);
|
current && current.reject(e);
|
||||||
|
@ -269,24 +254,36 @@ function onClose(e) {
|
||||||
ws = null; // fixme
|
ws = null; // fixme
|
||||||
}
|
}
|
||||||
|
|
||||||
function processQueue() {
|
function onMessage(e) {
|
||||||
if (commandQueue.length == 0) {
|
if (!current) { return; }
|
||||||
if (!current) { idle(); } // nothing to do
|
|
||||||
} else if (current) { // stuff waiting in queue but there is a command under way
|
let lines = JSON.parse(e.data);
|
||||||
if (canTerminateIdle) {
|
let last = lines.pop();
|
||||||
ws.send("noidle");
|
if (last.startsWith("OK")) {
|
||||||
canTerminateIdle = false;
|
current.resolve(lines);
|
||||||
|
} else {
|
||||||
|
console.warn(last);
|
||||||
|
current.reject(last);
|
||||||
}
|
}
|
||||||
} else { // advance to next command
|
current = null;
|
||||||
current = commandQueue.shift();
|
|
||||||
ws.send(current.cmd);
|
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() {
|
async function idle() {
|
||||||
let promise = command("idle stored_playlist playlist player options");
|
if (current) { return; }
|
||||||
|
|
||||||
canTerminateIdle = true;
|
canTerminateIdle = true;
|
||||||
let lines = await promise;
|
let lines = await command("idle stored_playlist playlist player options");
|
||||||
canTerminateIdle = false;
|
canTerminateIdle = false;
|
||||||
let changed = linesToStruct(lines).changed || [];
|
let changed = linesToStruct(lines).changed || [];
|
||||||
changed = [].concat(changed);
|
changed = [].concat(changed);
|
||||||
|
@ -301,7 +298,13 @@ async function command(cmd) {
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
commandQueue.push({cmd, resolve, reject});
|
commandQueue.push({cmd, resolve, reject});
|
||||||
processQueue();
|
|
||||||
|
if (!current) {
|
||||||
|
advanceQueue();
|
||||||
|
} else if (canTerminateIdle) {
|
||||||
|
ws.send("noidle");
|
||||||
|
canTerminateIdle = false;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,21 +5,6 @@ let commandQueue = [];
|
||||||
let current;
|
let current;
|
||||||
let canTerminateIdle = false;
|
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) {
|
function onError(e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
current && current.reject(e);
|
current && current.reject(e);
|
||||||
|
@ -32,24 +17,36 @@ function onClose(e) {
|
||||||
ws = null; // fixme
|
ws = null; // fixme
|
||||||
}
|
}
|
||||||
|
|
||||||
function processQueue() {
|
function onMessage(e) {
|
||||||
if (commandQueue.length == 0) {
|
if (!current) { return; }
|
||||||
if (!current) { idle(); } // nothing to do
|
|
||||||
} else if (current) { // stuff waiting in queue but there is a command under way
|
let lines = JSON.parse(e.data);
|
||||||
if (canTerminateIdle) {
|
let last = lines.pop();
|
||||||
ws.send("noidle");
|
if (last.startsWith("OK")) {
|
||||||
canTerminateIdle = false;
|
current.resolve(lines);
|
||||||
|
} else {
|
||||||
|
console.warn(last);
|
||||||
|
current.reject(last);
|
||||||
}
|
}
|
||||||
} else { // advance to next command
|
current = null;
|
||||||
current = commandQueue.shift();
|
|
||||||
ws.send(current.cmd);
|
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() {
|
async function idle() {
|
||||||
let promise = command("idle stored_playlist playlist player options");
|
if (current) { return; }
|
||||||
|
|
||||||
canTerminateIdle = true;
|
canTerminateIdle = true;
|
||||||
let lines = await promise;
|
let lines = await command("idle stored_playlist playlist player options");
|
||||||
canTerminateIdle = false;
|
canTerminateIdle = false;
|
||||||
let changed = parser.linesToStruct(lines).changed || [];
|
let changed = parser.linesToStruct(lines).changed || [];
|
||||||
changed = [].concat(changed);
|
changed = [].concat(changed);
|
||||||
|
@ -64,7 +61,13 @@ export async function command(cmd) {
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
commandQueue.push({cmd, resolve, reject});
|
commandQueue.push({cmd, resolve, reject});
|
||||||
processQueue();
|
|
||||||
|
if (!current) {
|
||||||
|
advanceQueue();
|
||||||
|
} else if (canTerminateIdle) {
|
||||||
|
ws.send("noidle");
|
||||||
|
canTerminateIdle = false;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue