From d869b5a085465338a504a62faa5bbe4a18f34f72 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Tue, 22 Feb 2022 13:58:37 -0500 Subject: [PATCH] adapt to new webxdc API, deprecate getAllUpdates() --- index.html | 3 +-- webxdc.d.ts | 14 +++++++++----- webxdc.js | 36 ++++++++++++++++++++++++++---------- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/index.html b/index.html index 9604759..f50ead3 100644 --- a/index.html +++ b/index.html @@ -18,8 +18,7 @@ document.getElementById('output').innerHTML += "<" + update.payload.name + "> " + update.payload.msg + "
"; } - window.webxdc.setUpdateListener(receiveUpdate); - window.webxdc.getAllUpdates().then(updates => updates.forEach(receiveUpdate)); + window.webxdc.setUpdateListener(receiveUpdate, 0); diff --git a/webxdc.d.ts b/webxdc.d.ts index d672413..51f4b9c 100644 --- a/webxdc.d.ts +++ b/webxdc.d.ts @@ -18,6 +18,10 @@ type SendingStatusUpdate = { type ReceivedStatusUpdate = { /** 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 { @@ -28,13 +32,13 @@ interface Webxdc { /** 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) => void): void; + setUpdateListener(cb: (statusUpdate: ReceivedStatusUpdate) => 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[]>; /** diff --git a/webxdc.js b/webxdc.js index a0808d7..7c27cc4 100644 --- a/webxdc.js +++ b/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); }, }; })();