Skip to content

Commit

Permalink
Merge pull request #1351 from stealjs/reloader
Browse files Browse the repository at this point in the history
reload(callback) syntax now returns an error
  • Loading branch information
matthewp authored Mar 6, 2018
2 parents 53f54ea + 0255624 commit fde0ab3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
17 changes: 16 additions & 1 deletion docs/module-live-reload.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ reload(function(){
});
```

@param {Function} callback A function to be called after a reload cycle is complete.
@param {function(err)} callback A function to be called after a reload cycle is complete.

@signature `reload(moduleName, callback)`

Expand Down Expand Up @@ -102,5 +102,20 @@ render();
reload(function(){
render();
});
```

## Error handling

The *live-reload* module will include an [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) if there is any error that occurs when reloading the module tree.

```js
import reload from "live-reload";

reload(function(err) {
if(err) {
displayErrorMessage(err);
} else {
// Do whatever you normally do on a reload.
}
});
```
15 changes: 12 additions & 3 deletions ext/live-reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,16 @@ function addLiveReload(loader) {
for(var i = 0, len = moduleNames.length; i < len; i++) {
promises.push(reload(moduleNames[i]));
}
return Promise.all(promises).then(function(){
return Promise.all(promises)
.then(function(){
var e = loader._liveEmitter;
loader._inLiveReloadCycle = false;
e.emit("!cycleComplete");
}, function(err){
var error = err.originalErr || err;
var e = loader._liveEmitter;
loader._inLiveReloadCycle = false;
e.emit("!cycleComplete", error);
});
}

Expand All @@ -240,11 +246,14 @@ function addLiveReload(loader) {
return Promise.all(imports).then(function(){
// Remove any newly orphaned modules before declaring the cycle complete.
removeOrphans(moduleName, currentDeps);
}, function(){
}, function(err){
// There was an error re-importing modules
// Workers don't have a location and no way to refresh the page.
if(loader.global.location && loader.global.location.reload) {
var canReload = loader.global.location && loader.global.location.reload;
if(canReload && loader.liveReloadReload !== false) {
loader.global.location.reload();
} else {
return Promise.reject(err);
}
});
}
Expand Down
33 changes: 33 additions & 0 deletions test/live_reload/unit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var reloader = require("live-reload");
var loader = require("@loader");
loader.liveReloadReload = false;

QUnit.module("Unit tests", {
beforeEach: function(){
Expand Down Expand Up @@ -86,6 +87,38 @@ QUnit.test("Can be cloned", function(assert){
.then(done, done);
});

QUnit.test("Returns an error when there is an error", function(assert){
var done = assert.async();
var loaded = false;

var fetch = loader.fetch;
loader.fetch = function(load){
if(load.name === "oops") {
if(loaded) {
return Promise.resolve("module.exports = {}; oops 'bad';");
} else {
return Promise.resolve("module.exports = {};");
}
}
return fetch.apply(this, arguments);
};

return loader.import("oops")
.then(function(){
loaded = true;
return loader.import("live-reload", { name: "error-in-tree" })
})
.then(function(reload){
reload(function(err){
assert.ok(err instanceof Error, "Got an error");
loader.fetch = fetch;
done();
});

reloader("oops");
});
});

QUnit.module("reload.isReloading");

QUnit.test("is false by default", function(assert){
Expand Down

0 comments on commit fde0ab3

Please sign in to comment.