pad

minimal etherpad alternative  https://pad.ce9e.org
git clone https://git.ce9e.org/pad.git

commit
eb34f1a032843db8bcee2f2ce1ca09f0dad0dc5d
parent
5ac276ac9a76a78b62d227e1fb43b2fb5e361e83
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2025-06-06 12:28
consolidate after post

- uses the new historyRemaining field from via
- deterministic
- only the client that posted the last messages does consolidation
- if more clients send messages at the same time, they may still all do
  consolidation, but less likely than before
- there is a buffer of 10 history entries in case the client disconnects
  before it can consolidate

Diffstat

M static/pad.js 31 +++++++++++++++++++------------

1 files changed, 19 insertions, 12 deletions


diff --git a/static/pad.js b/static/pad.js

@@ -14,6 +14,7 @@ var old = el.value;
   14    14 var localChanges = [];
   15    15 var stagedChanges = [];
   16    16 var failedRequests = 0;
   -1    17 var lastEventId = 0;
   17    18 
   18    19 document.title += ' - ' + location.hash.slice(1);
   19    20 
@@ -25,6 +26,16 @@ var exit = function() {
   25    26 	}
   26    27 };
   27    28 
   -1    29 var fetchJSON = function(res, options) {
   -1    30 	return fetch(res, options).then(response => {
   -1    31 		if (response.ok) {
   -1    32 			return response.json();
   -1    33 		} else {
   -1    34 			throw response;
   -1    35 		}
   -1    36 	});
   -1    37 };
   -1    38 
   28    39 var sendChanges = utils.throttled(function() {
   29    40 	if (stagedChanges.length) {
   30    41 		setTimeout(sendChanges, 500);
@@ -32,16 +43,15 @@ var sendChanges = utils.throttled(function() {
   32    43 		stagedChanges = localChanges;
   33    44 		localChanges = [];
   34    45 		var data = [id, 'changes', stagedChanges];
   35    -1 		return fetch(url, {
   -1    46 		return fetchJSON(url, {
   36    47 			method: 'POST',
   37    48 			body: JSON.stringify(data),
   38    -1 		}).then(response => {
   39    -1 			if (response.ok) {
   40    -1 				failedRequests = 0;
   41    -1 			} else {
   42    -1 				throw response;
   -1    49 		}).then(responseData => {
   -1    50 			failedRequests = 0;
   -1    51 			if (responseData.historyRemaining < 10) {
   -1    52 				consolidate();
   43    53 			}
   44    -1 		}).catch(() => {
   -1    54 		}, () => {
   45    55 			if (failedRequests > 3) {
   46    56 				exit();
   47    57 			} else {
@@ -94,7 +104,7 @@ var handleMessage = function(msg) {
   94   104 	}
   95   105 };
   96   106 
   97    -1 var consolidate = function(lastEventId) {
   -1   107 var consolidate = function() {
   98   108 	var text = el.value;
   99   109 	var myChanges = [].concat(stagedChanges, localChanges);
  100   110 
@@ -131,10 +141,7 @@ eventSource.onopen = function() {
  131   141 };
  132   142 eventSource.onmessage = function(event) {
  133   143 	handleMessage(JSON.parse(event.data));
  134    -1 
  135    -1 	if (Math.random() < 0.05) {
  136    -1 		consolidate(event.lastEventId);
  137    -1 	}
   -1   144 	lastEventId = event.lastEventId;
  138   145 };
  139   146 
  140   147 window.addEventListener('offline', exit);