websocket-ts 1.1.1 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +157 -93
  3. package/dist/cjs/src/backoff/backoff.d.ts +24 -0
  4. package/dist/cjs/src/backoff/backoff.d.ts.map +1 -0
  5. package/{lib → dist/cjs/src}/backoff/backoff.js.map +1 -1
  6. package/dist/cjs/src/backoff/constantbackoff.d.ts +18 -0
  7. package/dist/cjs/src/backoff/constantbackoff.d.ts.map +1 -0
  8. package/dist/cjs/src/backoff/constantbackoff.js +43 -0
  9. package/dist/cjs/src/backoff/constantbackoff.js.map +1 -0
  10. package/dist/cjs/src/backoff/exponentialbackoff.d.ts +48 -0
  11. package/dist/cjs/src/backoff/exponentialbackoff.d.ts.map +1 -0
  12. package/dist/cjs/src/backoff/exponentialbackoff.js +81 -0
  13. package/dist/cjs/src/backoff/exponentialbackoff.js.map +1 -0
  14. package/dist/cjs/src/backoff/linearbackoff.d.ts +51 -0
  15. package/dist/cjs/src/backoff/linearbackoff.d.ts.map +1 -0
  16. package/dist/cjs/src/backoff/linearbackoff.js +89 -0
  17. package/dist/cjs/src/backoff/linearbackoff.js.map +1 -0
  18. package/dist/cjs/src/index.d.ts +14 -0
  19. package/dist/cjs/src/index.d.ts.map +1 -0
  20. package/dist/cjs/src/index.js +20 -0
  21. package/dist/cjs/src/index.js.map +1 -0
  22. package/dist/cjs/src/queue/array_queue.d.ts +17 -0
  23. package/dist/cjs/src/queue/array_queue.d.ts.map +1 -0
  24. package/dist/cjs/src/queue/array_queue.js +36 -0
  25. package/dist/cjs/src/queue/array_queue.js.map +1 -0
  26. package/dist/cjs/src/queue/queue.d.ts +41 -0
  27. package/dist/cjs/src/queue/queue.d.ts.map +1 -0
  28. package/{lib/buffer/buffer.js → dist/cjs/src/queue/queue.js} +1 -1
  29. package/dist/cjs/src/queue/queue.js.map +1 -0
  30. package/dist/cjs/src/queue/ring_queue.d.ts +20 -0
  31. package/dist/cjs/src/queue/ring_queue.d.ts.map +1 -0
  32. package/dist/cjs/src/queue/ring_queue.js +57 -0
  33. package/dist/cjs/src/queue/ring_queue.js.map +1 -0
  34. package/dist/cjs/src/websocket.d.ts +206 -0
  35. package/dist/cjs/src/websocket.d.ts.map +1 -0
  36. package/dist/cjs/src/websocket.js +434 -0
  37. package/dist/cjs/src/websocket.js.map +1 -0
  38. package/dist/cjs/src/websocket_buffer.d.ts +16 -0
  39. package/dist/cjs/src/websocket_buffer.d.ts.map +1 -0
  40. package/dist/cjs/src/websocket_buffer.js +3 -0
  41. package/dist/cjs/src/websocket_buffer.js.map +1 -0
  42. package/dist/cjs/src/websocket_builder.d.ts +147 -0
  43. package/dist/cjs/src/websocket_builder.d.ts.map +1 -0
  44. package/dist/cjs/src/websocket_builder.js +263 -0
  45. package/dist/cjs/src/websocket_builder.js.map +1 -0
  46. package/dist/cjs/src/websocket_event.d.ts +67 -0
  47. package/dist/cjs/src/websocket_event.d.ts.map +1 -0
  48. package/dist/cjs/src/websocket_event.js +22 -0
  49. package/dist/cjs/src/websocket_event.js.map +1 -0
  50. package/dist/cjs/src/websocket_options.d.ts +21 -0
  51. package/dist/cjs/src/websocket_options.d.ts.map +1 -0
  52. package/dist/cjs/src/websocket_options.js +3 -0
  53. package/dist/cjs/src/websocket_options.js.map +1 -0
  54. package/dist/cjs/src/websocket_retry_options.d.ts +20 -0
  55. package/dist/cjs/src/websocket_retry_options.d.ts.map +1 -0
  56. package/dist/cjs/src/websocket_retry_options.js +3 -0
  57. package/dist/cjs/src/websocket_retry_options.js.map +1 -0
  58. package/dist/esm/src/backoff/backoff.d.ts +24 -0
  59. package/dist/esm/src/backoff/backoff.d.ts.map +1 -0
  60. package/dist/esm/src/backoff/backoff.js +2 -0
  61. package/dist/esm/src/backoff/backoff.js.map +1 -0
  62. package/dist/esm/src/backoff/constantbackoff.d.ts +18 -0
  63. package/dist/esm/src/backoff/constantbackoff.d.ts.map +1 -0
  64. package/dist/esm/src/backoff/constantbackoff.js +30 -0
  65. package/dist/esm/src/backoff/constantbackoff.js.map +1 -0
  66. package/dist/esm/src/backoff/exponentialbackoff.d.ts +48 -0
  67. package/dist/esm/src/backoff/exponentialbackoff.d.ts.map +1 -0
  68. package/dist/esm/src/backoff/exponentialbackoff.js +68 -0
  69. package/dist/esm/src/backoff/exponentialbackoff.js.map +1 -0
  70. package/dist/esm/src/backoff/linearbackoff.d.ts +51 -0
  71. package/dist/esm/src/backoff/linearbackoff.d.ts.map +1 -0
  72. package/dist/esm/src/backoff/linearbackoff.js +76 -0
  73. package/dist/esm/src/backoff/linearbackoff.js.map +1 -0
  74. package/dist/esm/src/index.d.ts +14 -0
  75. package/dist/esm/src/index.d.ts.map +1 -0
  76. package/dist/esm/src/index.js +9 -0
  77. package/dist/esm/src/index.js.map +1 -0
  78. package/dist/esm/src/queue/array_queue.d.ts +17 -0
  79. package/dist/esm/src/queue/array_queue.d.ts.map +1 -0
  80. package/dist/esm/src/queue/array_queue.js +31 -0
  81. package/dist/esm/src/queue/array_queue.js.map +1 -0
  82. package/dist/esm/src/queue/queue.d.ts +41 -0
  83. package/dist/esm/src/queue/queue.d.ts.map +1 -0
  84. package/dist/esm/src/queue/queue.js +2 -0
  85. package/dist/esm/src/queue/queue.js.map +1 -0
  86. package/dist/esm/src/queue/ring_queue.d.ts +20 -0
  87. package/dist/esm/src/queue/ring_queue.d.ts.map +1 -0
  88. package/dist/esm/src/queue/ring_queue.js +52 -0
  89. package/dist/esm/src/queue/ring_queue.js.map +1 -0
  90. package/dist/esm/src/websocket.d.ts +206 -0
  91. package/dist/esm/src/websocket.d.ts.map +1 -0
  92. package/dist/esm/src/websocket.js +356 -0
  93. package/dist/esm/src/websocket.js.map +1 -0
  94. package/dist/esm/src/websocket_buffer.d.ts +16 -0
  95. package/dist/esm/src/websocket_buffer.d.ts.map +1 -0
  96. package/dist/esm/src/websocket_buffer.js +2 -0
  97. package/dist/esm/src/websocket_buffer.js.map +1 -0
  98. package/dist/esm/src/websocket_builder.d.ts +147 -0
  99. package/dist/esm/src/websocket_builder.d.ts.map +1 -0
  100. package/dist/esm/src/websocket_builder.js +213 -0
  101. package/dist/esm/src/websocket_builder.js.map +1 -0
  102. package/dist/esm/src/websocket_event.d.ts +67 -0
  103. package/dist/esm/src/websocket_event.d.ts.map +1 -0
  104. package/dist/esm/src/websocket_event.js +19 -0
  105. package/dist/esm/src/websocket_event.js.map +1 -0
  106. package/dist/esm/src/websocket_options.d.ts +21 -0
  107. package/dist/esm/src/websocket_options.d.ts.map +1 -0
  108. package/dist/esm/src/websocket_options.js +2 -0
  109. package/dist/esm/src/websocket_options.js.map +1 -0
  110. package/dist/esm/src/websocket_retry_options.d.ts +20 -0
  111. package/dist/esm/src/websocket_retry_options.d.ts.map +1 -0
  112. package/dist/esm/src/websocket_retry_options.js +2 -0
  113. package/dist/esm/src/websocket_retry_options.js.map +1 -0
  114. package/package.json +27 -14
  115. package/src/backoff/backoff.ts +21 -13
  116. package/src/backoff/constantbackoff.ts +29 -11
  117. package/src/backoff/exponentialbackoff.ts +68 -28
  118. package/src/backoff/linearbackoff.ts +77 -26
  119. package/src/index.ts +23 -9
  120. package/src/queue/array_queue.ts +41 -0
  121. package/src/queue/queue.ts +46 -0
  122. package/src/queue/ring_queue.ts +69 -0
  123. package/src/websocket.ts +463 -138
  124. package/src/websocket_buffer.ts +18 -0
  125. package/src/websocket_builder.ts +274 -0
  126. package/src/websocket_event.ts +88 -0
  127. package/src/websocket_options.ts +23 -0
  128. package/src/websocket_retry_options.ts +22 -0
  129. package/{tsconfig.json → tsconfig.cjs.json} +8 -8
  130. package/tsconfig.esm.json +71 -0
  131. package/jest.config.js +0 -9
  132. package/lib/backoff/backoff.d.ts +0 -18
  133. package/lib/backoff/backoff.d.ts.map +0 -1
  134. package/lib/backoff/constantbackoff.d.ts +0 -11
  135. package/lib/backoff/constantbackoff.d.ts.map +0 -1
  136. package/lib/backoff/constantbackoff.js +0 -20
  137. package/lib/backoff/constantbackoff.js.map +0 -1
  138. package/lib/backoff/exponentialbackoff.d.ts +0 -22
  139. package/lib/backoff/exponentialbackoff.d.ts.map +0 -1
  140. package/lib/backoff/exponentialbackoff.js +0 -35
  141. package/lib/backoff/exponentialbackoff.js.map +0 -1
  142. package/lib/backoff/linearbackoff.d.ts +0 -19
  143. package/lib/backoff/linearbackoff.d.ts.map +0 -1
  144. package/lib/backoff/linearbackoff.js +0 -34
  145. package/lib/backoff/linearbackoff.js.map +0 -1
  146. package/lib/buffer/buffer.d.ts +0 -41
  147. package/lib/buffer/buffer.d.ts.map +0 -1
  148. package/lib/buffer/buffer.js.map +0 -1
  149. package/lib/buffer/lrubuffer.d.ts +0 -24
  150. package/lib/buffer/lrubuffer.d.ts.map +0 -1
  151. package/lib/buffer/lrubuffer.js +0 -76
  152. package/lib/buffer/lrubuffer.js.map +0 -1
  153. package/lib/buffer/timebuffer.d.ts +0 -25
  154. package/lib/buffer/timebuffer.d.ts.map +0 -1
  155. package/lib/buffer/timebuffer.js +0 -92
  156. package/lib/buffer/timebuffer.js.map +0 -1
  157. package/lib/index.d.ts +0 -10
  158. package/lib/index.d.ts.map +0 -1
  159. package/lib/index.js +0 -22
  160. package/lib/index.js.map +0 -1
  161. package/lib/websocket.d.ts +0 -47
  162. package/lib/websocket.d.ts.map +0 -1
  163. package/lib/websocket.js +0 -120
  164. package/lib/websocket.js.map +0 -1
  165. package/lib/websocketBuilder.d.ts +0 -32
  166. package/lib/websocketBuilder.d.ts.map +0 -1
  167. package/lib/websocketBuilder.js +0 -68
  168. package/lib/websocketBuilder.js.map +0 -1
  169. package/src/buffer/buffer.ts +0 -45
  170. package/src/buffer/lrubuffer.ts +0 -80
  171. package/src/buffer/timebuffer.ts +0 -105
  172. package/src/websocketBuilder.ts +0 -98
  173. package/test/backoff/constantbackoff.test.ts +0 -21
  174. package/test/backoff/exponentialbackoff.test.ts +0 -13
  175. package/test/backoff/linearbackoff.test.ts +0 -32
  176. package/test/buffer/common.ts +0 -10
  177. package/test/buffer/lrubuffer.test.ts +0 -128
  178. package/test/buffer/timebuffer.test.ts +0 -115
  179. package/test/websocket.test.ts +0 -341
  180. package/test/websocketBuilder.test.ts +0 -36
  181. /package/{lib → dist/cjs/src}/backoff/backoff.js +0 -0
