Skip to content

Commit 6ed5164

Browse files
committed
remove getAllUpdates() usage, adapt to new API
1 parent 84e9537 commit 6ed5164

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

app.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ def _onload():
3434
+ '<p id="msgs"></p>'
3535
)
3636

37-
window.webxdc.setUpdateListener(receiveUpdate) # process incoming messages
38-
window.webxdc.getAllUpdates().then(lambda updates: updates.forEach(receiveUpdate)) # restore app state
37+
window.webxdc.setUpdateListener(receiveUpdate, 0) # process incoming messages
3938

4039

4140
window.onload = _onload

webxdc.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,45 @@ window.webxdc = (() => {
1010
} else if (event.key === updatesKey) {
1111
var updates = JSON.parse(event.newValue);
1212
var update = updates[updates.length-1];
13+
update.max_serial = updates.length;
1314
console.log("[Webxdc] " + JSON.stringify(update));
1415
updateListener(update);
1516
}
1617
});
1718

19+
function getUpdates() {
20+
var updatesJSON = window.localStorage.getItem(updatesKey);
21+
return updatesJSON ? JSON.parse(updatesJSON) : [];
22+
}
23+
1824
var params = new URLSearchParams(window.location.hash.substr(1));
1925
return {
2026
selfAddr: params.get("addr") || "device0@local.host",
2127
selfName: params.get("name") || "device0",
22-
setUpdateListener: (cb) => (updateListener = cb),
28+
setUpdateListener: (cb, serial) => {
29+
var updates = getUpdates();
30+
var maxSerial = updates.length;
31+
updates.forEach((update) => {
32+
if (update.serial > serial) {
33+
update.max_serial = maxSerial;
34+
cb(update);
35+
}
36+
});
37+
updateListener = cb;
38+
},
2339
getAllUpdates: () => {
24-
var updatesJSON = window.localStorage.getItem(updatesKey);
25-
return Promise.resolve(updatesJSON ? JSON.parse(updatesJSON) : []);
40+
console.log('[Webxdc] WARNING: getAllUpdates() is deprecated.');
41+
return Promise.resolve([]);
2642
},
2743
sendUpdate: (update, description) => {
28-
// alert(description+"\n\n"+JSON.stringify(payload));
29-
update = {payload: update.payload, summary: update.summary, info: update.info}
30-
console.log('[Webxdc] description="' + description + '", ' + JSON.stringify(update));
31-
updateListener(update);
32-
var updatesJSON = window.localStorage.getItem(updatesKey);
33-
var updates = updatesJSON ? JSON.parse(updatesJSON) : [];
34-
updates.push(update);
44+
var updates = getUpdates();
45+
var serial = updates.length + 1;
46+
var _update = {payload: update.payload, summary: update.summary, info: update.info, serial: serial};
47+
updates.push(_update);
3548
window.localStorage.setItem(updatesKey, JSON.stringify(updates));
49+
_update.max_serial = serial;
50+
console.log('[Webxdc] description="' + description + '", ' + JSON.stringify(_update));
51+
updateListener(_update);
3652
},
3753
};
3854
})();

0 commit comments

Comments
 (0)