Skip to content

Commit

Permalink
use consistent event for moon browser XHR (#259)
Browse files Browse the repository at this point in the history
In the HTTP driver, the `onload` event is used rather than
`addEventListener`. This PR refactors Moon Browser to use the same
method by using `onload` along with an arrow function to clean up the
event handling code that adds the listener.
  • Loading branch information
kbrsh committed Oct 11, 2019
2 parents 855db29 + aab846f commit 3d7174e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
6 changes: 4 additions & 2 deletions packages/moon-browser/dist/moon-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,8 @@
load();
} else {
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function () {

xhr.onload = function () {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 0 || xhr.status === 200) {
var _scriptNew = document.createElement("script");
Expand All @@ -1268,7 +1269,8 @@
script.parentNode.removeChild(script);
load();
}
});
};

xhr.open("GET", src, true);
xhr.send();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/moon-browser/dist/moon-browser.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/moon-browser/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function load() {
} else {
const xhr = new XMLHttpRequest();

xhr.addEventListener("load", function() {
xhr.onload = () => {
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 0 || xhr.status === 200) {
const scriptNew = document.createElement("script");
Expand All @@ -41,7 +41,8 @@ function load() {
script.parentNode.removeChild(script);
load();
}
});
};

xhr.open("GET", src, true);
xhr.send();
}
Expand Down
9 changes: 4 additions & 5 deletions packages/moon-browser/test/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ let status = 200;

window.Moon = Moon;
window.XMLHttpRequest = function() {
this.addEventListener = (type, fn) => {
this.open = () => {};
this.send = () => {
this.readyState = 0;
fn();
this.onload();

this.readyState = 1;
fn();
this.onload();
};
this.open = () => {};
this.send = () => {};

this.DONE = 1;

Expand Down

0 comments on commit 3d7174e

Please sign in to comment.