Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dispatching messages from web content in an add-on
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