Skip to content

Commit bb30840

Browse files
committed
Fix patch and getSizeOfAllModules in DeltaPatcher
1 parent df5280e commit bb30840

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

app/middlewares/delta/DeltaPatcher.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
* @format
8-
*/
8+
*/
99

1010
import { checkFetchExists, patchFetchPolyfill } from './patchFetchPolyfill';
1111

@@ -56,8 +56,9 @@ export default class DeltaPatcher {
5656
applyDelta(deltaBundle) {
5757
const isOld = deltaBundle.id;
5858
// Make sure that the first received delta is a fresh one.
59-
if (isOld ? !this._initialized && !deltaBundle.reset :
60-
!this._initialized && !deltaBundle.base) {
59+
if (
60+
isOld ? !this._initialized && !deltaBundle.reset : !this._initialized && !deltaBundle.base
61+
) {
6162
throw new Error('DeltaPatcher should receive a fresh Delta when being initialized');
6263
}
6364

@@ -80,9 +81,9 @@ export default class DeltaPatcher {
8081
};
8182
}
8283

83-
this._lastNumModifiedFiles = isOld ?
84-
deltaBundle.pre.size + deltaBundle.post.size + deltaBundle.delta.size :
85-
deltaBundle.modules.length;
84+
this._lastNumModifiedFiles = isOld
85+
? deltaBundle.pre.size + deltaBundle.post.size + deltaBundle.delta.size
86+
: deltaBundle.modules.length;
8687

8788
if (deltaBundle.deleted) {
8889
this._lastNumModifiedFiles += deltaBundle.deleted.length;
@@ -101,9 +102,7 @@ export default class DeltaPatcher {
101102

102103
this._lastBundle.id = deltaBundle.id;
103104
} else {
104-
for (const [key, value] of deltaBundle.modules) {
105-
this._lastBundle.modules.set(key, value);
106-
}
105+
this._patchMap(this._lastBundle.modules, deltaBundle.modules);
107106

108107
if (deltaBundle.deleted) {
109108
for (const id of deltaBundle.deleted) {
@@ -136,19 +135,22 @@ export default class DeltaPatcher {
136135
}
137136

138137
getAllModules(isOld) {
139-
return isOld ? [].concat(
140-
Array.from(this._lastBundle.pre.values()),
141-
Array.from(this._lastBundle.modules.values()),
142-
Array.from(this._lastBundle.post.values())
143-
) : [].concat(
144-
[this._lastBundle.pre],
145-
Array.from(this._lastBundle.modules.values()),
146-
[this._lastBundle.post]
147-
);
138+
return isOld
139+
? [].concat(
140+
Array.from(this._lastBundle.pre.values()),
141+
Array.from(this._lastBundle.modules.values()),
142+
Array.from(this._lastBundle.post.values())
143+
)
144+
: [].concat([this._lastBundle.pre], Array.from(this._lastBundle.modules.values()), [
145+
this._lastBundle.post,
146+
]);
148147
}
149148

150149
getSizeOfAllModules() {
151-
return this._lastBundle.pre.size + this._lastBundle.modules.size + this._lastBundle.post.size;
150+
// Support legacy DeltaPatcher
151+
const preSize = this._lastBundle.pre instanceof Map ? this._lastBundle.pre.size : 1;
152+
const postSize = this._lastBundle.post instanceof Map ? this._lastBundle.post.size : 1;
153+
return preSize + this._lastBundle.modules.size + postSize;
152154
}
153155

154156
_patchMap(original, patch) {

app/middlewares/delta/patchFetchPolyfill.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,11 @@ const toggleFlag = 'fetch.polyfill = true';
2626
const toggleReplaceStr = `${toggleFlag};self.__NETWORK_INSPECT__ && self.__NETWORK_INSPECT__(true)`;
2727

2828
export const checkFetchExists = code => isFetch.some(regex => regex.test(code));
29-
export const patchFetchPolyfill = code =>
30-
code.replace(fetchSupportFlag, fetchSupportReplaceStr).replace(toggleFlag, toggleReplaceStr);
29+
export const patchFetchPolyfill = code => {
30+
if (Array.isArray(code)) {
31+
return code.map(item => (typeof item === 'string' ? patchFetchPolyfill(item) : item));
32+
}
33+
return code
34+
.replace(fetchSupportFlag, fetchSupportReplaceStr)
35+
.replace(toggleFlag, toggleReplaceStr);
36+
};

0 commit comments

Comments
 (0)