1function _class_call_check(instance, Constructor) { 2 if (!(instance instanceof Constructor)) { 3 throw new TypeError("Cannot call a class as a function"); 4 } 5} 6function _defineProperties(target, props) { 7 for(var i = 0; i < props.length; i++){ 8 var descriptor = props[i]; 9 descriptor.enumerable = descriptor.enumerable || false; 10 descriptor.configurable = true; 11 if ("value" in descriptor) descriptor.writable = true; 12 Object.defineProperty(target, descriptor.key, descriptor); 13 } 14} 15function _create_class(Constructor, protoProps, staticProps) { 16 if (protoProps) _defineProperties(Constructor.prototype, protoProps); 17 if (staticProps) _defineProperties(Constructor, staticProps); 18 return Constructor; 19} 20function _define_property(obj, key, value) { 21 if (key in obj) { 22 Object.defineProperty(obj, key, { 23 value: value, 24 enumerable: true, 25 configurable: true, 26 writable: true 27 }); 28 } else { 29 obj[key] = value; 30 } 31 return obj; 32} 33var MAX_RECONNECT_ATTEMPTS = 12; 34var MAX_RECONNECT_DELAY_MS = 10000; 35export var StreamingMessageEventSource = /*#__PURE__*/ function() { 36 "use strict"; 37 function StreamingMessageEventSource(url, callbacks) { 38 var lastStreamId = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : null; 39 var _this = this; 40 _class_call_check(this, StreamingMessageEventSource); 41 _define_property(this, "url", void 0); 42 _define_property(this, "callbacks", void 0); 43 _define_property(this, "lastStreamId", void 0); 44 _define_property(this, "eventSource", void 0); 45 _define_property(this, "reconnectTimer", void 0); 46 _define_property(this, "reconnectAttempts", void 0); 47 _define_property(this, "isDisposed", void 0); 48 _define_property(this, "isFinished", void 0); 49 _define_property(this, "isPaused", void 0); 50 _define_property(this, "onOpen", void 0); 51 _define_property(this, "onError", void 0); 52 this.url = url; 53 this.callbacks = callbacks; 54 this.lastStreamId = lastStreamId; 55 this.eventSource = null; 56 this.reconnectTimer = null; 57 this.reconnectAttempts = 0; 58 this.isDisposed = false; 59 this.isFinished = false; 60 this.isPaused = false; 61 this.onOpen = function() { 62 _this.reconnectAttempts = 0; 63 }; 64 this.onError = function() { 65 if (_this.isDisposed || _this.isFinished || _this.isPaused) { 66 return; 67 } 68 _this.closeStream(); 69 _this.scheduleReconnect(); 70 }; 71 } 72 _create_class(StreamingMessageEventSource, [ 73 { 74 key: "start", 75 value: function start() { 76 this.isDisposed = false; 77 this.isFinished = false; 78 this.isPaused = false; 79 this.reconnectAttempts = 0; 80 this.openStream(); 81 } 82 }, 83 { 84 key: "dispose", 85 value: function dispose() { 86 this.isDisposed = true; 87 this.clearReconnectTimer(); 88 this.closeStream(); 89 } 90 }, 91 { 92 key: "pause", 93 value: function pause() { 94 this.isPaused = true; 95 this.clearReconnectTimer(); 96 this.closeStream(); 97 } 98 }, 99 { 100 key: "resume", 101 value: function resume() { 102 if (this.isDisposed || this.isFinished) { 103 return; 104 } 105 this.isPaused = false; 106 this.reconnectAttempts = 0; 107 this.openStream(); 108 } 109 }, 110 { 111 key: "finish", 112 value: function finish() { 113 this.isFinished = true; 114 this.clearReconnectTimer(); 115 this.closeStream(); 116 } 117 }, 118 { 119 key: "setLastStreamId", 120 value: function setLastStreamId(id) { 121 this.lastStreamId = id; 122 } 123 }, 124 { 125 key: "openStream", 126 value: function openStream() { 127 var streamUrl = this.buildStreamUrl(); 128 this.closeStream(); 129 this.eventSource = new EventSource(streamUrl); 130 this.eventSource.onopen = this.onOpen; 131 this.eventSource.onmessage = this.callbacks.onMessage; 132 this.eventSource.onerror = this.onError; 133 } 134 }, 135 { 136 key: "closeStream", 137 value: function closeStream() { 138 if (!this.eventSource) { 139 return; 140 } 141 this.eventSource.close(); 142 this.eventSource = null; 143 } 144 }, 145 { 146 key: "buildStreamUrl", 147 value: function buildStreamUrl() { 148 if (!this.lastStreamId) { 149 return this.url; 150 } 151 var url = new URL(this.url, window.location.origin); 152 url.searchParams.set("lastId", this.lastStreamId); 153 return url.toString(); 154 } 155 }, 156 { 157 key: "scheduleReconnect", 158 value: function scheduleReconnect() { 159 var _this = this; 160 this.clearReconnectTimer(); 161 if (this.reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) { 162 this.callbacks.onReconnectLimitReached(); 163 return; 164 }
165 var delay = Math.min(1000 * Math.pow(2, this.reconnectAttempts), MAX_RECONNECT_DELAY_MS); 166 this.reconnectAttempts += 1; 167 this.reconnectTimer = window.setTimeout(function() { 168 if (_this.isDisposed || _this.isFinished) { 169 return; 170 } 171 _this.openStream(); 172 }, delay); 173 } 174 }, 175 { 176 key: "clearReconnectTimer", 177 value: function clearReconnectTimer() { 178 if (this.reconnectTimer === null) { 179 return; 180 } 181 window.clearTimeout(this.reconnectTimer); 182 this.reconnectTimer = null; 183 } 184 } 185 ]); 186 return StreamingMessageEventSource; 187}();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.