Moved most of the message handling to other classes.

I'm finding that this code is being used multiple times, so inheritance seems to be warranted.
This commit is contained in:
Markil3
2021-07-29 09:11:33 -07:00
parent 6cc001af70
commit 1321549ef3
10 changed files with 926 additions and 350 deletions

View File

@@ -4,10 +4,42 @@ On startup, connect to the "ping_pong" app.
*/
var port = browser.runtime.connectNative("universalmusic");
var listeners = [function (message, returnValue) {
if (returnValue == null)
{
returnValue = "pong";
}
return returnValue;
}];
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function handleMessage(message)
{
return new Promise((resolve, reject) => {
var returnValue = null;
for (index in listeners)
{
returnValue = listeners[index](message, returnValue);
}
resolve(returnValue);
})
}
/*
Listen for messages from the app.
*/
port.onMessage.addListener((response) => {
console.log("Received: " + response);
port.postMessage("pong");
* Listen for messages from the app.
*/
port.onMessage.addListener((message) => {
console.log("Received: ", message);
handleMessage(message.message).then((response) => {
returnValue = {
"messageNum": message.messageNum,
"message": response
}
console.log("Sending ", returnValue)
port.postMessage(returnValue);
});
});