adapt to new webxdc API, deprecate getAllUpdates()
This commit is contained in:
parent
e0ccce45b1
commit
d869b5a085
|
@ -18,8 +18,7 @@
|
|||
document.getElementById('output').innerHTML += "<strong><" + update.payload.name + "></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
14
webxdc.d.ts
vendored
|
@ -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>[]>;
|
||||
/**
|
||||
|
|
36
webxdc.js
36
webxdc.js
|
@ -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);
|
||||
},
|
||||
};
|
||||
})();
|
||||
|
|
Loading…
Reference in a new issue