From a69b1d0c580bc779740ef79a7d16b69229896785 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 28 Jan 2023 20:31:24 +0100 Subject: Client to Server: send diffs instead of whole file --- html/whiteboard.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'html/whiteboard.js') diff --git a/html/whiteboard.js b/html/whiteboard.js index aef4391..58023fe 100644 --- a/html/whiteboard.js +++ b/html/whiteboard.js @@ -4,6 +4,8 @@ function init() { } var revision; +var baseline = ""; // data contents relating to revision, acknowledged by server +var baseline_candidate = ""; // will become baseline, after ack by server // helper for breaking feedback loop var caretpos = 0; @@ -42,6 +44,7 @@ function on_getfile(data, rev, pos) board.value = data; } revision = rev; + baseline = data; textAreaSetPos("board", pos); } @@ -72,6 +75,7 @@ function on_version(version) function on_modify_ack(rev) { revision = rev; + baseline = baseline_candidate; } function on_message(e) { @@ -158,9 +162,6 @@ function init_board() { Module.onRuntimeInitialized = () => { connect_websocket(); }; - //Module.onRuntimeInitialized = () => { alert("DEBUG: " + Module._getnum(1) + " " + UTF8ToString(Module._getstring(allocateUTF8("abc")))); - //_free(allocateUTF8("abc")); - //}; var board = document.getElementById("board"); board.addEventListener("input", function() {on_input(); }); @@ -219,7 +220,25 @@ function on_input() dataElement.appendChild(document.createTextNode(document.getElementById("board").value)); requestElement.appendChild(dataElement); - var posElement = xmlDocument.createElement("pos"); + baseline_candidate = document.getElementById("board").value; + + var revisionElement = xmlDocument.createElement("baserev"); + revisionElement.appendChild(document.createTextNode(revision)); + requestElement.appendChild(revisionElement); + + //Module.onRuntimeInitialized = () => { alert("DEBUG: " + Module._getnum(1) + " " + UTF8ToString(Module._getstring(allocateUTF8("abc")))); + //_free(allocateUTF8("abc")); + //}; + var old_version = allocateUTF8(baseline); + var new_version = allocateUTF8(baseline_candidate); + var diff = Module._diff_create(old_version, new_version); + var diffDocument = parser.parseFromString(UTF8ToString(diff), "text/xml"); + _free(old_version); + _free(new_version); + _free(diff); + requestElement.appendChild(xmlDocument.importNode(diffDocument.getElementsByTagName("diff")[0], true)); + + var posElement = xmlDocument.createElement("pos"); posElement.appendChild(document.createTextNode(document.getElementById("board").selectionStart)); requestElement.appendChild(posElement); -- cgit v1.2.3