@@ -5178,19 +5178,29 @@ async function fetchTracks() {
51785178 paths = newPaths ;
51795179 } else {
51805180 for ( const mmsi in newPaths ) {
5181+ const newer = newPaths [ mmsi ] ;
51815182 if ( ! paths [ mmsi ] ) {
5182- paths [ mmsi ] = newPaths [ mmsi ] ;
5183- } else {
5184- for ( const pt of newPaths [ mmsi ] ) {
5185- const ts_start = pt [ 2 ] ;
5186- const idx = paths [ mmsi ] . findIndex ( p => p [ 2 ] === ts_start ) ;
5187- if ( idx >= 0 ) {
5188- paths [ mmsi ] [ idx ] [ 3 ] = pt [ 3 ] ;
5189- } else {
5190- paths [ mmsi ] . unshift ( pt ) ;
5191- }
5183+ paths [ mmsi ] = newer ;
5184+ continue ;
5185+ }
5186+ const older = paths [ mmsi ] ;
5187+ const merged = [ ] ;
5188+ let i = 0 , j = 0 ;
5189+ while ( i < newer . length && j < older . length ) {
5190+ const a = newer [ i ] , b = older [ j ] ;
5191+ if ( a [ 2 ] === b [ 2 ] ) {
5192+ b [ 0 ] = a [ 0 ] ; b [ 1 ] = a [ 1 ] ; b [ 3 ] = a [ 3 ] ;
5193+ merged . push ( b ) ;
5194+ i ++ ; j ++ ;
5195+ } else if ( a [ 2 ] > b [ 2 ] ) {
5196+ merged . push ( a ) ; i ++ ;
5197+ } else {
5198+ merged . push ( b ) ; j ++ ;
51925199 }
51935200 }
5201+ while ( i < newer . length ) merged . push ( newer [ i ++ ] ) ;
5202+ while ( j < older . length ) merged . push ( older [ j ++ ] ) ;
5203+ paths [ mmsi ] = merged ;
51945204 }
51955205 for ( const mmsi in paths ) {
51965206 if ( ! ( mmsi in shipsDB ) ) delete paths [ mmsi ] ;
@@ -5204,14 +5214,6 @@ async function fetchTracks() {
52045214 return false ;
52055215 }
52065216
5207- for ( var mmsi in paths ) {
5208- if ( paths . hasOwnProperty ( mmsi ) ) {
5209- if ( mmsi in shipsDB && paths [ mmsi ] . length > 0 ) {
5210- shipsDB [ mmsi ] . raw . lat = paths [ mmsi ] [ 0 ] [ 0 ] ;
5211- shipsDB [ mmsi ] . raw . lon = paths [ mmsi ] [ 0 ] [ 1 ] ;
5212- }
5213- }
5214- }
52155217 return true ;
52165218}
52175219
0 commit comments