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>";
}
window.webxdc.setUpdateListener(receiveUpdate);
window.webxdc.getAllUpdates().then(updates => updates.forEach(receiveUpdate));
window.webxdc.setUpdateListener(receiveUpdate, 0);
</script>
</body>
</html>

14
webxdc.d.ts vendored
View File

@ -18,6 +18,10 @@ type SendingStatusUpdate<T> = {
type ReceivedStatusUpdate<T> = {
/** the payload, deserialized json */
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> {
@ -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. */
selfName: string;
/**
* set a listener for new status updates
* note that own status updates, that you send with {@link sendUpdate}, also trigger this method
* set a listener for new status updates.
* 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,
* 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.
* WARNING! This function is deprecated, see setUpdateListener().
*/
getAllUpdates(): Promise<ReceivedStatusUpdate<T>[]>;
/**

View File

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