({
debounceTime = 100,
url = "ws://localhost:3001/ws",
}: {
url?: string;
debounceTime?: number;
} = {})
| 1 | export function liveReloadScript({ |
| 2 | debounceTime = 100, |
| 3 | url = "ws://localhost:3001/ws", |
| 4 | }: { |
| 5 | url?: string; |
| 6 | debounceTime?: number; |
| 7 | } = {}): string { |
| 8 | return ` |
| 9 | let reloadTimeout; |
| 10 | (function () { |
| 11 | let socket = new WebSocket(\"${url}\"); |
| 12 | |
| 13 | socket.onopen = function(e) { |
| 14 | console.log("connected") |
| 15 | }; |
| 16 | |
| 17 | |
| 18 | socket.onmessage = function(event) { |
| 19 | console.log("event", event.data) |
| 20 | // Clear any existing reload timeout |
| 21 | clearTimeout(reloadTimeout); |
| 22 | |
| 23 | // Set a new reload timeout |
| 24 | reloadTimeout = setTimeout(() => { |
| 25 | location.reload(); |
| 26 | }, ${debounceTime}); // 50ms debounce time |
| 27 | }; |
| 28 | |
| 29 | socket.onclose = function(event) { |
| 30 | console.log("closed"); |
| 31 | }; |
| 32 | |
| 33 | socket.onerror = function(error) { |
| 34 | console.log("error: " + error.message); |
| 35 | }; |
| 36 | })(); |
| 37 | `; |
| 38 | } |
nothing calls this directly
no outgoing calls
no test coverage detected