55 * LICENSE file in the root directory of this source tree.
66 *
77 * @format
8- */
8+ */
99
1010import { checkFetchExists , patchFetchPolyfill } from './patchFetchPolyfill' ;
1111
@@ -29,10 +29,10 @@ import { checkFetchExists, patchFetchPolyfill } from './patchFetchPolyfill';
2929export default class DeltaPatcher {
3030 constructor ( ) {
3131 this . _lastBundle = {
32+ id : undefined ,
3233 pre : new Map ( ) ,
3334 post : new Map ( ) ,
3435 modules : new Map ( ) ,
35- id : undefined ,
3636 } ;
3737 this . _initialized = false ;
3838 this . _lastNumModifiedFiles = 0 ;
@@ -54,35 +54,65 @@ export default class DeltaPatcher {
5454 * Applies a Delta Bundle to the current bundle.
5555 */
5656 applyDelta ( deltaBundle ) {
57+ const isOld = deltaBundle . id ;
5758 // Make sure that the first received delta is a fresh one.
58- if ( ! this . _initialized && ! deltaBundle . reset ) {
59+ if ( isOld ? ! this . _initialized && ! deltaBundle . reset :
60+ ! this . _initialized && ! deltaBundle . base ) {
5961 throw new Error ( 'DeltaPatcher should receive a fresh Delta when being initialized' ) ;
6062 }
6163
6264 this . _initialized = true ;
6365
6466 // Reset the current delta when we receive a fresh delta.
65- if ( deltaBundle . reset ) {
67+ if ( deltaBundle . reset && isOld ) {
6668 this . _lastBundle = {
6769 pre : new Map ( ) ,
6870 post : new Map ( ) ,
6971 modules : new Map ( ) ,
7072 id : undefined ,
7173 } ;
74+ } else if ( deltaBundle . base ) {
75+ this . _lastBundle = {
76+ id : deltaBundle . revisionId ,
77+ pre : deltaBundle . pre ,
78+ post : deltaBundle . post ,
79+ modules : new Map ( deltaBundle . modules ) ,
80+ } ;
81+ }
82+
83+ this . _lastNumModifiedFiles = isOld ?
84+ deltaBundle . pre . size + deltaBundle . post . size + deltaBundle . delta . size :
85+ deltaBundle . modules . length ;
86+
87+ if ( deltaBundle . deleted ) {
88+ this . _lastNumModifiedFiles += deltaBundle . deleted . length ;
7289 }
7390
74- this . _lastNumModifiedFiles =
75- deltaBundle . pre . size + deltaBundle . post . size + deltaBundle . delta . size ;
91+ this . _lastBundle . id = isOld ? deltaBundle . id : deltaBundle . revisionId ;
7692
7793 if ( this . _lastNumModifiedFiles > 0 ) {
7894 this . _lastModifiedDate = new Date ( ) ;
7995 }
8096
81- this . _patchMap ( this . _lastBundle . pre , deltaBundle . pre ) ;
82- this . _patchMap ( this . _lastBundle . post , deltaBundle . post ) ;
83- this . _patchMap ( this . _lastBundle . modules , deltaBundle . delta ) ;
97+ if ( isOld ) {
98+ this . _patchMap ( this . _lastBundle . pre , deltaBundle . pre ) ;
99+ this . _patchMap ( this . _lastBundle . post , deltaBundle . post ) ;
100+ this . _patchMap ( this . _lastBundle . modules , deltaBundle . delta ) ;
101+
102+ this . _lastBundle . id = deltaBundle . id ;
103+ } else {
104+ for ( const [ key , value ] of deltaBundle . modules ) {
105+ this . _lastBundle . modules . set ( key , value ) ;
106+ }
84107
85- this . _lastBundle . id = deltaBundle . id ;
108+ if ( deltaBundle . deleted ) {
109+ for ( const id of deltaBundle . deleted ) {
110+ this . _lastBundle . modules . delete ( id ) ;
111+ }
112+ }
113+
114+ this . _lastBundle . id = deltaBundle . revisionId ;
115+ }
86116
87117 return this ;
88118 }
@@ -105,11 +135,15 @@ export default class DeltaPatcher {
105135 return this . _lastModifiedDate ;
106136 }
107137
108- getAllModules ( ) {
109- return [ ] . concat (
138+ getAllModules ( isOld ) {
139+ return isOld ? [ ] . concat (
110140 Array . from ( this . _lastBundle . pre . values ( ) ) ,
111141 Array . from ( this . _lastBundle . modules . values ( ) ) ,
112142 Array . from ( this . _lastBundle . post . values ( ) )
143+ ) : [ ] . concat (
144+ [ this . _lastBundle . pre ] ,
145+ Array . from ( this . _lastBundle . modules . values ( ) ) ,
146+ [ this . _lastBundle . post ]
113147 ) ;
114148 }
115149
0 commit comments