From 7263ac52c0c259bdea67b5eec5f52fc199136c28 Mon Sep 17 00:00:00 2001 From: WofWca Date: Sun, 14 Jul 2024 12:52:50 +0400 Subject: [PATCH] simplify `setUpdateListener` example --- src-docs/spec/setUpdateListener.md | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/src-docs/spec/setUpdateListener.md b/src-docs/spec/setUpdateListener.md index 0318f4c..7ace51c 100644 --- a/src-docs/spec/setUpdateListener.md +++ b/src-docs/spec/setUpdateListener.md @@ -32,43 +32,28 @@ Each `update` which is passed to the callback comes with the following propertie Example: ```js -let myDocumentState = localStorage.getItem("myDocumentState") ?? ""; - -let initialPendingUpdatesHandled = false; +let myDocumentState = ""; const initialPendingUpdatesHandledPromise = window.webxdc.setUpdateListener( (update) => { // Remember that the listener is invoked for // your own `window.webxdc.sendUpdate()` calls as well! - applyDocumentUpdate(update.payload); - localStorage.setItem("myDocumentState", myDocumentState); - localStorage.setItem("lastHandledUpdateSerial", update.serial); + // Dummy document update logic. + // Yours might be more complex, + // such as applying a chess move to the board. + myDocumentState = myDocumentUpdate; const areMoreUpdatesPending = update.serial !== update.max_serial; - if ( - !areMoreUpdatesPending && - // We'll make the initial render when the promise resolves, - // because if there are no pending updates, - // the listener will not be invoked. - initialPendingUpdatesHandled - ) { + if (!areMoreUpdatesPending) { renderDocument(); } - }, - parseInt(localStorage.getItem("lastHandledUpdateSerial") ?? "0") + } ); initialPendingUpdatesHandledPromise.then(() => { - initialPendingUpdatesHandled = true; renderDocument(); }); -function applyDocumentUpdate(myDocumentUpdate) { - // Dummy `applyDocumentUpdate` logic. - // Yours might be more complex, - // such as applying a chess move to the board. - myDocumentState = myDocumentUpdate; -} // Let's only call this when there are no pending updates. function renderDocument() { document.body.innerText = myDocumentState;