5
5
* LICENSE file in the root directory of this source tree.
6
6
*
7
7
* @format
8
- */
8
+ */
9
9
10
10
import { checkFetchExists , patchFetchPolyfill } from './patchFetchPolyfill' ;
11
11
@@ -29,10 +29,10 @@ import { checkFetchExists, patchFetchPolyfill } from './patchFetchPolyfill';
29
29
export default class DeltaPatcher {
30
30
constructor ( ) {
31
31
this . _lastBundle = {
32
+ id : undefined ,
32
33
pre : new Map ( ) ,
33
34
post : new Map ( ) ,
34
35
modules : new Map ( ) ,
35
- id : undefined ,
36
36
} ;
37
37
this . _initialized = false ;
38
38
this . _lastNumModifiedFiles = 0 ;
@@ -54,35 +54,65 @@ export default class DeltaPatcher {
54
54
* Applies a Delta Bundle to the current bundle.
55
55
*/
56
56
applyDelta ( deltaBundle ) {
57
+ const isOld = deltaBundle . id ;
57
58
// 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 ) {
59
61
throw new Error ( 'DeltaPatcher should receive a fresh Delta when being initialized' ) ;
60
62
}
61
63
62
64
this . _initialized = true ;
63
65
64
66
// Reset the current delta when we receive a fresh delta.
65
- if ( deltaBundle . reset ) {
67
+ if ( deltaBundle . reset && isOld ) {
66
68
this . _lastBundle = {
67
69
pre : new Map ( ) ,
68
70
post : new Map ( ) ,
69
71
modules : new Map ( ) ,
70
72
id : undefined ,
71
73
} ;
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 ;
72
89
}
73
90
74
- this . _lastNumModifiedFiles =
75
- deltaBundle . pre . size + deltaBundle . post . size + deltaBundle . delta . size ;
91
+ this . _lastBundle . id = isOld ? deltaBundle . id : deltaBundle . revisionId ;
76
92
77
93
if ( this . _lastNumModifiedFiles > 0 ) {
78
94
this . _lastModifiedDate = new Date ( ) ;
79
95
}
80
96
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
+ }
84
107
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
+ }
86
116
87
117
return this ;
88
118
}
@@ -105,11 +135,15 @@ export default class DeltaPatcher {
105
135
return this . _lastModifiedDate ;
106
136
}
107
137
108
- getAllModules ( ) {
109
- return [ ] . concat (
138
+ getAllModules ( isOld ) {
139
+ return isOld ? [ ] . concat (
110
140
Array . from ( this . _lastBundle . pre . values ( ) ) ,
111
141
Array . from ( this . _lastBundle . modules . values ( ) ) ,
112
142
Array . from ( this . _lastBundle . post . values ( ) )
143
+ ) : [ ] . concat (
144
+ [ this . _lastBundle . pre ] ,
145
+ Array . from ( this . _lastBundle . modules . values ( ) ) ,
146
+ [ this . _lastBundle . post ]
113
147
) ;
114
148
}
115
149
0 commit comments