mirror of
				https://github.com/mermaid-js/mermaid.git
				synced 2025-10-31 10:54:15 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			23 lines
		
	
	
		
			636 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			23 lines
		
	
	
		
			636 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| // Connect to the server and reload the page if the server sends a reload message
 | |
| const connectToEvents = () => {
 | |
|   const events = new EventSource('/events');
 | |
|   const loadTime = Date.now();
 | |
|   events.onmessage = (event) => {
 | |
|     const time = JSON.parse(event.data);
 | |
|     if (time && time > loadTime) {
 | |
|       location.reload();
 | |
|     }
 | |
|   };
 | |
|   events.onerror = (error) => {
 | |
|     console.error(error);
 | |
|     events.close();
 | |
|     // Try to reconnect after 1 second in case of errors
 | |
|     setTimeout(connectToEvents, 1000);
 | |
|   };
 | |
|   events.onopen = () => {
 | |
|     console.log('Connected to live reload server');
 | |
|   };
 | |
| };
 | |
| 
 | |
| setTimeout(connectToEvents, 500);
 | 
