Skip to content

Commit

Permalink
Support RN ^0.59.0-rc.0 for Debugger and NPM package (jhen0409#321)
Browse files Browse the repository at this point in the history
* [npm-package] Support inject script for @react-native-commonunity/cli (RN ^0.59.0-rc.0)

* Update test

* Add test for RN 0.59

* Update delta client for support RN 0.59

* Clear console.log
  • Loading branch information
jhen0409 committed Feb 15, 2019
1 parent fc875b4 commit 26dfa9a
Show file tree
Hide file tree
Showing 6 changed files with 477 additions and 95 deletions.
57 changes: 34 additions & 23 deletions app/middlewares/delta/DeltaPatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,21 @@ export default class DeltaPatcher {
* Applies a Delta Bundle to the current bundle.
*/
applyDelta(deltaBundle) {
const isOld = deltaBundle.id;
// NOTE: Support for RN <= 0.57
const isLegacy = deltaBundle.id;
this._isLegacy = isLegacy;

// Make sure that the first received delta is a fresh one.
if (
isOld ? !this._initialized && !deltaBundle.reset : !this._initialized && !deltaBundle.base
isLegacy ? !this._initialized && !deltaBundle.reset : !this._initialized && !deltaBundle.base
) {
throw new Error('DeltaPatcher should receive a fresh Delta when being initialized');
}

this._initialized = true;

// Reset the current delta when we receive a fresh delta.
if (deltaBundle.reset && isOld) {
if (deltaBundle.reset && isLegacy) {
this._lastBundle = {
pre: new Map(),
post: new Map(),
Expand All @@ -81,42 +84,50 @@ export default class DeltaPatcher {
};
}

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

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

this._lastBundle.id = isOld ? deltaBundle.id : deltaBundle.revisionId;
if (isLegacy) {
this._lastNumModifiedFiles =
deltaBundle.pre.size + deltaBundle.post.size + deltaBundle.delta.size;

if (this._lastNumModifiedFiles > 0) {
this._lastModifiedDate = new Date();
}
this._lastBundle.id = deltaBundle.id;

if (isOld) {
this._patchMap(this._lastBundle.pre, deltaBundle.pre);
this._patchMap(this._lastBundle.post, deltaBundle.post);
this._patchMap(this._lastBundle.modules, deltaBundle.delta);

this._lastBundle.id = deltaBundle.id;
} else {
this._patchMap(this._lastBundle.modules, deltaBundle.modules);
// 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);
this._lastNumModifiedFiles = modules.length;

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

this._lastBundle.id = deltaBundle.revisionId;

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

if (deltaBundle.deleted) {
for (const id of deltaBundle.deleted) {
this._lastBundle.modules.delete(id);
}
}
}

this._lastBundle.id = deltaBundle.revisionId;
if (this._lastNumModifiedFiles > 0) {
this._lastModifiedDate = new Date();
}

return this;
}

getLastBundleId() {
isLegacy() {
return this._isLegacy;
}

getLastRevisionId() {
return this._lastBundle.id;
}

Expand All @@ -134,8 +145,8 @@ export default class DeltaPatcher {
return this._lastModifiedDate;
}

getAllModules(isOld) {
return isOld
getAllModules(isLegacy) {
return isLegacy
? [].concat(
Array.from(this._lastBundle.pre.values()),
Array.from(this._lastBundle.modules.values()),
Expand Down
26 changes: 16 additions & 10 deletions app/middlewares/delta/deltaUrlToBlobUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ const cachedBundleUrls = new Map();
export default async function deltaUrlToBlobUrl(deltaUrl) {
const client = DeltaPatcher.get(deltaUrl);

const lastBundleId = client.getLastBundleId();

const deltaBundleId = lastBundleId
? `${deltaUrl.indexOf('?') === -1 ? '?' : '&'}deltaBundleId=${lastBundleId}`
: '';
const isLegacy = client.isLegacy();
let query;
if (isLegacy) {
const lastBundleId = client.getLastRevisionId();
query = lastBundleId
? `${deltaUrl.indexOf('?') === -1 ? '?' : '&'}deltaBundleId=${lastBundleId}`
: '';
} else {
const lastRevisionId = client.getLastRevisionId();
query = lastRevisionId
? `${deltaUrl.indexOf('?') === -1 ? '?' : '&'}revisionId=${lastRevisionId}`
: '';
}

const data = await fetch(deltaUrl + deltaBundleId);
const data = await fetch(deltaUrl + query);
const bundle = await data.json();

const isOld = bundle.id;

const deltaPatcher = client.applyDelta(
isOld
isLegacy
? {
id: bundle.id,
pre: new Map(bundle.pre),
Expand All @@ -54,7 +60,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(isOld).map(module => `${module}\n`);
const blobContent = deltaPatcher.getAllModules(isLegacy).map(module => `${module}\n`);

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

0 comments on commit 26dfa9a

Please sign in to comment.