From e404d44985869544beceb4e9ea259f5cf3926a3a Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Thu, 22 Apr 2021 16:48:01 -0700 Subject: extension: allow hot patching of Routes; keep backgroundJS alive (across hot reloads) --- extension/background.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'extension/background.js') diff --git a/extension/background.js b/extension/background.js index d58fbb2..c885d45 100644 --- a/extension/background.js +++ b/extension/background.js @@ -153,7 +153,8 @@ const routeWithContents = (function() { return routeWithContents; })(); -const Routes = {}; +// global so it can be hot-reloaded +window.Routes = {}; Routes["/tabs/create"] = { usage: 'echo "https://www.google.com" > $0', @@ -572,16 +573,19 @@ Routes["/runtime/reload"] = { }; (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', { async get() { - if (!__backgroundJS) { - __backgroundJS = await window.fetch(chrome.runtime.getURL('background.js')) + if (!window.__backgroundJS) { + window.__backgroundJS = await window.fetch(chrome.runtime.getURL('background.js')) .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 }); })(); @@ -594,9 +598,7 @@ Routes["/runtime/background.js"] = { ...routeWithContents( async () => { // `window.backgroundJS` is the source code of the file you're - // reading right now! it needs to be a global because we want - // its value (the changed JS text) to survive even as this whole - // module gets re-evaluated. + // reading right now! return window.backgroundJS; }, async ({}, buf) => { window.backgroundJS = buf; } -- cgit v1.2.3