Skip to content

Commit

Permalink
Merge pull request #50 from Gariest/DynamicImportOfflineBugFixv2
Browse files Browse the repository at this point in the history
Enable retrying dynamic imports after failure.
  • Loading branch information
benjamn authored Oct 13, 2018
2 parents 8e420c4 + ed1a803 commit 635fd0c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
20 changes: 17 additions & 3 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,26 @@ makeInstaller = function (options) {
var toBeFetched = missing;
missing = null;

return Promise.resolve(
function clearPending() {
if (toBeFetched) {
Object.keys(toBeFetched).forEach(function (id) {
getOwn(filesByModuleId, id).pending = false;
});
}
}

return new Promise(function (resolve) {
// The install.fetch function takes an object mapping missing
// dynamic module identifiers to options objects, and should
// return a Promise that resolves to a module tree that can be
// installed. As an optimization, if there were no missing dynamic
// modules, then we can skip calling install.fetch entirely.
toBeFetched && install.fetch(toBeFetched)
resolve(toBeFetched && install.fetch(toBeFetched));

).then(function (tree) {
}).then(function (tree) {
function both() {
install(tree);
clearPending();
return absChildId;
}

Expand All @@ -165,6 +174,11 @@ makeInstaller = function (options) {
// Whether previousPromise was resolved or rejected, carry on with
// the installation regardless.
return previousPromise.then(both, both);

}, function (error) {
// Fixes https://github.com/meteor/meteor/issues/10182.
clearPending();
throw error;
});
});
};
Expand Down
54 changes: 54 additions & 0 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,60 @@ describe("install", function () {
});
});

it("supports retrying dynamic imports after failure", function () {
var install = makeInstaller();

var threw = false;
install.fetch = function (ids) {
if (! threw) {
threw = true;
debugger;
throw new Error("network failure, or something");
}

var tree = {};

Object.keys(ids).forEach(function (id) {
var info = ids[id];
assert.strictEqual(info.module.id, id);
addToTree(tree, id, function (r, exports, module) {
assert.strictEqual(module, info.module);
exports.name = module.id;
});
});

return tree;
};

var require = install({
"main.js": function (require, exports, module) {
exports.attempt = function (id) {
return module.prefetch(id);
};
},
"a.js": ["./c", "./b"],
"b.js": ["./a", "./c"],
"c.js": ["./a", "./b"]
});

var attempt = require("./main").attempt;

return attempt("./a").then(function () {
throw new Error("should have failed");
}, function (error) {
assert.strictEqual(threw, true);
assert.strictEqual(
error.message,
"network failure, or something"
);

return attempt("./c").then(function (id) {
assert.strictEqual(id, "/c.js");
assert.strictEqual(require("./c").name, id);
});
});
});

it("respects module.exports before file.contents", function () {
var install = makeInstaller();

Expand Down

0 comments on commit 635fd0c

Please sign in to comment.