@@ -0,0 +1,434 @@
1
+ "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.Websocket = void 0;
13
+ var websocket_event_1 = require("./websocket_event");
14
+ /**
15
+ * A websocket wrapper that can be configured to reconnect automatically and buffer messages when the websocket is not connected.
16
+ */
17
+ var Websocket = /** @class */ (function () {
18
+ /**
19
+ * Creates a new websocket.
20
+ *
21
+ * @param url to connect to.
22
+ * @param protocols optional protocols to use.
23
+ * @param options optional options to use.
24
+ */
25
+ function Websocket(url, protocols, options) {
26
+ var _this = this;
27
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
28
+ this._closedByUser = false; // whether the websocket was closed by the user
29
+ /**
30
+ * Handles the 'open' event of the browser-native websocket.
31
+ * @param event to handle.
32
+ */
33
+ this.handleOpenEvent = function (event) {
34
+ return _this.handleEvent(websocket_event_1.WebsocketEvent.open, event);
35
+ };
36
+ /**
37
+ * Handles the 'error' event of the browser-native websocket.
38
+ * @param event to handle.
39
+ */
40
+ this.handleErrorEvent = function (event) {
41
+ return _this.handleEvent(websocket_event_1.WebsocketEvent.error, event);
42
+ };
43
+ /**
44
+ * Handles the 'close' event of the browser-native websocket.
45
+ * @param event to handle.
46
+ */
47
+ this.handleCloseEvent = function (event) {
48
+ return _this.handleEvent(websocket_event_1.WebsocketEvent.close, event);
49
+ };
50
+ /**
51
+ * Handles the 'message' event of the browser-native websocket.
52
+ * @param event to handle.
53
+ */
54
+ this.handleMessageEvent = function (event) {
55
+ return _this.handleEvent(websocket_event_1.WebsocketEvent.message, event);
56
+ };
57
+ this._url = url;
58
+ this._protocols = protocols;
59
+ // make a copy of the options to prevent the user from changing them
60
+ this._options = {
61
+ buffer: options === null || options === void 0 ? void 0 : options.buffer,
62
+ retry: {
63
+ maxRetries: (_a = options === null || options === void 0 ? void 0 : options.retry) === null || _a === void 0 ? void 0 : _a.maxRetries,
64
+ instantReconnect: (_b = options === null || options === void 0 ? void 0 : options.retry) === null || _b === void 0 ? void 0 : _b.instantReconnect,
65
+ backoff: (_c = options === null || options === void 0 ? void 0 : options.retry) === null || _c === void 0 ? void 0 : _c.backoff,
66
+ },
67
+ listeners: {
68
+ open: __spreadArray([], ((_e = (_d = options === null || options === void 0 ? void 0 : options.listeners) === null || _d === void 0 ? void 0 : _d.open) !== null && _e !== void 0 ? _e : []), true),
69
+ close: __spreadArray([], ((_g = (_f = options === null || options === void 0 ? void 0 : options.listeners) === null || _f === void 0 ? void 0 : _f.close) !== null && _g !== void 0 ? _g : []), true),
70
+ error: __spreadArray([], ((_j = (_h = options === null || options === void 0 ? void 0 : options.listeners) === null || _h === void 0 ? void 0 : _h.error) !== null && _j !== void 0 ? _j : []), true),
71
+ message: __spreadArray([], ((_l = (_k = options === null || options === void 0 ? void 0 : options.listeners) === null || _k === void 0 ? void 0 : _k.message) !== null && _l !== void 0 ? _l : []), true),
72
+ retry: __spreadArray([], ((_o = (_m = options === null || options === void 0 ? void 0 : options.listeners) === null || _m === void 0 ? void 0 : _m.retry) !== null && _o !== void 0 ? _o : []), true),
73
+ reconnect: __spreadArray([], ((_q = (_p = options === null || options === void 0 ? void 0 : options.listeners) === null || _p === void 0 ? void 0 : _p.reconnect) !== null && _q !== void 0 ? _q : []), true),
74
+ },
75
+ };
76
+ this._underlyingWebsocket = this.tryConnect();
77
+ }
78
+ Object.defineProperty(Websocket.prototype, "url", {
79
+ /**
80
+ * Getter for the url.
81
+ *
82
+ * @return the url.
83
+ */
84
+ get: function () {
85
+ return this._url;
86
+ },
87
+ enumerable: false,
88
+ configurable: true
89
+ });
90
+ Object.defineProperty(Websocket.prototype, "protocols", {
91
+ /**
92
+ * Getter for the protocols.
93
+ *
94
+ * @return the protocols, or undefined if none were provided.
95
+ */
96
+ get: function () {
97
+ return this._protocols;
98
+ },
99
+ enumerable: false,
100
+ configurable: true
101
+ });
102
+ Object.defineProperty(Websocket.prototype, "buffer", {
103
+ /**
104
+ * Getter for the buffer.
105
+ *
106
+ * @return the buffer, or undefined if none was provided.
107
+ */
108
+ get: function () {
109
+ return this._options.buffer;
110
+ },
111
+ enumerable: false,
112
+ configurable: true
113
+ });
114
+ Object.defineProperty(Websocket.prototype, "maxRetries", {
115
+ /**
116
+ * Getter for the maxRetries.
117
+ *
118
+ * @return the maxRetries, or undefined if none was provided (no limit).
119
+ */
120
+ get: function () {
121
+ return this._options.retry.maxRetries;
122
+ },
123
+ enumerable: false,
124
+ configurable: true
125
+ });
126
+ Object.defineProperty(Websocket.prototype, "instantReconnect", {
127
+ /**
128
+ * Getter for the instantReconnect.
129
+ *
130
+ * @return the instantReconnect, or undefined if none was provided.
131
+ */
132
+ get: function () {
133
+ return this._options.retry.instantReconnect;
134
+ },
135
+ enumerable: false,
136
+ configurable: true
137
+ });
138
+ Object.defineProperty(Websocket.prototype, "backoff", {
139
+ /**
140
+ * Getter for the backoff.
141
+ *
142
+ * @return the backoff, or undefined if none was provided.
143
+ */
144
+ get: function () {
145
+ return this._options.retry.backoff;
146
+ },
147
+ enumerable: false,
148
+ configurable: true
149
+ });
150
+ Object.defineProperty(Websocket.prototype, "closedByUser", {
151
+ /**
152
+ * Whether the websocket was closed by the user. A websocket is closed by the user if the close().
153
+ *
154
+ * @return true if the websocket was closed by the user, false otherwise.
155
+ */
156
+ get: function () {
157
+ return this._closedByUser;
158
+ },
159
+ enumerable: false,
160
+ configurable: true
161
+ });
162
+ Object.defineProperty(Websocket.prototype, "lastConnection", {
163
+ /**
164
+ * Getter for the last 'open' event, e.g. the last time the websocket was connected.
165
+ *
166
+ * @return the last 'open' event, or undefined if the websocket was never connected.
167
+ */
168
+ get: function () {
169
+ return this._lastConnection;
170
+ },
171
+ enumerable: false,
172
+ configurable: true
173
+ });
174
+ Object.defineProperty(Websocket.prototype, "underlyingWebsocket", {
175
+ /**
176
+ * Getter for the underlying websocket. This can be used to access the browser's native websocket directly.
177
+ *
178
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
179
+ * @return the underlying websocket.
180
+ */
181
+ get: function () {
182
+ return this._underlyingWebsocket;
183
+ },
184
+ enumerable: false,
185
+ configurable: true
186
+ });
187
+ Object.defineProperty(Websocket.prototype, "readyState", {
188
+ /**
189
+ * Getter for the readyState of the underlying websocket.
190
+ *
191
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState
192
+ * @return the readyState of the underlying websocket.
193
+ */
194
+ get: function () {
195
+ return this._underlyingWebsocket.readyState;
196
+ },
197
+ enumerable: false,
198
+ configurable: true
199
+ });
200
+ Object.defineProperty(Websocket.prototype, "bufferedAmount", {
201
+ /**
202
+ * Getter for the bufferedAmount of the underlying websocket.
203
+ *
204
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/bufferedAmount
205
+ * @return the bufferedAmount of the underlying websocket.
206
+ */
207
+ get: function () {
208
+ return this._underlyingWebsocket.bufferedAmount;
209
+ },
210
+ enumerable: false,
211
+ configurable: true
212
+ });
213
+ Object.defineProperty(Websocket.prototype, "extensions", {
214
+ /**
215
+ * Getter for the extensions of the underlying websocket.
216
+ *
217
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/extensions
218
+ * @return the extensions of the underlying websocket.
219
+ */
220
+ get: function () {
221
+ return this._underlyingWebsocket.extensions;
222
+ },
223
+ enumerable: false,
224
+ configurable: true
225
+ });
226
+ Object.defineProperty(Websocket.prototype, "binaryType", {
227
+ /**
228
+ * Getter for the binaryType of the underlying websocket.
229
+ *
230
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/binaryType
231
+ * @return the binaryType of the underlying websocket.
232
+ */
233
+ get: function () {
234
+ return this._underlyingWebsocket.binaryType;
235
+ },
236
+ /**
237
+ * Setter for the binaryType of the underlying websocket.
238
+ *
239
+ * @param value to set, 'blob' or 'arraybuffer'.
240
+ */
241
+ set: function (value) {
242
+ this._underlyingWebsocket.binaryType = value;
243
+ },
244
+ enumerable: false,
245
+ configurable: true
246
+ });
247
+ /**
248
+ * Sends data over the websocket.
249
+ *
250
+ * If the websocket is not connected and a buffer was provided on creation, the data will be added to the buffer.
251
+ * If no buffer was provided or the websocket was closed by the user, the data will be dropped.
252
+ *
253
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send
254
+ * @param data to send.
255
+ */
256
+ Websocket.prototype.send = function (data) {
257
+ if (this.closedByUser)
258
+ return; // no-op if closed by user
259
+ if (this._underlyingWebsocket.readyState === this._underlyingWebsocket.OPEN) {
260
+ this._underlyingWebsocket.send(data); // websocket is connected, send data
261
+ }
262
+ else if (this.buffer !== undefined) {
263
+ this.buffer.add(data); // websocket is not connected, add data to buffer
264
+ }
265
+ };
266
+ /**
267
+ * Close the websocket. No connection-retry will be attempted after this.
268
+ *
269
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close
270
+ * @param code optional close code.
271
+ * @param reason optional close reason.
272
+ */
273
+ Websocket.prototype.close = function (code, reason) {
274
+ this.cancelScheduledConnectionRetry(); // cancel any scheduled retries
275
+ this._closedByUser = true; // mark websocket as closed by user
276
+ this._underlyingWebsocket.close(code, reason); // close underlying websocket with provided code and reason
277
+ };
278
+ /**
279
+ * Adds an event listener for the given event-type.
280
+ *
281
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
282
+ * @param type of the event to add the listener for.
283
+ * @param listener to add.
284
+ * @param options to use when adding the listener.
285
+ */
286
+ Websocket.prototype.addEventListener = function (type, listener, options) {
287
+ this._options.listeners[type].push({ listener: listener, options: options }); // add listener to list of listeners
288
+ };
289
+ /**
290
+ * Removes one or more event listener for the given event-type that match the given listener and options.
291
+ *
292
+ * @param type of the event to remove the listener for.
293
+ * @param listener to remove.
294
+ * @param options that were used when the listener was added.
295
+ */
296
+ Websocket.prototype.removeEventListener = function (type, listener, options) {
297
+ var isListenerNotToBeRemoved = function (l) { return l.listener !== listener || l.options !== options; };
298
+ this._options.listeners[type] =
299
+ this._options.listeners[type].filter(isListenerNotToBeRemoved); // only keep listeners that are not to be removed
300
+ };
301
+ /**
302
+ * Creates a new browser-native websocket and connects it to the given URL with the given protocols
303
+ * and adds all event listeners to the browser-native websocket.
304
+ *
305
+ * @return the created browser-native websocket which is also stored in the '_underlyingWebsocket' property.
306
+ */
307
+ Websocket.prototype.tryConnect = function () {
308
+ this._underlyingWebsocket = new WebSocket(this.url, this.protocols); // create new browser-native websocket and add all event listeners
309
+ this._underlyingWebsocket.addEventListener(websocket_event_1.WebsocketEvent.open, this.handleOpenEvent);
310
+ this._underlyingWebsocket.addEventListener(websocket_event_1.WebsocketEvent.close, this.handleCloseEvent);
311
+ this._underlyingWebsocket.addEventListener(websocket_event_1.WebsocketEvent.error, this.handleErrorEvent);
312
+ this._underlyingWebsocket.addEventListener(websocket_event_1.WebsocketEvent.message, this.handleMessageEvent);
313
+ return this._underlyingWebsocket;
314
+ };
315
+ /**
316
+ * Removes all event listeners from the browser-native websocket and closes it.
317
+ */
318
+ Websocket.prototype.clearWebsocket = function () {
319
+ this._underlyingWebsocket.removeEventListener(websocket_event_1.WebsocketEvent.open, this.handleOpenEvent);
320
+ this._underlyingWebsocket.removeEventListener(websocket_event_1.WebsocketEvent.close, this.handleCloseEvent);
321
+ this._underlyingWebsocket.removeEventListener(websocket_event_1.WebsocketEvent.error, this.handleErrorEvent);
322
+ this._underlyingWebsocket.removeEventListener(websocket_event_1.WebsocketEvent.message, this.handleMessageEvent);
323
+ this._underlyingWebsocket.close();
324
+ };
325
+ /**
326
+ * Dispatch an event to all listeners of the given event-type.
327
+ *
328
+ * @param type of the event to dispatch.
329
+ * @param event to dispatch.
330
+ */
331
+ Websocket.prototype.dispatchEvent = function (type, event) {
332
+ var _this = this;
333
+ var eventListeners = this._options.listeners[type];
334
+ var newEventListeners = [];
335
+ eventListeners.forEach(function (_a) {
336
+ var listener = _a.listener, options = _a.options;
337
+ listener(_this, event); // invoke listener with event
338
+ if (options === undefined ||
339
+ options.once === undefined ||
340
+ !options.once) {
341
+ newEventListeners.push({ listener: listener, options: options }); // only keep listener if it isn't a once-listener
342
+ }
343
+ });
344
+ this._options.listeners[type] = newEventListeners; // replace old listeners with new listeners that don't include once-listeners
345
+ };
346
+ /**
347
+ * Handles the given event by dispatching it to all listeners of the given event-type.
348
+ *
349
+ * @param type of the event to handle.
350
+ * @param event to handle.
351
+ */
352
+ Websocket.prototype.handleEvent = function (type, event) {
353
+ switch (type) {
354
+ case websocket_event_1.WebsocketEvent.close:
355
+ this.dispatchEvent(type, event);
356
+ this.scheduleConnectionRetryIfNeeded(); // schedule a new connection retry if the websocket was closed by the server
357
+ break;
358
+ case websocket_event_1.WebsocketEvent.open:
359
+ if (this.backoff !== undefined && this._lastConnection !== undefined) {
360
+ // websocket was reconnected, dispatch reconnect event and reset backoff
361
+ var detail = {
362
+ retries: this.backoff.retries,
363
+ lastConnection: new Date(this._lastConnection),
364
+ };
365
+ var event_1 = new CustomEvent(websocket_event_1.WebsocketEvent.reconnect, {
366
+ detail: detail,
367
+ });
368
+ this.dispatchEvent(websocket_event_1.WebsocketEvent.reconnect, event_1);
369
+ this.backoff.reset();
370
+ }
371
+ this._lastConnection = new Date();
372
+ this.dispatchEvent(type, event); // dispatch open event and send buffered data
373
+ this.sendBufferedData();
374
+ break;
375
+ case websocket_event_1.WebsocketEvent.retry:
376
+ this.dispatchEvent(type, event); // dispatch retry event and try to connect
377
+ this.clearWebsocket(); // clear the old websocket
378
+ this.tryConnect();
379
+ break;
380
+ default:
381
+ this.dispatchEvent(type, event); // dispatch event to all listeners of the given event-type
382
+ break;
383
+ }
384
+ };
385
+ /**
386
+ * Sends buffered data if there is a buffer defined.
387
+ */
388
+ Websocket.prototype.sendBufferedData = function () {
389
+ if (this.buffer === undefined) {
390
+ return; // no buffer defined, nothing to send
391
+ }
392
+ for (var ele = this.buffer.read(); ele !== undefined; ele = this.buffer.read()) {
393
+ this.send(ele); // send buffered data
394
+ }
395
+ };
396
+ /**
397
+ * Schedules a connection-retry if there is a backoff defined and the websocket was not closed by the user.
398
+ */
399
+ Websocket.prototype.scheduleConnectionRetryIfNeeded = function () {
400
+ var _this = this;
401
+ if (this.closedByUser) {
402
+ return; // user closed the websocket, no retry
403
+ }
404
+ if (this.backoff === undefined) {
405
+ return; // no backoff defined, no retry
406
+ }
407
+ var handleRetryEvent = function (detail) {
408
+ var event = new CustomEvent(websocket_event_1.WebsocketEvent.retry, { detail: detail });
409
+ _this.handleEvent(websocket_event_1.WebsocketEvent.retry, event);
410
+ };
411
+ // create retry event detail, depending on the 'instantReconnect' option
412
+ var retryEventDetail = {
413
+ backoff: this._options.retry.instantReconnect === true ? 0 : this.backoff.next(),
414
+ retries: this._options.retry.instantReconnect === true
415
+ ? 0
416
+ : this.backoff.retries,
417
+ lastConnection: this._lastConnection,
418
+ };
419
+ // schedule a new connection-retry if the maximum number of retries is not reached yet
420
+ if (this._options.retry.maxRetries === undefined ||
421
+ retryEventDetail.retries <= this._options.retry.maxRetries) {
422
+ this.retryTimeout = window.setTimeout(function () { return handleRetryEvent(retryEventDetail); }, retryEventDetail.backoff);
423
+ }
424
+ };
425
+ /**
426
+ * Cancels the scheduled connection-retry, if there is one.
427
+ */
428
+ Websocket.prototype.cancelScheduledConnectionRetry = function () {
429
+ window.clearTimeout(this.retryTimeout);
430
+ };
431
+ return Websocket;
432
+ }());
433
+ exports.Websocket = Websocket;
434
+ //# sourceMappingURL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AAM,KAAK,GAAkC,IAAI,WAAW,CAC1D,gCAAc,CAAC,KAAK,EACpB,EAAE,MAAM,QAAA,EAAE,CACX,CAAC;YACF,KAAI,CAAC,WAAW,CAAC,gCAAc,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAChD,CAAC,CAAC;QAEF,wEAAwE;QACxE,IAAM,gBAAgB,GAAqB;YACzC,OAAO,EACL,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,gBAAgB,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE;YACzE,OAAO,EACL,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,gBAAgB,KAAK,IAAI;gBAC3C,CAAC,CAAC,CAAC;gBACH,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO;YAC1B,cAAc,EAAE,IAAI,CAAC,eAAe;SACrC,CAAC;QAEF,sFAAsF;QACtF,IACE,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,UAAU,KAAK,SAAS;YAC5C,gBAAgB,CAAC,OAAO,IAAI,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,UAAU,EAC1D;YACA,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,UAAU,CACnC,cAAM,OAAA,gBAAgB,CAAC,gBAAgB,CAAC,EAAlC,CAAkC,EACxC,gBAAgB,CAAC,OAAO,CACzB,CAAC;SACH;IACH,CAAC;IAED;;OAEG;IACK,kDAA8B,GAAtC;QACE,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACzC,CAAC;IACH,gBAAC;AAAD,CAAC,AArdD,IAqdC;AArdY,8BAAS"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * A WebsocketBuffer is used to store messages temporarily until they can be sent.
3
+ */
4
+ export interface WebsocketBuffer<E = string | ArrayBufferLike | Blob | ArrayBufferView> {
5
+ /**
6
+ * Adds an element to the buffer.
7
+ * @param element the element to add
8
+ */
9
+ add(element: E): void;
10
+ /**
11
+ * Reads an element from the buffer.
12
+ * @return an element from the buffer or undefined if the buffer is empty
13
+ */
14
+ read(): E | undefined;
15
+ }
16
+ //# sourceMappingURL=websocket_buffer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket_buffer.d.ts","sourceRoot":"","sources":["../../../src/websocket_buffer.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,eAAe,CAC9B,CAAC,GAAG,MAAM,GAAG,eAAe,GAAG,IAAI,GAAG,eAAe;IAErD;;;OAGG;IACH,GAAG,CAAC,OAAO,EAAE,CAAC,GAAG,IAAI,CAAC;IAEtB;;;OAGG;IACH,IAAI,IAAI,CAAC,GAAG,SAAS,CAAC;CACvB"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=websocket_buffer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket_buffer.js","sourceRoot":"","sources":["../../../src/websocket_buffer.ts"],"names":[],"mappings":""}
@@ -0,0 +1,147 @@
1
+ import { Backoff } from "./backoff/backoff";
2
+ import { WebsocketEvent, WebsocketEventListener, WebsocketEventListenerOptions } from "./websocket_event";
3
+ import { Websocket } from "./websocket";
4
+ import { WebsocketBuffer } from "./websocket_buffer";
5
+ /**
6
+ * Builder for websockets.
7
+ */
8
+ export declare class WebsocketBuilder {
9
+ private readonly _url;
10
+ private _protocols?;
11
+ private _options?;
12
+ /**
13
+ * Creates a new WebsocketBuilder.
14
+ *
15
+ * @param url the url to connect to
16
+ */
17
+ constructor(url: string);
18
+ /**
19
+ * Getter for the url.
20
+ *
21
+ * @returns the url
22
+ */
23
+ get url(): string;
24
+ /**
25
+ * Adds protocols to the websocket. Subsequent calls to this method will override the previously set protocols.
26
+ *
27
+ * @param protocols the protocols to add
28
+ */
29
+ withProtocols(protocols: string | string[] | undefined): WebsocketBuilder;
30
+ /**
31
+ * Getter for the protocols.
32
+ *
33
+ * @returns the protocols, undefined if no protocols have been set
34
+ */
35
+ get protocols(): string | string[] | undefined;
36
+ /**
37
+ * Sets the maximum number of retries before giving up. No limit if undefined.
38
+ *
39
+ * @param maxRetries the maximum number of retries before giving up
40
+ */
41
+ withMaxRetries(maxRetries: number | undefined): WebsocketBuilder;
42
+ /**
43
+ * Getter for the maximum number of retries before giving up.
44
+ *
45
+ * @returns the maximum number of retries before giving up, undefined if no maximum has been set
46
+ */
47
+ get maxRetries(): number | undefined;
48
+ /**
49
+ * Sets wether to reconnect immediately after a connection has been lost, ignoring the backoff strategy for the first retry.
50
+ *
51
+ * @param instantReconnect wether to reconnect immediately after a connection has been lost
52
+ */
53
+ withInstantReconnect(instantReconnect: boolean | undefined): WebsocketBuilder;
54
+ /**
55
+ * Getter for wether to reconnect immediately after a connection has been lost, ignoring the backoff strategy for the first retry.
56
+ *
57
+ * @returns wether to reconnect immediately after a connection has been lost, undefined if no value has been set
58
+ */
59
+ get instantReconnect(): boolean | undefined;
60
+ /**
61
+ * Adds a backoff to the websocket. Subsequent calls to this method will override the previously set backoff.
62
+ *
63
+ * @param backoff the backoff to add
64
+ */
65
+ withBackoff(backoff: Backoff | undefined): WebsocketBuilder;
66
+ /**
67
+ * Getter for the backoff.
68
+ *
69
+ * @returns the backoff, undefined if no backoff has been set
70
+ */
71
+ get backoff(): Backoff | undefined;
72
+ /**
73
+ * Adds a buffer to the websocket. Subsequent calls to this method will override the previously set buffer.
74
+ *
75
+ * @param buffer the buffer to add
76
+ */
77
+ withBuffer(buffer: WebsocketBuffer | undefined): WebsocketBuilder;
78
+ /**
79
+ * Getter for the buffer.
80
+ *
81
+ * @returns the buffer, undefined if no buffer has been set
82
+ */
83
+ get buffer(): WebsocketBuffer | undefined;
84
+ /**
85
+ * Adds an 'open' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
86
+ * called in the order they were added.
87
+ *
88
+ * @param listener the listener to add
89
+ * @param options the listener options
90
+ */
91
+ onOpen(listener: WebsocketEventListener<WebsocketEvent.open>, options?: WebsocketEventListenerOptions): WebsocketBuilder;
92
+ /**
93
+ * Adds an 'close' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
94
+ * called in the order they were added.
95
+ *
96
+ * @param listener the listener to add
97
+ * @param options the listener options
98
+ */
99
+ onClose(listener: WebsocketEventListener<WebsocketEvent.close>, options?: WebsocketEventListenerOptions): WebsocketBuilder;
100
+ /**
101
+ * Adds an 'error' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
102
+ * called in the order they were added.
103
+ *
104
+ * @param listener the listener to add
105
+ * @param options the listener options
106
+ */
107
+ onError(listener: WebsocketEventListener<WebsocketEvent.error>, options?: WebsocketEventListenerOptions): WebsocketBuilder;
108
+ /**
109
+ * Adds an 'message' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
110
+ * called in the order they were added.
111
+ *
112
+ * @param listener the listener to add
113
+ * @param options the listener options
114
+ */
115
+ onMessage(listener: WebsocketEventListener<WebsocketEvent.message>, options?: WebsocketEventListenerOptions): WebsocketBuilder;
116
+ /**
117
+ * Adds an 'retry' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
118
+ * called in the order they were added.
119
+ *
120
+ * @param listener the listener to add
121
+ * @param options the listener options
122
+ */
123
+ onRetry(listener: WebsocketEventListener<WebsocketEvent.retry>, options?: WebsocketEventListenerOptions): WebsocketBuilder;
124
+ /**
125
+ * Adds an 'reconnect' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
126
+ * called in the order they were added.
127
+ *
128
+ * @param listener the listener to add
129
+ * @param options the listener options
130
+ */
131
+ onReconnect(listener: WebsocketEventListener<WebsocketEvent.reconnect>, options?: WebsocketEventListenerOptions): WebsocketBuilder;
132
+ /**
133
+ * Builds the websocket.
134
+ *
135
+ * @return a new websocket, with the set options
136
+ */
137
+ build(): Websocket;
138
+ /**
139
+ * Adds an event listener to the options.
140
+ *
141
+ * @param event the event to add the listener to
142
+ * @param listener the listener to add
143
+ * @param options the listener options
144
+ */
145
+ private addListener;
146
+ }
147
+ //# sourceMappingURL=websocket_builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket_builder.d.ts","sourceRoot":"","sources":["../../../src/websocket_builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EACL,cAAc,EACd,sBAAsB,EACtB,6BAA6B,EAC9B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAGrD;;GAEG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAE9B,OAAO,CAAC,UAAU,CAAC,CAAoB;IACvC,OAAO,CAAC,QAAQ,CAAC,CAAmB;IAEpC;;;;OAIG;gBACS,GAAG,EAAE,MAAM;IAIvB;;;;OAIG;IACH,IAAI,GAAG,IAAI,MAAM,CAEhB;IAED;;;;OAIG;IACI,aAAa,CAClB,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,GACvC,gBAAgB;IAKnB;;;;OAIG;IACH,IAAI,SAAS,IAAI,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAE7C;IAED;;;;OAIG;IACI,cAAc,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,GAAG,gBAAgB;IAQvE;;;;OAIG;IACH,IAAI,UAAU,IAAI,MAAM,GAAG,SAAS,CAEnC;IAED;;;;OAIG;IACI,oBAAoB,CACzB,gBAAgB,EAAE,OAAO,GAAG,SAAS,GACpC,gBAAgB;IAQnB;;;;OAIG;IACH,IAAI,gBAAgB,IAAI,OAAO,GAAG,SAAS,CAE1C;IAED;;;;OAIG;IACI,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,GAAG,gBAAgB;IAQlE;;;;OAIG;IACH,IAAI,OAAO,IAAI,O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