mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-28 11:51:53 +01:00
v0.26.0
This commit is contained in:
@@ -1,6 +1,26 @@
|
||||
/**
|
||||
* @typedef {"debug"|"info"|"warn"|"error"} EventLevel
|
||||
* @see goutils/events/level.go
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{ message?: string, error?: string }} WakeEvent
|
||||
* @see internal/idlewatcher/events.go WakeEvent
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {"starting"|"waking_dep"|"dep_ready"|"container_woke"|"waiting_ready"|"ready"|"error"} WakeEventType
|
||||
* @see internal/idlewatcher/events.go WakeEventType
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {{ timestamp: string, level: EventLevel, category: string, action: WakeEventType, data: WakeEvent }} WakeSSEEvent
|
||||
* @see goutils/events/event.go Event
|
||||
*/
|
||||
|
||||
let ready = false;
|
||||
|
||||
window.onload = async function () {
|
||||
window.onload = async () => {
|
||||
const consoleEl = document.getElementById("console");
|
||||
const loadingDotsEl = document.getElementById("loading-dots");
|
||||
|
||||
@@ -9,6 +29,10 @@ window.onload = async function () {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} timestamp - ISO timestamp string
|
||||
* @returns {string}
|
||||
*/
|
||||
function formatTimestamp(timestamp) {
|
||||
const date = new Date(timestamp);
|
||||
return date.toLocaleTimeString("en-US", {
|
||||
@@ -20,6 +44,11 @@ window.onload = async function () {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} type - Console line type (e.g. ready, error, or WakeEventType)
|
||||
* @param {string} message
|
||||
* @param {string} timestamp - ISO timestamp string
|
||||
*/
|
||||
function addConsoleLine(type, message, timestamp) {
|
||||
const line = document.createElement("div");
|
||||
line.className = `console-line ${type}`;
|
||||
@@ -43,7 +72,7 @@ window.onload = async function () {
|
||||
addConsoleLine(
|
||||
"error",
|
||||
"Configuration error: wakeEventsPath not defined",
|
||||
new Date().toISOString()
|
||||
new Date().toISOString(),
|
||||
);
|
||||
loadingDotsEl.style.display = "none";
|
||||
return;
|
||||
@@ -53,7 +82,7 @@ window.onload = async function () {
|
||||
addConsoleLine(
|
||||
"error",
|
||||
"Browser does not support Server-Sent Events",
|
||||
new Date().toISOString()
|
||||
new Date().toISOString(),
|
||||
);
|
||||
loadingDotsEl.style.display = "none";
|
||||
return;
|
||||
@@ -62,44 +91,46 @@ window.onload = async function () {
|
||||
// Connect to SSE endpoint
|
||||
const eventSource = new EventSource(wakeEventsPath);
|
||||
|
||||
eventSource.onmessage = function (event) {
|
||||
let data;
|
||||
eventSource.onmessage = (event) => {
|
||||
/** @type {WakeSSEEvent} */
|
||||
let evt;
|
||||
try {
|
||||
data = JSON.parse(event.data);
|
||||
} catch (error) {
|
||||
evt = JSON.parse(event.data);
|
||||
} catch {
|
||||
addConsoleLine(
|
||||
"error",
|
||||
"Invalid event data: " + event.data,
|
||||
new Date().toISOString()
|
||||
`Invalid event data: ${event.data}`,
|
||||
new Date().toISOString(),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (data.type === "ready") {
|
||||
const payload = evt.data || {};
|
||||
const type = evt.action;
|
||||
const timestamp = evt.timestamp;
|
||||
|
||||
if (type === "ready") {
|
||||
ready = true;
|
||||
// Container is ready, hide loading dots and refresh
|
||||
loadingDotsEl.style.display = "none";
|
||||
addConsoleLine(
|
||||
data.type,
|
||||
"Container is ready, refreshing...",
|
||||
data.timestamp
|
||||
);
|
||||
addConsoleLine(type, "Container is ready, refreshing...", timestamp);
|
||||
setTimeout(() => {
|
||||
window.location.reload();
|
||||
}, 200);
|
||||
} else if (data.type === "error") {
|
||||
} else if (type === "error" || evt.level === "error") {
|
||||
// Show error message and hide loading dots
|
||||
const errorMessage = data.error || data.message;
|
||||
addConsoleLine(data.type, errorMessage, data.timestamp);
|
||||
const errorMessage = payload.error || payload.message || "Unknown error";
|
||||
addConsoleLine(type, errorMessage, timestamp);
|
||||
loadingDotsEl.style.display = "none";
|
||||
eventSource.close();
|
||||
} else {
|
||||
// Show other message types
|
||||
addConsoleLine(data.type, data.message, data.timestamp);
|
||||
const message = payload.message;
|
||||
addConsoleLine(type, message, timestamp);
|
||||
}
|
||||
};
|
||||
|
||||
eventSource.onerror = function (event) {
|
||||
eventSource.onerror = () => {
|
||||
if (ready) {
|
||||
// event will be closed by the server
|
||||
return;
|
||||
@@ -107,7 +138,7 @@ window.onload = async function () {
|
||||
addConsoleLine(
|
||||
"error",
|
||||
"Connection lost. Please refresh the page.",
|
||||
new Date().toISOString()
|
||||
new Date().toISOString(),
|
||||
);
|
||||
loadingDotsEl.style.display = "none";
|
||||
eventSource.close();
|
||||
|
||||
Reference in New Issue
Block a user