- commit
- 93d82e55f15255aff68a5c7852206edbd4e16f24
- parent
- cbad3c4d4d2d46bbc5b1e289657f57f25b5c0e6d
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2020-05-05 17:35
signal: extend for storage
Diffstat
M | static/signal.js | 16 | +++++++++++++--- |
1 files changed, 13 insertions, 3 deletions
diff --git a/static/signal.js b/static/signal.js
@@ -10,15 +10,25 @@ export var randomString = function(length) { 10 10 return result.join(''); 11 11 }; 12 1213 -1 export var post = function(key, data) {14 -1 return fetch(baseUrl + key, {method: 'POST', body: JSON.stringify(data)});-1 13 export var send = function(key, data) { -1 14 return fetch(baseUrl + 'msg/' + key, {method: 'POST', body: JSON.stringify(data)}); 15 15 }; 16 16 17 17 export var listen = function(key, fn) {18 -1 var evtSource = new EventSource(baseUrl + key + '?sse');-1 18 var evtSource = new EventSource(baseUrl + 'msg/' + key); 19 19 evtSource.onmessage = msg => fn(JSON.parse(msg.data)); 20 20 }; 21 21 -1 22 export var store = function(key, data) { -1 23 return navigator.sendBeacon(baseUrl + 'store/' + key, JSON.stringify(data)); -1 24 }; -1 25 -1 26 export var restore = function(key) { -1 27 return fetch(baseUrl + 'store/' + key) -1 28 .then(r => r.text()) -1 29 .then(s => JSON.parse(s)); -1 30 }; -1 31 22 32 if (!location.hash) { 23 33 location.hash = randomString(10); 24 34 }