Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaffle committed Jul 21, 2016
1 parent f276028 commit ae66053
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 82 deletions.
91 changes: 51 additions & 40 deletions eventsource.js
Expand Up @@ -51,7 +51,11 @@
while (++i < length) {
listener = typeListeners[i];
try {
listener.call(this, event);
if (typeof listener.handleEvent === "function") {
listener.handleEvent(event);
} else {
listener.call(this, event);
}
} catch (e) {
throwError(e);
}
Expand Down Expand Up @@ -226,7 +230,7 @@
if (contentType == undefined) {
contentType = "";
}
if (status === 0 && statusText === "" && type === "load" && responseText !== "") {
if (status === 0 && statusText === "" && (type === "load" || type === "error") && responseText !== "") {
status = 200;
statusText = "OK";
if (contentType === "") { // Opera 12
Expand Down Expand Up @@ -344,40 +348,45 @@
charOffset = length;
}

if ((currentState === OPEN || currentState === CONNECTING) &&
(type === "load" || type === "error" || isWrongStatusCodeOrContentType || (charOffset > 1024 * 1024) || (timeout === 0 && !wasActivity))) {
if (isWrongStatusCodeOrContentType) {
close();
} else {
if (type === "" && timeout === 0 && !wasActivity) {
setTimeout(function () {
throw new Error("No activity within " + heartbeatTimeout + " milliseconds. Reconnecting.");
}, 0);
}
currentState = WAITING;
xhr.abort();
if (timeout !== 0) {
clearTimeout(timeout);
timeout = 0;
}
if (retry > initialRetry * 16) {
retry = initialRetry * 16;
if (currentState === OPEN || currentState === CONNECTING) {
if (type === "load" ||
type === "error" ||
isWrongStatusCodeOrContentType ||
(charOffset > 1024 * 1024) ||
(timeout === 0 && !wasActivity)) {
if (isWrongStatusCodeOrContentType) {
close();
} else {
if (type === "" && timeout === 0 && !wasActivity) {
setTimeout(function () {
throw new Error("No activity within " + heartbeatTimeout + " milliseconds. Reconnecting.");
}, 0);
}
currentState = WAITING;
xhr.abort();
if (timeout !== 0) {
clearTimeout(timeout);
timeout = 0;
}
if (retry > initialRetry * 16) {
retry = initialRetry * 16;
}
if (retry > MAXIMUM_DURATION) {
retry = MAXIMUM_DURATION;
}
timeout = setTimeout(onTimeout, retry);
retry = retry * 2 + 1;

that.readyState = CONNECTING;
}
if (retry > MAXIMUM_DURATION) {
retry = MAXIMUM_DURATION;
event = new Event("error");
that.dispatchEvent(event);
fire(that, that.onerror, event);
} else {
if (timeout === 0) {
wasActivity = false;
timeout = setTimeout(onTimeout, heartbeatTimeout);
}
timeout = setTimeout(onTimeout, retry);
retry = retry * 2 + 1;

that.readyState = CONNECTING;
}
event = new Event("error");
that.dispatchEvent(event);
fire(that, that.onerror, event);
} else {
if (timeout === 0) {
wasActivity = false;
timeout = setTimeout(onTimeout, heartbeatTimeout);
}
}
}
Expand All @@ -395,14 +404,16 @@
}

function onReadyStateChange() {
if (xhr.readyState === 4) {
if (xhr.status === 0) {
onEvent("error");
if (xhr != undefined) { // Opera 12
if (xhr.readyState === 4) {
if (xhr.status === 0) {
onEvent("error");
} else {
onEvent("load");
}
} else {
onEvent("load");
onEvent("progress");
}
} else {
onEvent("progress");
}
}

Expand Down
95 changes: 53 additions & 42 deletions tests/eventsource.js
Expand Up @@ -12,7 +12,7 @@

var setTimeout = global.setTimeout;
var clearTimeout = global.clearTimeout;

function Map() {
this.data = {};
}
Expand Down Expand Up @@ -51,7 +51,11 @@
while (++i < length) {
listener = typeListeners[i];
try {
listener.call(this, event);
if (typeof listener.handleEvent === "function") {
listener.handleEvent(event);
} else {
listener.call(this, event);
}
} catch (e) {
throwError(e);
}
Expand Down Expand Up @@ -226,7 +230,7 @@
if (contentType == undefined) {
contentType = "";
}
if (status === 0 && statusText === "" && type === "load" && responseText !== "") {
if (status === 0 && statusText === "" && (type === "load" || type === "error") && responseText !== "") {
status = 200;
statusText = "OK";
if (contentType === "") { // Opera 12
Expand Down Expand Up @@ -344,40 +348,45 @@
charOffset = length;
}

if ((currentState === OPEN || currentState === CONNECTING) &&
(type === "load" || type === "error" || isWrongStatusCodeOrContentType || (charOffset > 1024 * 1024) || (timeout === 0 && !wasActivity))) {
if (isWrongStatusCodeOrContentType) {
close();
} else {
if (type === "" && timeout === 0 && !wasActivity) {
setTimeout(function () {
throw new Error("No activity within " + heartbeatTimeout + " milliseconds. Reconnecting.");
}, 0);
}
currentState = WAITING;
xhr.abort();
if (timeout !== 0) {
clearTimeout(timeout);
timeout = 0;
}
if (retry > initialRetry * 16) {
retry = initialRetry * 16;
if (currentState === OPEN || currentState === CONNECTING) {
if (type === "load" ||
type === "error" ||
isWrongStatusCodeOrContentType ||
(charOffset > 1024 * 1024) ||
(timeout === 0 && !wasActivity)) {
if (isWrongStatusCodeOrContentType) {
close();
} else {
if (type === "" && timeout === 0 && !wasActivity) {
setTimeout(function () {
throw new Error("No activity within " + heartbeatTimeout + " milliseconds. Reconnecting.");
}, 0);
}
currentState = WAITING;
xhr.abort();
if (timeout !== 0) {
clearTimeout(timeout);
timeout = 0;
}
if (retry > initialRetry * 16) {
retry = initialRetry * 16;
}
if (retry > MAXIMUM_DURATION) {
retry = MAXIMUM_DURATION;
}
timeout = setTimeout(onTimeout, retry);
retry = retry * 2 + 1;

that.readyState = CONNECTING;
}
if (retry > MAXIMUM_DURATION) {
retry = MAXIMUM_DURATION;
event = new Event("error");
that.dispatchEvent(event);
fire(that, that.onerror, event);
} else {
if (timeout === 0) {
wasActivity = false;
timeout = setTimeout(onTimeout, heartbeatTimeout);
}
timeout = setTimeout(onTimeout, retry);
retry = retry * 2 + 1;

that.readyState = CONNECTING;
}
event = new Event("error");
that.dispatchEvent(event);
fire(that, that.onerror, event);
} else {
if (timeout === 0) {
wasActivity = false;
timeout = setTimeout(onTimeout, heartbeatTimeout);
}
}
}
Expand All @@ -395,14 +404,16 @@
}

function onReadyStateChange() {
if (xhr.readyState === 4) {
if (xhr.status === 0) {
onEvent("error");
if (xhr != undefined) { // Opera 12
if (xhr.readyState === 4) {
if (xhr.status === 0) {
onEvent("error");
} else {
onEvent("load");
}
} else {
onEvent("load");
onEvent("progress");
}
} else {
onEvent("progress");
}
}

Expand Down Expand Up @@ -538,4 +549,4 @@
global.EventSource = EventSource;
}

}(this));
}(typeof window !== 'undefined' ? window : this));

0 comments on commit ae66053

Please sign in to comment.