From 158ad14f97646099809484b3fb3c0c61dc7160e9 Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Thu, 22 Apr 2021 16:39:15 -0700 Subject: [PATCH] extension: fix hot patching of backgroundJS (to the point where you can log and it shows up; it doesn't seem able to replace routes yet) --- extension/background.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/extension/background.js b/extension/background.js index 6729c68..d58fbb2 100644 --- a/extension/background.js +++ b/extension/background.js @@ -575,12 +575,14 @@ Routes["/runtime/reload"] = { let __backgroundJS; Object.defineProperty(window, 'backgroundJS', { async get() { - __backgroundJS = __backgroundJS || - await window.fetch(chrome.runtime.getURL('background.js')) - .then(r => r.text()); + if (!__backgroundJS) { + __backgroundJS = await window.fetch(chrome.runtime.getURL('background.js')) + .then(r => r.text()); + } return __backgroundJS; }, - set(js) { __backgroundJS = js; } + set(js) { __backgroundJS = js; }, + configurable: true // so we can rerun this on hot reload }); })(); @@ -599,9 +601,9 @@ Routes["/runtime/background.js"] = { }, async ({}, buf) => { window.backgroundJS = buf; } ), - release({fh}) { + async release({fh}) { // Note that we eval on release, not on write. - eval(window.backgroundJS); + eval(await window.backgroundJS); // TODO: would be better if we could call 'super'.release() so // we wouldn't need to involve how Cache works here. routeWithContents.Cache.removeObjectForHandle(fh); @@ -635,7 +637,7 @@ Routes["/runtime/background.js.html"] = routeWithContents(async () => {