@@ -10,29 +10,45 @@ window.webxdc = (() => {
1010 } else if ( event . key === updatesKey ) {
1111 var updates = JSON . parse ( event . newValue ) ;
1212 var update = updates [ updates . length - 1 ] ;
13+ update . max_serial = updates . length ;
1314 console . log ( "[Webxdc] " + JSON . stringify ( update ) ) ;
1415 updateListener ( update ) ;
1516 }
1617 } ) ;
1718
19+ function getUpdates ( ) {
20+ var updatesJSON = window . localStorage . getItem ( updatesKey ) ;
21+ return updatesJSON ? JSON . parse ( updatesJSON ) : [ ] ;
22+ }
23+
1824 var params = new URLSearchParams ( window . location . hash . substr ( 1 ) ) ;
1925 return {
2026 selfAddr : params . get ( "addr" ) || "device0@local.host" ,
2127 selfName : params . get ( "name" ) || "device0" ,
22- setUpdateListener : ( cb ) => ( updateListener = cb ) ,
28+ setUpdateListener : ( cb , serial ) => {
29+ var updates = getUpdates ( ) ;
30+ var maxSerial = updates . length ;
31+ updates . forEach ( ( update ) => {
32+ if ( update . serial > serial ) {
33+ update . max_serial = maxSerial ;
34+ cb ( update ) ;
35+ }
36+ } ) ;
37+ updateListener = cb ;
38+ } ,
2339 getAllUpdates : ( ) => {
24- var updatesJSON = window . localStorage . getItem ( updatesKey ) ;
25- return Promise . resolve ( updatesJSON ? JSON . parse ( updatesJSON ) : [ ] ) ;
40+ console . log ( '[Webxdc] WARNING: getAllUpdates() is deprecated.' ) ;
41+ return Promise . resolve ( [ ] ) ;
2642 } ,
2743 sendUpdate : ( update , description ) => {
28- // alert(description+"\n\n"+JSON.stringify(payload));
29- update = { payload : update . payload , summary : update . summary , info : update . info }
30- console . log ( '[Webxdc] description="' + description + '", ' + JSON . stringify ( update ) ) ;
31- updateListener ( update ) ;
32- var updatesJSON = window . localStorage . getItem ( updatesKey ) ;
33- var updates = updatesJSON ? JSON . parse ( updatesJSON ) : [ ] ;
34- updates . push ( update ) ;
44+ var updates = getUpdates ( ) ;
45+ var serial = updates . length + 1 ;
46+ var _update = { payload : update . payload , summary : update . summary , info : update . info , serial : serial } ;
47+ updates . push ( _update ) ;
3548 window . localStorage . setItem ( updatesKey , JSON . stringify ( updates ) ) ;
49+ _update . max_serial = serial ;
50+ console . log ( '[Webxdc] description="' + description + '", ' + JSON . stringify ( _update ) ) ;
51+ updateListener ( _update ) ;
3652 } ,
3753 } ;
3854} ) ( ) ;
0 commit comments