Skip to content

Commit

Permalink
Use promises for amcp command
Browse files Browse the repository at this point in the history
  • Loading branch information
martastain committed Jul 13, 2021
1 parent 346d8b6 commit 85053a4
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,32 @@ function next() {}
function update(data) {}


var amcp_bridge_url = "http://127.0.0.1:9731/amcp";
var amcp_bridge_url = "http://127.0.0.1:9731/amcp"
var amcp_bridge_timeout = 5000

function amcp(command){
log("SENDING " + command)
var xhr = new XMLHttpRequest()
xhr.open('POST', amcp_bridge_url)
xhr.responseType = "text"
xhr.send(command)
return new Promise(function(resolve, reject){

xhr.onload = function() {
};
log("AMCP TX: " + command)

xhr.onerror = function() {
log("Status:" + xhr.status + " : " + xhr.statusText)
log(xhr.responseText)
}
var xhr = new XMLHttpRequest()
xhr.timeout = amcp_bridge_timeout
xhr.open('POST', amcp_bridge_url)
xhr.responseType = "text"
xhr.send(command)

xhr.onload = function() {
log("AMCP RX: " + xhr.status + " " + command)
if (xhr.status < 400)
resolve(xhr.status, xhr.responseText)
else
reject(xhr.status, xhr.responseText)
};

xhr.onerror = function() {
log("AMCP RX ERR:", xhr.status)
reject(xhr.status, xhr.responseTextx)
}

}) // return new Promise
}

0 comments on commit 85053a4

Please sign in to comment.