MCPcopy Create free account
hub / github.com/Yaffle/EventSource / start

Function start

tests/eventsource.js:736–1006  ·  view source on GitHub ↗
(es, url, options)

Source from the content-addressed store, hash-verified

734 var isFetchSupported = fetch != undefined && Response != undefined && "body" in Response.prototype;
735
736 function start(es, url, options) {
737 url = String(url);
738 var withCredentials = Boolean(options.withCredentials);
739 var lastEventIdQueryParameterName = options.lastEventIdQueryParameterName || "lastEventId";
740
741 var initialRetry = clampDuration(1000);
742 var heartbeatTimeout = parseDuration(options.heartbeatTimeout, 45000);
743
744 var lastEventId = "";
745 var retry = initialRetry;
746 var wasActivity = false;
747 var textLength = 0;
748 var headers = options.headers || {};
749 var TransportOption = options.Transport;
750 var xhr = isFetchSupported && TransportOption == undefined ? undefined : new XHRWrapper(TransportOption != undefined ? new TransportOption() : getBestXHRTransport());
751 var transport = TransportOption != null && typeof TransportOption !== "string" ? new TransportOption() : (xhr == undefined ? new FetchTransport() : new XHRTransport());
752 var abortController = undefined;
753 var timeout = 0;
754 var currentState = WAITING;
755 var dataBuffer = "";
756 var lastEventIdBuffer = "";
757 var eventTypeBuffer = "";
758
759 var textBuffer = "";
760 var state = FIELD_START;
761 var fieldStart = 0;
762 var valueStart = 0;
763
764 var onStart = function (status, statusText, contentType, headers) {
765 if (currentState === CONNECTING) {
766 if (status === 200 && contentType != undefined && contentTypeRegExp.test(contentType)) {
767 currentState = OPEN;
768 wasActivity = Date.now();
769 retry = initialRetry;
770 es.readyState = OPEN;
771 var event = new ConnectionEvent("open", {
772 status: status,
773 statusText: statusText,
774 headers: headers
775 });
776 es.dispatchEvent(event);
777 fire(es, es.onopen, event);
778 } else {
779 var message = "";
780 if (status !== 200) {
781 if (statusText) {
782 statusText = statusText.replace(/\s+/g, " ");
783 }
784 message = "EventSource's response has a status " + status + " " + statusText + " that is not 200. Aborting the connection.";
785 } else {
786 message = "EventSource's response has a Content-Type specifying an unsupported type: " + (contentType == undefined ? "-" : contentType.replace(/\s+/g, " ")) + ". Aborting the connection.";
787 }
788 close();
789 var event = new ConnectionEvent("error", {
790 status: status,
791 statusText: statusText,
792 headers: headers
793 });

Callers 6

mainTestsFunction · 0.70
onTimeoutFunction · 0.70
fFunction · 0.70
otherTestsFunction · 0.70
aFunction · 0.70
EventSourcePolyfillFunction · 0.70

Calls 4

clampDurationFunction · 0.70
parseDurationFunction · 0.70
getBestXHRTransportFunction · 0.70
onTimeoutFunction · 0.70

Tested by

no test coverage detected