sliftutils 1.2.12 → 1.2.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/builders/hotReload.ts +6 -18
- package/package.json +1 -1
package/builders/hotReload.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { isInBrowser, isInChromeExtension, isInChromeExtensionBackground, isInCh
|
|
|
3
3
|
|
|
4
4
|
const DEFAULT_WATCH_PORT = 9876;
|
|
5
5
|
const CONTENT_SCRIPT_POLL_INTERVAL_MS = 1000;
|
|
6
|
-
const
|
|
6
|
+
const HOT_RELOAD_HASH_STORAGE_KEY = "sliftHotReloadBuildHash";
|
|
7
7
|
|
|
8
8
|
export async function enableHotReloading(config?: {
|
|
9
9
|
port?: number;
|
|
@@ -79,18 +79,9 @@ function watchPortHotReload(port = DEFAULT_WATCH_PORT, onReload: () => void, onH
|
|
|
79
79
|
function chromeExtensionBackgroundHotReload(port = DEFAULT_WATCH_PORT) {
|
|
80
80
|
let currentHash: string | undefined;
|
|
81
81
|
|
|
82
|
-
// Content scripts poll the background for the latest build hash and reload
|
|
83
|
-
// themselves when it changes.
|
|
84
|
-
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
|
85
|
-
if (message && message.type === HOT_RELOAD_HASH_MESSAGE) {
|
|
86
|
-
sendResponse({ hash: currentHash });
|
|
87
|
-
return; // synchronous response
|
|
88
|
-
}
|
|
89
|
-
return undefined;
|
|
90
|
-
});
|
|
91
|
-
|
|
92
82
|
watchPortHotReload(port, () => { /* unused — hash callback drives reloads */ }, (hash) => {
|
|
93
83
|
if (!hash) return;
|
|
84
|
+
chrome.storage.local.set({ [HOT_RELOAD_HASH_STORAGE_KEY]: hash });
|
|
94
85
|
if (currentHash === undefined) {
|
|
95
86
|
currentHash = hash;
|
|
96
87
|
console.log(`[Hot Reload] Initial build hash: ${hash}`);
|
|
@@ -109,12 +100,9 @@ function chromeExtensionContentScriptHotReload() {
|
|
|
109
100
|
|
|
110
101
|
setInterval(() => {
|
|
111
102
|
try {
|
|
112
|
-
chrome.
|
|
113
|
-
if (chrome.runtime.lastError)
|
|
114
|
-
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
let hash: string | undefined = response?.hash;
|
|
103
|
+
chrome.storage.local.get(HOT_RELOAD_HASH_STORAGE_KEY, (result) => {
|
|
104
|
+
if (chrome.runtime.lastError) return;
|
|
105
|
+
let hash: string | undefined = result?.[HOT_RELOAD_HASH_STORAGE_KEY];
|
|
118
106
|
if (!hash) return;
|
|
119
107
|
if (lastHash === undefined) {
|
|
120
108
|
lastHash = hash;
|
|
@@ -127,7 +115,7 @@ function chromeExtensionContentScriptHotReload() {
|
|
|
127
115
|
}
|
|
128
116
|
});
|
|
129
117
|
} catch (error) {
|
|
130
|
-
//
|
|
118
|
+
// Extension context can be transiently invalidated during reload.
|
|
131
119
|
}
|
|
132
120
|
}, CONTENT_SCRIPT_POLL_INTERVAL_MS);
|
|
133
121
|
}
|