2024-09-17 17:13:22 +00:00
|
|
|
import htmx from "htmx.org";
|
|
|
|
|
import {createWebSocketClient} from "../util/ws";
|
|
|
|
|
|
|
|
|
|
let lastVersion = "";
|
|
|
|
|
|
|
|
|
|
htmx.defineExtension("livereload", {
|
|
|
|
|
init: function () {
|
2024-09-21 03:59:07 +00:00
|
|
|
|
|
|
|
|
let enabled = false
|
|
|
|
|
for (const element of Array.from(htmx.findAll("[hx-ext]"))) {
|
|
|
|
|
const value = element.getAttribute("hx-ext");
|
|
|
|
|
if(value?.split(" ").includes("livereload")) {
|
|
|
|
|
enabled = true
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!enabled) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 17:13:22 +00:00
|
|
|
console.log('livereload extension initialized.');
|
2024-09-26 19:15:57 +00:00
|
|
|
// Create a new EventSource object and point it to your SSE endpoint
|
|
|
|
|
const eventSource = new EventSource('/dev/livereload');
|
|
|
|
|
// Listen for messages from the server
|
|
|
|
|
eventSource.onmessage = function(event) {
|
|
|
|
|
const message = event.data
|
|
|
|
|
// Log the message data received from the server
|
|
|
|
|
if(lastVersion === "") {
|
|
|
|
|
lastVersion = message;
|
|
|
|
|
}
|
|
|
|
|
if(lastVersion !== message) {
|
|
|
|
|
lastVersion = message;
|
|
|
|
|
reload()
|
2024-09-17 17:13:22 +00:00
|
|
|
}
|
2024-09-26 19:15:57 +00:00
|
|
|
};
|
|
|
|
|
// Handle errors (e.g., when the connection is closed)
|
|
|
|
|
eventSource.onerror = function(error) {
|
|
|
|
|
console.error('EventSource error:', error);
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-17 17:13:22 +00:00
|
|
|
},
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
onEvent: function (name, evt) {
|
|
|
|
|
|
|
|
|
|
},
|
2024-09-17 18:35:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function reload() {
|
2024-09-19 03:32:09 +00:00
|
|
|
window.location.reload()
|
2024-09-17 18:35:44 +00:00
|
|
|
}
|