use local storage if possible to persist updates
This commit is contained in:
parent
be5f1b8f99
commit
7eecf6d4ef
69
webxdc.js
69
webxdc.js
|
@ -1,5 +1,53 @@
|
||||||
// debug friend: document.writeln(JSON.stringify(value));
|
// debug friend: document.writeln(JSON.stringify(value));
|
||||||
// maybe helpful: window.sessionStorage, https://www.w3schools.com/html/html5_webstorage.asp
|
window.xdcStorage = (() => {
|
||||||
|
var updatesKey = "__xdcUpdatesKey__";
|
||||||
|
var fakeStorage = {
|
||||||
|
_data: {},
|
||||||
|
|
||||||
|
setItem: function (id, val) {
|
||||||
|
return this._data[id] = String(val);
|
||||||
|
},
|
||||||
|
|
||||||
|
getItem: function (id) {
|
||||||
|
return this._data.hasOwnProperty(id) ? this._data[id] : undefined;
|
||||||
|
},
|
||||||
|
|
||||||
|
removeItem: function (id) {
|
||||||
|
return delete this._data[id];
|
||||||
|
},
|
||||||
|
|
||||||
|
clear: function () {
|
||||||
|
return this._data = {};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var localStorageSupported = () => {
|
||||||
|
var testKey = "__xdcTestKey__";
|
||||||
|
try {
|
||||||
|
var storage = window.localStorage;
|
||||||
|
storage.setItem(testKey, "1");
|
||||||
|
storage.removeItem(testKey);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var storage = localStorageSupported() ? window.localStorage : fakeStorage;
|
||||||
|
|
||||||
|
return {
|
||||||
|
getUpdates: () => {
|
||||||
|
var updatesJSON = storage.getItem(updatesKey);
|
||||||
|
return updatesJSON ? JSON.parse(updatesJSON) : [];
|
||||||
|
},
|
||||||
|
saveUpdate: (update) => {
|
||||||
|
var updates = window.xdcStorage.getUpdates();
|
||||||
|
updates.push(update);
|
||||||
|
storage.setItem(updatesKey, JSON.stringify(updates));
|
||||||
|
},
|
||||||
|
clear: () => {
|
||||||
|
storage.clear();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
||||||
window.webxdc = (() => {
|
window.webxdc = (() => {
|
||||||
var updateListener = () => {};
|
var updateListener = () => {};
|
||||||
|
@ -7,13 +55,11 @@ window.webxdc = (() => {
|
||||||
return {
|
return {
|
||||||
selfAddr: () => window.xdcSelfAddr || "device0@local.host",
|
selfAddr: () => window.xdcSelfAddr || "device0@local.host",
|
||||||
setUpdateListener: (cb) => (window.xdcUpdateListener = cb),
|
setUpdateListener: (cb) => (window.xdcUpdateListener = cb),
|
||||||
getAllUpdates: () => {
|
getAllUpdates: () => {return getXdcRoot().xdcStorage.getUpdates();},
|
||||||
return getXdcRoot().xdcUpdates;
|
|
||||||
},
|
|
||||||
sendUpdate: (description, payload) => {
|
sendUpdate: (description, payload) => {
|
||||||
// alert(description+"\n\n"+JSON.stringify(payload));
|
// alert(description+"\n\n"+JSON.stringify(payload));
|
||||||
var update = {payload: payload};
|
var update = {payload: payload};
|
||||||
getXdcRoot().xdcUpdates.push(update);
|
getXdcRoot().xdcStorage.saveUpdate(update);
|
||||||
var all = getAllXdcWindows();
|
var all = getAllXdcWindows();
|
||||||
all.forEach((w) => {
|
all.forEach((w) => {
|
||||||
//alert(w.xdcUpdateListener);
|
//alert(w.xdcUpdateListener);
|
||||||
|
@ -24,10 +70,9 @@ window.webxdc = (() => {
|
||||||
})();
|
})();
|
||||||
|
|
||||||
window.allXdcWindows = [window];
|
window.allXdcWindows = [window];
|
||||||
window.xdcUpdates = [];
|
|
||||||
window.xdcUpdateListener = 12;
|
window.xdcUpdateListener = 12;
|
||||||
|
|
||||||
var styleControlPanel = 'position: fixed; bottom:1em; left:1em; background-color: #000; opacity:0.8; padding:.5em; font-family: sans-serif; width: 50%;';
|
var styleControlPanel = 'position: fixed; bottom:1em; left:1em; background-color: #000; opacity:0.8; padding:.5em; font-family: sans-serif; width: 50%;color:#fff;';
|
||||||
var styleMenuLink = 'color:#fff; text-decoration: none;';
|
var styleMenuLink = 'color:#fff; text-decoration: none;';
|
||||||
|
|
||||||
function getXdcRoot() {
|
function getXdcRoot() {
|
||||||
|
@ -43,7 +88,7 @@ function getAllXdcWindows() {
|
||||||
return xdcRoot.allXdcWindows;
|
return xdcRoot.allXdcWindows;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addPeer() {
|
function addXdcPeer() {
|
||||||
var xdcChild = window.open(window.location);
|
var xdcChild = window.open(window.location);
|
||||||
var xdcRoot = getXdcRoot();
|
var xdcRoot = getXdcRoot();
|
||||||
xdcChild.xdcRoot = xdcRoot;
|
xdcChild.xdcRoot = xdcRoot;
|
||||||
|
@ -51,6 +96,11 @@ function addPeer() {
|
||||||
xdcRoot.allXdcWindows.push(xdcChild);
|
xdcRoot.allXdcWindows.push(xdcChild);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function clearXdcStorage() {
|
||||||
|
getXdcRoot().xdcStorage.clear();
|
||||||
|
alert("Done.");
|
||||||
|
}
|
||||||
|
|
||||||
function alterApp() {
|
function alterApp() {
|
||||||
var title = document.getElementsByTagName('title')[0];
|
var title = document.getElementsByTagName('title')[0];
|
||||||
if (typeof title == 'undefined') {
|
if (typeof title == 'undefined') {
|
||||||
|
@ -63,7 +113,8 @@ function alterApp() {
|
||||||
var div = document.createElement('div');
|
var div = document.createElement('div');
|
||||||
div.innerHTML =
|
div.innerHTML =
|
||||||
'<div style="' + styleControlPanel + '">' +
|
'<div style="' + styleControlPanel + '">' +
|
||||||
'<a href="javascript:addPeer();" style="' + styleMenuLink + '">Add Peer</a>' +
|
'<a href="javascript:addXdcPeer();" style="' + styleMenuLink + '">Add Peer</a> | ' +
|
||||||
|
'<a href="javascript:clearXdcStorage();" style="' + styleMenuLink + '">Clear Storage</a>' +
|
||||||
'<div>';
|
'<div>';
|
||||||
document.getElementsByTagName('body')[0].append(div.firstChild);
|
document.getElementsByTagName('body')[0].append(div.firstChild);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue