extension: allow hot patching of Routes; keep backgroundJS alive

(across hot reloads)
This commit is contained in:
Omar Rizwan 2021-04-22 16:48:01 -07:00
parent 158ad14f97
commit e404d44985

View file

@ -153,7 +153,8 @@ const routeWithContents = (function() {
return routeWithContents; return routeWithContents;
})(); })();
const Routes = {}; // global so it can be hot-reloaded
window.Routes = {};
Routes["/tabs/create"] = { Routes["/tabs/create"] = {
usage: 'echo "https://www.google.com" > $0', usage: 'echo "https://www.google.com" > $0',
@ -572,16 +573,19 @@ Routes["/runtime/reload"] = {
}; };
(function() { (function() {
let __backgroundJS; // window.__backgroundJS needs to be a global because we want
// its value (the changed JS text) to survive even as this whole
// module gets re-evaluated.
window.__backgroundJS = window.__backgroundJS || false;
Object.defineProperty(window, 'backgroundJS', { Object.defineProperty(window, 'backgroundJS', {
async get() { async get() {
if (!__backgroundJS) { if (!window.__backgroundJS) {
__backgroundJS = await window.fetch(chrome.runtime.getURL('background.js')) window.__backgroundJS = await window.fetch(chrome.runtime.getURL('background.js'))
.then(r => r.text()); .then(r => r.text());
} }
return __backgroundJS; return window.__backgroundJS;
}, },
set(js) { __backgroundJS = js; }, set(js) { window.__backgroundJS = js; },
configurable: true // so we can rerun this on hot reload configurable: true // so we can rerun this on hot reload
}); });
})(); })();
@ -594,9 +598,7 @@ Routes["/runtime/background.js"] = {
...routeWithContents( ...routeWithContents(
async () => { async () => {
// `window.backgroundJS` is the source code of the file you're // `window.backgroundJS` is the source code of the file you're
// reading right now! it needs to be a global because we want // reading right now!
// its value (the changed JS text) to survive even as this whole
// module gets re-evaluated.
return window.backgroundJS; return window.backgroundJS;
}, },
async ({}, buf) => { window.backgroundJS = buf; } async ({}, buf) => { window.backgroundJS = buf; }