Diffdown is a real-time collaborative Markdown editor/previewer built on the AT Protocol diffdown.com

fix: extract closeWS helper, flush pending deltas on close and page unload

+17 -2
+17 -2
templates/document_edit.html
··· 539 539 clearTimeout(wsEditTimer); 540 540 pendingDeltas = []; 541 541 stopHeartbeat(); 542 + ws = null; 542 543 updatePresence([]); 543 544 scheduleReconnect(); 544 545 }; 545 546 546 547 ws.onerror = () => { 547 - ws.close(); 548 + closeWS(); 548 549 }; 549 550 } 550 551 ··· 563 564 ws.send(JSON.stringify({ type: 'ping' })); 564 565 wsMissedPings++; 565 566 if (wsMissedPings >= 3) { 566 - ws.close(); 567 + closeWS(); 567 568 } 568 569 } 569 570 }, 30000); ··· 727 728 pendingDeltas = []; 728 729 } 729 730 731 + function closeWS() { 732 + if (!ws) return; 733 + clearTimeout(wsEditTimer); 734 + flushDeltas(); // send any buffered deltas before closing 735 + ws.close(); 736 + ws = null; 737 + clearInterval(wsPingTimer); 738 + } 739 + 730 740 // sendEdit sends a full-document replacement via the granular-delta path. 731 741 // to: -1 is a sentinel meaning "end of document" — the server OT engine 732 742 // clamps it to len(documentText), and applyRemoteEdit clamps it to docLen. ··· 874 884 connectWebSocket(); 875 885 loadComments(); 876 886 } 887 + 888 + window.addEventListener('beforeunload', () => { 889 + clearTimeout(wsEditTimer); 890 + flushDeltas(); 891 + }); 877 892 </script> 878 893 {{end}}