Skip to content

Commit

Permalink
Fix patch and getSizeOfAllModules in DeltaPatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Feb 1, 2019
1 parent df5280e commit bb30840
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
40 changes: 21 additions & 19 deletions app/middlewares/delta/DeltaPatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*
* @format
*/
*/

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

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

Expand All @@ -80,9 +81,9 @@ export default class DeltaPatcher {
};
}

this._lastNumModifiedFiles = isOld ?
deltaBundle.pre.size + deltaBundle.post.size + deltaBundle.delta.size :
deltaBundle.modules.length;
this._lastNumModifiedFiles = isOld
? deltaBundle.pre.size + deltaBundle.post.size + deltaBundle.delta.size
: deltaBundle.modules.length;

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

this._lastBundle.id = deltaBundle.id;
} else {
for (const [key, value] of deltaBundle.modules) {
this._lastBundle.modules.set(key, value);
}
this._patchMap(this._lastBundle.modules, deltaBundle.modules);

if (deltaBundle.deleted) {
for (const id of deltaBundle.deleted) {
Expand Down Expand Up @@ -136,19 +135,22 @@ export default class DeltaPatcher {
}

getAllModules(isOld) {
return isOld ? [].concat(
Array.from(this._lastBundle.pre.values()),
Array.from(this._lastBundle.modules.values()),
Array.from(this._lastBundle.post.values())
) : [].concat(
[this._lastBundle.pre],
Array.from(this._lastBundle.modules.values()),
[this._lastBundle.post]
);
return isOld
? [].concat(
Array.from(this._lastBundle.pre.values()),
Array.from(this._lastBundle.modules.values()),
Array.from(this._lastBundle.post.values())
)
: [].concat([this._lastBundle.pre], Array.from(this._lastBundle.modules.values()), [
this._lastBundle.post,
]);
}

getSizeOfAllModules() {
return this._lastBundle.pre.size + this._lastBundle.modules.size + this._lastBundle.post.size;
// Support legacy DeltaPatcher
const preSize = this._lastBundle.pre instanceof Map ? this._lastBundle.pre.size : 1;
const postSize = this._lastBundle.post instanceof Map ? this._lastBundle.post.size : 1;
return preSize + this._lastBundle.modules.size + postSize;
}

_patchMap(original, patch) {
Expand Down
10 changes: 8 additions & 2 deletions app/middlewares/delta/patchFetchPolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@ const toggleFlag = 'fetch.polyfill = true';
const toggleReplaceStr = `${toggleFlag};self.__NETWORK_INSPECT__ && self.__NETWORK_INSPECT__(true)`;

export const checkFetchExists = code => isFetch.some(regex => regex.test(code));
export const patchFetchPolyfill = code =>
code.replace(fetchSupportFlag, fetchSupportReplaceStr).replace(toggleFlag, toggleReplaceStr);
export const patchFetchPolyfill = code => {
if (Array.isArray(code)) {
return code.map(item => (typeof item === 'string' ? patchFetchPolyfill(item) : item));
}
return code
.replace(fetchSupportFlag, fetchSupportReplaceStr)
.replace(toggleFlag, toggleReplaceStr);
};

0 comments on commit bb30840

Please sign in to comment.