diff options
Diffstat (limited to 'ui/src/frontend/live_reload.ts')
-rw-r--r-- | ui/src/frontend/live_reload.ts | 63 |
1 files changed, 0 insertions, 63 deletions
diff --git a/ui/src/frontend/live_reload.ts b/ui/src/frontend/live_reload.ts deleted file mode 100644 index c30b6e0f9..000000000 --- a/ui/src/frontend/live_reload.ts +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (C) 2021 The Android Open Source Project -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -let lastReloadDialogTime = 0; -const kMinTimeBetweenDialogsMs = 10000; -const changedPaths = new Set<string>(); - -export function initLiveReloadIfLocalhost() { - if (!location.origin.startsWith('http://localhost:')) return; - - const monitor = new EventSource('/live_reload'); - monitor.onmessage = msg => { - const change = msg.data; - console.log('Live reload:', change); - changedPaths.add(change); - if (change.endsWith('.css')) { - reloadCSS(); - } else if (change.endsWith('.html') || change.endsWith('.js')) { - reloadDelayed(); - } - }; - monitor.onerror = (err) => { - // In most cases the error is fired on reload, when the socket disconnects. - // Delay the error and the reconnection, so in the case of a reload we don't - // see any midleading message. - setTimeout(() => console.error('LiveReload SSE error', err), 1000); - }; -} - -function reloadCSS() { - const css = document.querySelector('link[rel=stylesheet]') as HTMLLinkElement; - if (!css) return; - const parent = css.parentElement!; - parent.removeChild(css); - parent.appendChild(css); -} - -function reloadDelayed() { - setTimeout(() => { - let pathsStr = ''; - for (const path of changedPaths) { - pathsStr += path + '\n'; - } - changedPaths.clear(); - if (Date.now() - lastReloadDialogTime < kMinTimeBetweenDialogsMs) return; - const reload = confirm(`${pathsStr}changed, click to reload`); - lastReloadDialogTime = Date.now(); - if (reload) { - window.location.reload(); - } - }, 1000); -} |