@@ -32,43 +32,28 @@ Each `update` which is passed to the callback comes with the following propertie
32
32
Example:
33
33
34
34
``` js
35
- let myDocumentState = localStorage .getItem (" myDocumentState" ) ?? " " ;
36
-
37
- let initialPendingUpdatesHandled = false ;
35
+ let myDocumentState = " " ;
38
36
const initialPendingUpdatesHandledPromise = window .webxdc .setUpdateListener (
39
37
(update ) => {
40
38
// Remember that the listener is invoked for
41
39
// your own `window.webxdc.sendUpdate()` calls as well!
42
40
43
- applyDocumentUpdate (update .payload );
44
- localStorage .setItem (" myDocumentState" , myDocumentState);
45
- localStorage .setItem (" lastHandledUpdateSerial" , update .serial );
41
+ // Dummy document update logic.
42
+ // Yours might be more complex,
43
+ // such as applying a chess move to the board.
44
+ myDocumentState = myDocumentUpdate;
46
45
47
46
const areMoreUpdatesPending = update .serial !== update .max_serial ;
48
- if (
49
- ! areMoreUpdatesPending &&
50
- // We'll make the initial render when the promise resolves,
51
- // because if there are no pending updates,
52
- // the listener will not be invoked.
53
- initialPendingUpdatesHandled
54
- ) {
47
+ if (! areMoreUpdatesPending) {
55
48
renderDocument ();
56
49
}
57
- },
58
- parseInt (localStorage .getItem (" lastHandledUpdateSerial" ) ?? " 0" )
50
+ }
59
51
);
60
52
61
53
initialPendingUpdatesHandledPromise .then (() => {
62
- initialPendingUpdatesHandled = true ;
63
54
renderDocument ();
64
55
});
65
56
66
- function applyDocumentUpdate (myDocumentUpdate ) {
67
- // Dummy `applyDocumentUpdate` logic.
68
- // Yours might be more complex,
69
- // such as applying a chess move to the board.
70
- myDocumentState = myDocumentUpdate;
71
- }
72
57
// Let's only call this when there are no pending updates.
73
58
function renderDocument () {
74
59
document .body .innerText = myDocumentState;
0 commit comments