Merge pull request #20 from deltachat/delete_getallupdates

adapt to new webxdc API, deprecate getAllUpdates()
master
Asiel Díaz Benítez 2022-03-14 02:14:51 -04:00 committed by GitHub
commit c3467cca8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 17 deletions

View File

@ -18,8 +18,7 @@
document.getElementById('output').innerHTML += "<strong>&lt;" + update.payload.name + "&gt;</strong> " + update.payload.msg + "<br>"; document.getElementById('output').innerHTML += "<strong>&lt;" + update.payload.name + "&gt;</strong> " + update.payload.msg + "<br>";
} }
window.webxdc.setUpdateListener(receiveUpdate); window.webxdc.setUpdateListener(receiveUpdate, 0);
window.webxdc.getAllUpdates().then(updates => updates.forEach(receiveUpdate));
</script> </script>
</body> </body>
</html> </html>

14
webxdc.d.ts vendored
View File

@ -18,6 +18,10 @@ type SendingStatusUpdate<T> = {
type ReceivedStatusUpdate<T> = { type ReceivedStatusUpdate<T> = {
/** the payload, deserialized json */ /** the payload, deserialized json */
payload: T; payload: T;
/** the serial number of this update. Serials are larger than 0 and newer serials have higher numbers */
serial: number;
/** the maximum serial currently known */
max_serial: number;
}; };
interface Webxdc<T> { interface Webxdc<T> {
@ -28,13 +32,13 @@ interface Webxdc<T> {
/** Returns the peer's own name. This is name chosen by the user in their settings, if there is nothing set, that defaults to the peer's address. */ /** Returns the peer's own name. This is name chosen by the user in their settings, if there is nothing set, that defaults to the peer's address. */
selfName: string; selfName: string;
/** /**
* set a listener for new status updates * set a listener for new status updates.
* note that own status updates, that you send with {@link sendUpdate}, also trigger this method * The "serial" specifies the last serial that you know about (defaults to 0).
* Note that own status updates, that you send with {@link sendUpdate}, also trigger this method
* */ * */
setUpdateListener(cb: (statusUpdate: ReceivedStatusUpdate<T>) => void): void; setUpdateListener(cb: (statusUpdate: ReceivedStatusUpdate<T>) => void, serial: number): void;
/** /**
* In case your Webxdc was just started, * WARNING! This function is deprecated, see setUpdateListener().
* you may want to reconstruct the status from the last run - and also incorporate updates that may have arrived while the app was not running.
*/ */
getAllUpdates(): Promise<ReceivedStatusUpdate<T>[]>; getAllUpdates(): Promise<ReceivedStatusUpdate<T>[]>;
/** /**

View File

@ -10,29 +10,45 @@ window.webxdc = (() => {
} else if (event.key === updatesKey) { } else if (event.key === updatesKey) {
var updates = JSON.parse(event.newValue); var updates = JSON.parse(event.newValue);
var update = updates[updates.length-1]; var update = updates[updates.length-1];
update.max_serial = updates.length;
console.log("[Webxdc] " + JSON.stringify(update)); console.log("[Webxdc] " + JSON.stringify(update));
updateListener(update); updateListener(update);
} }
}); });
function getUpdates() {
var updatesJSON = window.localStorage.getItem(updatesKey);
return updatesJSON ? JSON.parse(updatesJSON) : [];
}
var params = new URLSearchParams(window.location.hash.substr(1)); var params = new URLSearchParams(window.location.hash.substr(1));
return { return {
selfAddr: params.get("addr") || "device0@local.host", selfAddr: params.get("addr") || "device0@local.host",
selfName: params.get("name") || "device0", selfName: params.get("name") || "device0",
setUpdateListener: (cb) => (updateListener = cb), setUpdateListener: (cb, serial) => {
var updates = getUpdates();
var maxSerial = updates.length;
updates.forEach((update) => {
if (update.serial > serial) {
update.max_serial = maxSerial;
cb(update);
}
});
updateListener = cb;
},
getAllUpdates: () => { getAllUpdates: () => {
var updatesJSON = window.localStorage.getItem(updatesKey); console.log('[Webxdc] WARNING: getAllUpdates() is deprecated.');
return Promise.resolve(updatesJSON ? JSON.parse(updatesJSON) : []); return Promise.resolve([]);
}, },
sendUpdate: (update, description) => { sendUpdate: (update, description) => {
// alert(description+"\n\n"+JSON.stringify(payload)); var updates = getUpdates();
update = {payload: update.payload, summary: update.summary, info: update.info} var serial = updates.length + 1;
console.log('[Webxdc] description="' + description + '", ' + JSON.stringify(update)); var _update = {payload: update.payload, summary: update.summary, info: update.info, serial: serial};
updateListener(update); updates.push(_update);
var updatesJSON = window.localStorage.getItem(updatesKey);
var updates = updatesJSON ? JSON.parse(updatesJSON) : [];
updates.push(update);
window.localStorage.setItem(updatesKey, JSON.stringify(updates)); window.localStorage.setItem(updatesKey, JSON.stringify(updates));
_update.max_serial = serial;
console.log('[Webxdc] description="' + description + '", ' + JSON.stringify(_update));
updateListener(_update);
}, },
}; };
})(); })();