Skip to content

Commit

Permalink
DeltaPatcher: Fix isLegacy check for RN versions <= 0.57, closes #325
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Feb 16, 2019
1 parent 2500962 commit 19bd74d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 38 deletions.
53 changes: 30 additions & 23 deletions app/middlewares/delta/DeltaPatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default class DeltaPatcher {
*/
applyDelta(deltaBundle) {
// NOTE: Support for RN <= 0.57
const isLegacy = deltaBundle.id;
const isLegacy = !!deltaBundle.id;
this._isLegacy = isLegacy;

// Make sure that the first received delta is a fresh one.
Expand All @@ -67,50 +67,57 @@ export default class DeltaPatcher {

this._initialized = true;

let bundle = deltaBundle;
if (isLegacy) {
bundle = {
...deltaBundle,
pre: new Map(deltaBundle.pre),
post: new Map(deltaBundle.post),
delta: new Map(deltaBundle.delta),
};
}

// Reset the current delta when we receive a fresh delta.
if (deltaBundle.reset && isLegacy) {
if (bundle.reset && isLegacy) {
this._lastBundle = {
pre: new Map(),
post: new Map(),
modules: new Map(),
id: undefined,
};
} else if (deltaBundle.base) {
} else if (bundle.base) {
this._lastBundle = {
id: deltaBundle.revisionId,
pre: deltaBundle.pre,
post: deltaBundle.post,
modules: new Map(deltaBundle.modules),
id: bundle.revisionId,
pre: bundle.pre,
post: bundle.post,
modules: new Map(bundle.modules),
};
}

if (isLegacy) {
this._lastNumModifiedFiles =
deltaBundle.pre.size + deltaBundle.post.size + deltaBundle.delta.size;
this._lastNumModifiedFiles = bundle.pre.size + bundle.post.size + bundle.delta.size;

this._lastBundle.id = deltaBundle.id;
this._lastBundle.id = bundle.id;

this._patchMap(this._lastBundle.pre, deltaBundle.pre);
this._patchMap(this._lastBundle.post, deltaBundle.post);
this._patchMap(this._lastBundle.modules, deltaBundle.delta);
this._patchMap(this._lastBundle.pre, bundle.pre);
this._patchMap(this._lastBundle.post, bundle.post);
this._patchMap(this._lastBundle.modules, bundle.delta);
} else {
// TODO T37123645 The former case is deprecated, but necessary in order to
// support older versions of the Metro bundler.
const modules = deltaBundle.modules
? deltaBundle.modules
: deltaBundle.added.concat(deltaBundle.modified);
const modules = bundle.modules ? bundle.modules : bundle.added.concat(bundle.modified);
this._lastNumModifiedFiles = modules.length;

if (deltaBundle.deleted) {
this._lastNumModifiedFiles += deltaBundle.deleted.length;
if (bundle.deleted) {
this._lastNumModifiedFiles += bundle.deleted.length;
}

this._lastBundle.id = deltaBundle.revisionId;
this._lastBundle.id = bundle.revisionId;

this._patchMap(this._lastBundle.modules, modules);

if (deltaBundle.deleted) {
for (const id of deltaBundle.deleted) {
if (bundle.deleted) {
for (const id of bundle.deleted) {
this._lastBundle.modules.delete(id);
}
}
Expand Down Expand Up @@ -145,8 +152,8 @@ export default class DeltaPatcher {
return this._lastModifiedDate;
}

getAllModules(isLegacy) {
return isLegacy
getAllModules() {
return this.isLegacy()
? [].concat(
Array.from(this._lastBundle.pre.values()),
Array.from(this._lastBundle.modules.values()),
Expand Down
20 changes: 5 additions & 15 deletions app/middlewares/delta/deltaUrlToBlobUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export default async function deltaUrlToBlobUrl(deltaUrl) {
const client = DeltaPatcher.get(deltaUrl);

const isLegacy = client.isLegacy();
let query;
if (isLegacy) {
let query = '';
if (isLegacy !== undefined && isLegacy) {
const lastBundleId = client.getLastRevisionId();
query = lastBundleId
? `${deltaUrl.indexOf('?') === -1 ? '?' : '&'}deltaBundleId=${lastBundleId}`
: '';
} else {
} else if (isLegacy !== undefined) {
const lastRevisionId = client.getLastRevisionId();
query = lastRevisionId
? `${deltaUrl.indexOf('?') === -1 ? '?' : '&'}revisionId=${lastRevisionId}`
Expand All @@ -33,17 +33,7 @@ export default async function deltaUrlToBlobUrl(deltaUrl) {
const data = await fetch(deltaUrl + query);
const bundle = await data.json();

const deltaPatcher = client.applyDelta(
isLegacy
? {
id: bundle.id,
pre: new Map(bundle.pre),
post: new Map(bundle.post),
delta: new Map(bundle.delta),
reset: bundle.reset,
}
: bundle
);
const deltaPatcher = client.applyDelta(bundle);

const cachedBundle = cachedBundleUrls.get(deltaUrl);

Expand All @@ -60,7 +50,7 @@ export default async function deltaUrlToBlobUrl(deltaUrl) {

// To make Source Maps work correctly, we need to add a newline between
// modules.
const blobContent = deltaPatcher.getAllModules(isLegacy).map(module => `${module}\n`);
const blobContent = deltaPatcher.getAllModules().map(module => `${module}\n`);

// Build the blob with the whole JS bundle.
const blob = new Blob(blobContent, {
Expand Down

0 comments on commit 19bd74d

Please sign in to comment.