Skip to content

Commit 635fd0c

Browse files
authored
Merge pull request benjamn#50 from Gariest/DynamicImportOfflineBugFixv2
Enable retrying dynamic imports after failure.
2 parents 8e420c4 + ed1a803 commit 635fd0c

2 files changed

Lines changed: 71 additions & 3 deletions

File tree

install.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,26 @@ makeInstaller = function (options) {
143143
var toBeFetched = missing;
144144
missing = null;
145145

146-
return Promise.resolve(
146+
function clearPending() {
147+
if (toBeFetched) {
148+
Object.keys(toBeFetched).forEach(function (id) {
149+
getOwn(filesByModuleId, id).pending = false;
150+
});
151+
}
152+
}
153+
154+
return new Promise(function (resolve) {
147155
// The install.fetch function takes an object mapping missing
148156
// dynamic module identifiers to options objects, and should
149157
// return a Promise that resolves to a module tree that can be
150158
// installed. As an optimization, if there were no missing dynamic
151159
// modules, then we can skip calling install.fetch entirely.
152-
toBeFetched && install.fetch(toBeFetched)
160+
resolve(toBeFetched && install.fetch(toBeFetched));
153161

154-
).then(function (tree) {
162+
}).then(function (tree) {
155163
function both() {
156164
install(tree);
165+
clearPending();
157166
return absChildId;
158167
}
159168

@@ -165,6 +174,11 @@ makeInstaller = function (options) {
165174
// Whether previousPromise was resolved or rejected, carry on with
166175
// the installation regardless.
167176
return previousPromise.then(both, both);
177+
178+
}, function (error) {
179+
// Fixes https://github.com/meteor/meteor/issues/10182.
180+
clearPending();
181+
throw error;
168182
});
169183
});
170184
};

test/run.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,60 @@ describe("install", function () {
981981
});
982982
});
983983

984+
it("supports retrying dynamic imports after failure", function () {
985+
var install = makeInstaller();
986+
987+
var threw = false;
988+
install.fetch = function (ids) {
989+
if (! threw) {
990+
threw = true;
991+
debugger;
992+
throw new Error("network failure, or something");
993+
}
994+
995+
var tree = {};
996+
997+
Object.keys(ids).forEach(function (id) {
998+
var info = ids[id];
999+
assert.strictEqual(info.module.id, id);
1000+
addToTree(tree, id, function (r, exports, module) {
1001+
assert.strictEqual(module, info.module);
1002+
exports.name = module.id;
1003+
});
1004+
});
1005+
1006+
return tree;
1007+
};
1008+
1009+
var require = install({
1010+
"main.js": function (require, exports, module) {
1011+
exports.attempt = function (id) {
1012+
return module.prefetch(id);
1013+
};
1014+
},
1015+
"a.js": ["./c", "./b"],
1016+
"b.js": ["./a", "./c"],
1017+
"c.js": ["./a", "./b"]
1018+
});
1019+
1020+
var attempt = require("./main").attempt;
1021+
1022+
return attempt("./a").then(function () {
1023+
throw new Error("should have failed");
1024+
}, function (error) {
1025+
assert.strictEqual(threw, true);
1026+
assert.strictEqual(
1027+
error.message,
1028+
"network failure, or something"
1029+
);
1030+
1031+
return attempt("./c").then(function (id) {
1032+
assert.strictEqual(id, "/c.js");
1033+
assert.strictEqual(require("./c").name, id);
1034+
});
1035+
});
1036+
});
1037+
9841038
it("respects module.exports before file.contents", function () {
9851039
var install = makeInstaller();
9861040

0 commit comments

Comments
 (0)