From 7be3d1cbafa4434b05b8ae5b3ec2ba08a5edac9c Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Fri, 2 Mar 2018 02:47:42 -0800 Subject: [PATCH] Make the chrome debugger handle dynamic delta ids Differential Revision: D7112419 fbshipit-source-id: 1d80c0c13144dd19bbcd5535383befc6567cacf7 --- .../server/util/debugger-ui/DeltaPatcher.js | 8 +++++++ .../util/debugger-ui/deltaUrlToBlobUrl.js | 24 +++++++++---------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/local-cli/server/util/debugger-ui/DeltaPatcher.js b/local-cli/server/util/debugger-ui/DeltaPatcher.js index c137894942c51c..ecc92dce7b0ec1 100644 --- a/local-cli/server/util/debugger-ui/DeltaPatcher.js +++ b/local-cli/server/util/debugger-ui/DeltaPatcher.js @@ -30,6 +30,7 @@ pre: new Map(), post: new Map(), modules: new Map(), + id: undefined, }; this._initialized = false; this._lastNumModifiedFiles = 0; @@ -66,6 +67,7 @@ pre: new Map(), post: new Map(), modules: new Map(), + id: undefined, }; } @@ -80,9 +82,15 @@ this._patchMap(this._lastBundle.post, deltaBundle.post); this._patchMap(this._lastBundle.modules, deltaBundle.delta); + this._lastBundle.id = deltaBundle.id; + return this; } + getLastBundleId() { + return this._lastBundle.id; + } + /** * Returns the number of modified files in the last received Delta. This is * currently used to populate the `X-Metro-Files-Changed-Count` HTTP header diff --git a/local-cli/server/util/debugger-ui/deltaUrlToBlobUrl.js b/local-cli/server/util/debugger-ui/deltaUrlToBlobUrl.js index afc1dd5caea3d6..d611062952aa22 100644 --- a/local-cli/server/util/debugger-ui/deltaUrlToBlobUrl.js +++ b/local-cli/server/util/debugger-ui/deltaUrlToBlobUrl.js @@ -20,31 +20,34 @@ * whole JS bundle Blob. */ async function deltaUrlToBlobUrl(deltaUrl) { - let cachedBundle = cachedBundleUrls.get(deltaUrl); + const client = global.DeltaPatcher.get(deltaUrl); - const deltaBundleId = cachedBundle - ? `&deltaBundleId=${cachedBundle.id}` + const deltaBundleId = client.getLastBundleId() + ? `&deltaBundleId=${client.getLastBundleId()}` : ''; const data = await fetch(deltaUrl + deltaBundleId); const bundle = await data.json(); - const deltaPatcher = global.DeltaPatcher.get(bundle.id).applyDelta({ + const deltaPatcher = client.applyDelta({ + id: bundle.id, pre: new Map(bundle.pre), post: new Map(bundle.post), delta: new Map(bundle.delta), reset: bundle.reset, }); + let cachedBundle = cachedBundleUrls.get(deltaUrl); + // If nothing changed, avoid recreating a bundle blob by reusing the // previous one. if (deltaPatcher.getLastNumModifiedFiles() === 0 && cachedBundle) { - return cachedBundle.url; + return cachedBundle; } // Clean up the previous bundle URL to not leak memory. if (cachedBundle) { - URL.revokeObjectURL(cachedBundle.url); + URL.revokeObjectURL(cachedBundle); } // To make Source Maps work correctly, we need to add a newline between @@ -58,13 +61,10 @@ type: 'application/javascript', }); - const bundleUrl = URL.createObjectURL(blob); - cachedBundleUrls.set(deltaUrl, { - id: bundle.id, - url: bundleUrl, - }); + const bundleContents = URL.createObjectURL(blob); + cachedBundleUrls.set(deltaUrl, bundleContents); - return bundleUrl; + return bundleContents; } global.deltaUrlToBlobUrl = deltaUrlToBlobUrl;