Skip to content

Commit 977a5eb

Browse files
Eugnisjhen0409
authored andcommitted
Fixed to work with RN 0.58+ (#307)
* Fixed to work with RN 0.58+ * Removed unused import for new patcher * Fixed to pass eslint check * Fixed to work with both <0.58 and >0.58 * fixed typo * fixed typo #2
1 parent 2ec8080 commit 977a5eb

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

app/middlewares/delta/DeltaPatcher.js

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
* @format
8-
*/
8+
*/
99

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

@@ -29,10 +29,10 @@ import { checkFetchExists, patchFetchPolyfill } from './patchFetchPolyfill';
2929
export default class DeltaPatcher {
3030
constructor() {
3131
this._lastBundle = {
32+
id: undefined,
3233
pre: new Map(),
3334
post: new Map(),
3435
modules: new Map(),
35-
id: undefined,
3636
};
3737
this._initialized = false;
3838
this._lastNumModifiedFiles = 0;
@@ -54,35 +54,65 @@ export default class DeltaPatcher {
5454
* Applies a Delta Bundle to the current bundle.
5555
*/
5656
applyDelta(deltaBundle) {
57+
const isOld = deltaBundle.id;
5758
// Make sure that the first received delta is a fresh one.
58-
if (!this._initialized && !deltaBundle.reset) {
59+
if (isOld ? !this._initialized && !deltaBundle.reset :
60+
!this._initialized && !deltaBundle.base) {
5961
throw new Error('DeltaPatcher should receive a fresh Delta when being initialized');
6062
}
6163

6264
this._initialized = true;
6365

6466
// Reset the current delta when we receive a fresh delta.
65-
if (deltaBundle.reset) {
67+
if (deltaBundle.reset && isOld) {
6668
this._lastBundle = {
6769
pre: new Map(),
6870
post: new Map(),
6971
modules: new Map(),
7072
id: undefined,
7173
};
74+
} else if (deltaBundle.base) {
75+
this._lastBundle = {
76+
id: deltaBundle.revisionId,
77+
pre: deltaBundle.pre,
78+
post: deltaBundle.post,
79+
modules: new Map(deltaBundle.modules),
80+
};
81+
}
82+
83+
this._lastNumModifiedFiles = isOld ?
84+
deltaBundle.pre.size + deltaBundle.post.size + deltaBundle.delta.size :
85+
deltaBundle.modules.length;
86+
87+
if (deltaBundle.deleted) {
88+
this._lastNumModifiedFiles += deltaBundle.deleted.length;
7289
}
7390

74-
this._lastNumModifiedFiles =
75-
deltaBundle.pre.size + deltaBundle.post.size + deltaBundle.delta.size;
91+
this._lastBundle.id = isOld ? deltaBundle.id : deltaBundle.revisionId;
7692

7793
if (this._lastNumModifiedFiles > 0) {
7894
this._lastModifiedDate = new Date();
7995
}
8096

81-
this._patchMap(this._lastBundle.pre, deltaBundle.pre);
82-
this._patchMap(this._lastBundle.post, deltaBundle.post);
83-
this._patchMap(this._lastBundle.modules, deltaBundle.delta);
97+
if (isOld) {
98+
this._patchMap(this._lastBundle.pre, deltaBundle.pre);
99+
this._patchMap(this._lastBundle.post, deltaBundle.post);
100+
this._patchMap(this._lastBundle.modules, deltaBundle.delta);
101+
102+
this._lastBundle.id = deltaBundle.id;
103+
} else {
104+
for (const [key, value] of deltaBundle.modules) {
105+
this._lastBundle.modules.set(key, value);
106+
}
84107

85-
this._lastBundle.id = deltaBundle.id;
108+
if (deltaBundle.deleted) {
109+
for (const id of deltaBundle.deleted) {
110+
this._lastBundle.modules.delete(id);
111+
}
112+
}
113+
114+
this._lastBundle.id = deltaBundle.revisionId;
115+
}
86116

87117
return this;
88118
}
@@ -105,11 +135,15 @@ export default class DeltaPatcher {
105135
return this._lastModifiedDate;
106136
}
107137

108-
getAllModules() {
109-
return [].concat(
138+
getAllModules(isOld) {
139+
return isOld ? [].concat(
110140
Array.from(this._lastBundle.pre.values()),
111141
Array.from(this._lastBundle.modules.values()),
112142
Array.from(this._lastBundle.post.values())
143+
) : [].concat(
144+
[this._lastBundle.pre],
145+
Array.from(this._lastBundle.modules.values()),
146+
[this._lastBundle.post]
113147
);
114148
}
115149

app/middlewares/delta/deltaUrlToBlobUrl.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ export default async function deltaUrlToBlobUrl(deltaUrl) {
2626
const data = await fetch(deltaUrl + deltaBundleId);
2727
const bundle = await data.json();
2828

29-
const deltaPatcher = client.applyDelta({
29+
const isOld = bundle.id;
30+
31+
const deltaPatcher = client.applyDelta(isOld ? {
3032
id: bundle.id,
3133
pre: new Map(bundle.pre),
3234
post: new Map(bundle.post),
3335
delta: new Map(bundle.delta),
3436
reset: bundle.reset,
35-
});
37+
} : bundle);
3638

3739
const cachedBundle = cachedBundleUrls.get(deltaUrl);
3840

@@ -49,7 +51,7 @@ export default async function deltaUrlToBlobUrl(deltaUrl) {
4951

5052
// To make Source Maps work correctly, we need to add a newline between
5153
// modules.
52-
const blobContent = deltaPatcher.getAllModules().map(module => `${module}\n`);
54+
const blobContent = deltaPatcher.getAllModules(isOld).map(module => `${module}\n`);
5355

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

0 commit comments

Comments
 (0)