- commit
- 431c9337e3eccd117e34422cb36b6aadf81c7a2e
- parent
- a549d7ec148a43c9e80fea34626d0447e0c298fc
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2020-10-15 10:40
implement history optimization
Diffstat
| M | static/pad.js | 4 | ++++ |
| M | static/via.js | 14 | +++++++++++++- |
2 files changed, 17 insertions, 1 deletions
diff --git a/static/pad.js b/static/pad.js
@@ -74,6 +74,10 @@ el.addEventListener('input', function() {
74 74 via.listen(room, function(msg) {
75 75 if (msg[1] === 'open') {
76 76 el.readOnly = false;
-1 77 } else if (msg[1] === 'optimize' && !el.readOnly) {
-1 78 var change = diff.diff('', el.value, 3);
-1 79 var data = [id, 'changes', [change]];
-1 80 via.put(room, data, msg[2]);
77 81 } else if (msg[1] === 'changes' && !el.readOnly) {
78 82 if (msg[0] === id) {
79 83 stagedChanges = [];
diff --git a/static/via.js b/static/via.js
@@ -6,8 +6,20 @@ export var send = function(key, data) {
6 6 return fetch(url, {method: 'POST', body: body});
7 7 };
8 8
-1 9 export var put = function(key, data, lastEventId) {
-1 10 var url = baseUrl + 'hmsg/' + key;
-1 11 var body = JSON.stringify(data);
-1 12 return fetch(url, {method: 'PUT', body: body, headers: {'Last-Event-ID': lastEventId}});
-1 13 };
-1 14
9 15 export var listen = function(key, fn) {
10 16 var evtSource = new EventSource(baseUrl + 'hmsg/' + key);
11 -1 evtSource.onmessage = msg => fn(JSON.parse(msg.data));
-1 17 evtSource.onmessage = function(msg) {
-1 18 fn(JSON.parse(msg.data));
-1 19
-1 20 if (Math.random() < 0.05) {
-1 21 fn([null, 'optimize', msg.lastEventId]);
-1 22 }
-1 23 };
12 24 evtSource.onopen = () => fn([null, 'open']);
13 25 };