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:
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user