Created
March 7, 2018 19:40
-
-
Save lmorchard/bc768f49bd1b102d15450269ad864181 to your computer and use it in GitHub Desktop.
Dispatching messages from web content in an add-on
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const messageListener = port => message => { | |
let theme; | |
switch (message.type) { | |
case "fetchTheme": | |
log("fetchTheme"); | |
fetchTheme().then(({ theme: currentTheme }) => | |
port.postMessage({ type: "fetchedTheme", theme: currentTheme }) | |
); | |
break; | |
case "setTheme": | |
theme = normalizeTheme(message.theme); | |
log("setTheme", theme); | |
storeTheme({ theme }); | |
applyTheme({ theme }); | |
break; | |
case "ping": | |
port.postMessage({ type: "pong" }); | |
break; | |
default: | |
log("unexpected message", message); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment