- commit
- ab9983aa6c2236a9f1e393809d8b53d146157945
- parent
- 431c9337e3eccd117e34422cb36b6aadf81c7a2e
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2020-10-15 10:56
inline via.js
Diffstat
M | static/pad.js | 56 | +++++++++++++++++++++++++++++++++++++++----------------- |
D | static/via.js | 25 | ------------------------- |
2 files changed, 39 insertions, 42 deletions
diff --git a/static/pad.js b/static/pad.js
@@ -1,12 +1,12 @@ 1 1 import * as utils from './utils.js'; 2 2 import * as diff from './diff.js';3 -1 import * as via from './via.js';4 3 5 4 if (!location.hash) { 6 5 location.hash = utils.randomString(10); 7 6 } 8 7 9 8 var room = 'pad/' + location.hash.slice(1); -1 9 var url = 'https://via.ce9e.org/hmsg/' + room; 10 10 var id = utils.randomString(6); 11 11 12 12 var el = document.querySelector('textarea'); @@ -23,7 +23,11 @@ var sendChanges = utils.throttled(function() { 23 23 } else { 24 24 stagedChanges = localChanges; 25 25 localChanges = [];26 -1 via.send(room, [id, 'changes', stagedChanges]).catch(() => {-1 26 var data = [id, 'changes', stagedChanges]; -1 27 return fetch(url, { -1 28 method: 'POST', -1 29 body: JSON.stringify(data), -1 30 }).catch(() => { 27 31 localChanges = stagedChanges.concat(localChanges); 28 32 stagedChanges = []; 29 33 sendChanges(); @@ -64,29 +68,47 @@ var applyChanges = function(changes) { 64 68 } 65 69 }; 66 7067 -1 el.addEventListener('input', function() {68 -1 var change = diff.diff(old, el.value, 3);69 -1 diff.pushChange(localChanges, change);70 -1 old = el.value;71 -1 sendChanges();72 -1 });73 -174 -1 via.listen(room, function(msg) {75 -1 if (msg[1] === 'open') {76 -1 el.readOnly = false;77 -1 } else if (msg[1] === 'optimize' && !el.readOnly) {78 -1 var change = diff.diff('', el.value, 3);79 -1 var data = [id, 'changes', [change]];80 -1 via.put(room, data, msg[2]);81 -1 } else if (msg[1] === 'changes' && !el.readOnly) {-1 71 var handleMessage = function(msg) { -1 72 if (msg[1] === 'changes') { 82 73 if (msg[0] === id) { 83 74 stagedChanges = []; 84 75 } else { 85 76 applyChanges(msg[2]); 86 77 } 87 78 } -1 79 }; -1 80 -1 81 var optimize = function(lastEventId) { -1 82 var change = diff.diff('', el.value, 3); -1 83 var data = [id, 'changes', [change]]; -1 84 fetch(url, { -1 85 method: 'PUT', -1 86 body: JSON.stringify(data), -1 87 headers: {'Last-Event-ID': lastEventId}, -1 88 }); -1 89 }; -1 90 -1 91 el.addEventListener('input', function() { -1 92 var change = diff.diff(old, el.value, 3); -1 93 diff.pushChange(localChanges, change); -1 94 old = el.value; -1 95 sendChanges(); 88 96 }); 89 97 90 98 window.addEventListener('offline', function() { 91 99 el.readOnly = true; 92 100 }); -1 101 -1 102 var evtSource = new EventSource(url); -1 103 evtSource.onopen = function() { -1 104 el.readOnly = false; -1 105 }; -1 106 evtSource.onmessage = function(event) { -1 107 if (!el.readOnly) { -1 108 handleMessage(JSON.parse(event.data)); -1 109 -1 110 if (Math.random() < 0.05) { -1 111 optimize(event.lastEventId); -1 112 } -1 113 } -1 114 };
diff --git a/static/via.js b/static/via.js
@@ -1,25 +0,0 @@1 -1 var baseUrl = 'https://via.ce9e.org/';2 -13 -1 export var send = function(key, data) {4 -1 var url = baseUrl + 'hmsg/' + key;5 -1 var body = JSON.stringify(data);6 -1 return fetch(url, {method: 'POST', body: body});7 -1 };8 -19 -1 export var put = function(key, data, lastEventId) {10 -1 var url = baseUrl + 'hmsg/' + key;11 -1 var body = JSON.stringify(data);12 -1 return fetch(url, {method: 'PUT', body: body, headers: {'Last-Event-ID': lastEventId}});13 -1 };14 -115 -1 export var listen = function(key, fn) {16 -1 var evtSource = new EventSource(baseUrl + 'hmsg/' + key);17 -1 evtSource.onmessage = function(msg) {18 -1 fn(JSON.parse(msg.data));19 -120 -1 if (Math.random() < 0.05) {21 -1 fn([null, 'optimize', msg.lastEventId]);22 -1 }23 -1 };24 -1 evtSource.onopen = () => fn([null, 'open']);25 -1 };