mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-01-12 22:50:10 +08:00
Update the Delta/HMR format
Summary: Makes the delta bundle data structures more consistent. The changes are as follows: * There are now two types of JSON bundles that can be downloaded from the delta endpoint. Base bundles (`Bundle` type), and Delta bundles (`DeltaBundle` type). * The `reset` boolean is renamed to `base`. * `pre` and `post` properties are now strings. * Only `Bundle` can define `pre` and `post` properties. * The `delta` property is renamed to `modules`. * Deleted modules are now listed inside of the `deleted` property, which is only defined by `DeltaBundle`. Reviewed By: mjesun Differential Revision: D10446831 fbshipit-source-id: 40e229a2811d48950f0bad8dd341ece189089e9b
This commit is contained in:
committed by
Facebook Github Bot
parent
bb93abf5ca
commit
1eedf05651
@@ -27,10 +27,10 @@
|
||||
class DeltaPatcher {
|
||||
constructor() {
|
||||
this._lastBundle = {
|
||||
pre: new Map(),
|
||||
post: new Map(),
|
||||
revisionId: undefined,
|
||||
pre: '',
|
||||
post: '',
|
||||
modules: new Map(),
|
||||
id: undefined,
|
||||
};
|
||||
this._initialized = false;
|
||||
this._lastNumModifiedFiles = 0;
|
||||
@@ -51,44 +51,41 @@
|
||||
/**
|
||||
* Applies a Delta Bundle to the current bundle.
|
||||
*/
|
||||
applyDelta(deltaBundle) {
|
||||
// Make sure that the first received delta is a fresh one.
|
||||
if (!this._initialized && !deltaBundle.reset) {
|
||||
applyDelta(bundle) {
|
||||
// Make sure that the first received bundle is a base.
|
||||
if (!this._initialized && !bundle.base) {
|
||||
throw new Error(
|
||||
'DeltaPatcher should receive a fresh Delta when being initialized',
|
||||
'DeltaPatcher should receive a base Bundle when being initialized',
|
||||
);
|
||||
}
|
||||
|
||||
this._initialized = true;
|
||||
|
||||
// Reset the current delta when we receive a fresh delta.
|
||||
if (deltaBundle.reset) {
|
||||
// Reset the current bundle when we receive a base bundle.
|
||||
if (bundle.base) {
|
||||
this._lastBundle = {
|
||||
pre: new Map(),
|
||||
post: new Map(),
|
||||
revisionId: undefined,
|
||||
pre: bundle.pre,
|
||||
post: bundle.post,
|
||||
modules: new Map(),
|
||||
id: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
this._lastNumModifiedFiles =
|
||||
deltaBundle.pre.size + deltaBundle.post.size + deltaBundle.delta.size;
|
||||
this._lastNumModifiedFiles = bundle.modules.size;
|
||||
|
||||
if (this._lastNumModifiedFiles > 0) {
|
||||
this._lastModifiedDate = new Date();
|
||||
}
|
||||
|
||||
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.modules, bundle.modules);
|
||||
|
||||
this._lastBundle.id = deltaBundle.id;
|
||||
this._lastBundle.revisionId = bundle.revisionId;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
getLastBundleId() {
|
||||
return this._lastBundle.id;
|
||||
getLastRevisionId() {
|
||||
return this._lastBundle.revisionId;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,9 +104,9 @@
|
||||
|
||||
getAllModules() {
|
||||
return [].concat(
|
||||
Array.from(this._lastBundle.pre.values()),
|
||||
this._lastBundle.pre,
|
||||
Array.from(this._lastBundle.modules.values()),
|
||||
Array.from(this._lastBundle.post.values()),
|
||||
this._lastBundle.post,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,19 +22,19 @@
|
||||
async function deltaUrlToBlobUrl(deltaUrl) {
|
||||
const client = global.DeltaPatcher.get(deltaUrl);
|
||||
|
||||
const deltaBundleId = client.getLastBundleId()
|
||||
? `&deltaBundleId=${client.getLastBundleId()}`
|
||||
const revisionId = client.getLastRevisionId()
|
||||
? `&revisionId=${client.getLastRevisionId()}`
|
||||
: '';
|
||||
|
||||
const data = await fetch(deltaUrl + deltaBundleId);
|
||||
const data = await fetch(deltaUrl + revisionId);
|
||||
const bundle = await data.json();
|
||||
|
||||
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,
|
||||
base: bundle.base,
|
||||
revisionId: bundle.revisionId,
|
||||
pre: bundle.pre,
|
||||
post: bundle.post,
|
||||
modules: new Map(bundle.modules),
|
||||
});
|
||||
|
||||
let cachedBundle = cachedBundleUrls.get(deltaUrl);
|
||||
|
||||
Reference in New Issue
Block a user