Skip to content

Commit

Permalink
avoid unhandledrejection
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaffle committed Aug 9, 2019
1 parent 5eb75b8 commit d6a5671
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
15 changes: 9 additions & 6 deletions src/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
var XMLHttpRequest = global.XMLHttpRequest;
var XDomainRequest = global.XDomainRequest;
var NativeEventSource = global.EventSource;

var document = global.document;
var Promise = global.Promise;
var fetch = global.fetch;
Expand Down Expand Up @@ -336,7 +337,7 @@
// IE 8 fires "onload" without "onprogress
xhr.onabort = onFinish;

// https://bugzilla.mozilla.org/show_bug.cgi?id=736723
// https://bugzilla.mozilla.org/show_bug.cgi?id=736723
if (!("sendAsBinary" in XMLHttpRequest.prototype) && !("mozAnon" in XMLHttpRequest.prototype)) {
xhr.onprogress = onProgress;
}
Expand Down Expand Up @@ -495,15 +496,17 @@
readNextChunk();
}
})["catch"](function (error) {
if (error.name === "AbortError") {
resolve(undefined); // catch the exception, see #130
} else {
reject(error);
}
reject(error);
});
};
readNextChunk();
});
})["catch"](function (error) {
if (error.name === "AbortError") {
return undefined; // catch the exception, see #130, #133
} else {
throw error;
}
})["finally"](function () {
onFinishCallback();
});
Expand Down
15 changes: 9 additions & 6 deletions tests/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
var XMLHttpRequest = global.XMLHttpRequest;
var XDomainRequest = global.XDomainRequest;
var NativeEventSource = global.EventSource;

var document = global.document;
var Promise = global.Promise;
var fetch = global.fetch;
Expand Down Expand Up @@ -336,7 +337,7 @@
// IE 8 fires "onload" without "onprogress
xhr.onabort = onFinish;

// https://bugzilla.mozilla.org/show_bug.cgi?id=736723
// https://bugzilla.mozilla.org/show_bug.cgi?id=736723
if (!("sendAsBinary" in XMLHttpRequest.prototype) && !("mozAnon" in XMLHttpRequest.prototype)) {
xhr.onprogress = onProgress;
}
Expand Down Expand Up @@ -495,15 +496,17 @@
readNextChunk();
}
})["catch"](function (error) {
if (error.name === "AbortError") {
resolve(undefined); // catch the exception, see #130
} else {
reject(error);
}
reject(error);
});
};
readNextChunk();
});
})["catch"](function (error) {
if (error.name === "AbortError") {
return undefined; // catch the exception, see #130, #133
} else {
throw error;
}
})["finally"](function () {
onFinishCallback();
});
Expand Down
30 changes: 30 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,36 @@
};
});

asyncTest("EventSource + close before open", function () {
var es = new EventSource(url + "?estest=" + encodeURIComponent(commonHeaders + "\n\n" + "data:test\n\n"));
var events = '';
window.onunhandledrejection = es.onopen = es.onmessage = es.onerror = function (event) {
events += event.type;
};
es.close();
window.setTimeout(function () {
strictEqual(events, '');
strictEqual(es.readyState, EventSource.CLOSED);
start();
}, 1000);
});

asyncTest("EventSource + close after open", function () {
var es = new EventSource(url + "?estest=" + encodeURIComponent(commonHeaders + "\n\n" + "data:test\n\n"));
var events = '';
window.onunhandledrejection = es.onopen = es.onmessage = es.onerror = function (event) {
events += event.type;
if (event.type === 'open') {
es.close();
}
};
window.setTimeout(function () {
strictEqual(events, 'open');
strictEqual(es.readyState, EventSource.CLOSED);
start();
}, 1000);
});

};

mainTests();
Expand Down

0 comments on commit d6a5671

Please sign in to comment.