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>";
|
document.getElementById('output').innerHTML += "<strong><" + update.payload.name + "></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
14
webxdc.d.ts
vendored
|
@ -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>[]>;
|
||||||
/**
|
/**
|
||||||
|
|
36
webxdc.js
36
webxdc.js
|
@ -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);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Reference in a new issue