├── sample.html
└── speech.browser.sdk.js
/sample.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
208 |
209 |
210 |
308 |
309 |
--------------------------------------------------------------------------------
/speech.browser.sdk.js:
--------------------------------------------------------------------------------
1 | var __extends = (this && this.__extends) || (function () {
2 | var extendStatics = Object.setPrototypeOf ||
3 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
4 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
5 | return function (d, b) {
6 | extendStatics(d, b);
7 | function __() { this.constructor = d; }
8 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
9 | };
10 | })();
11 | define("src/common/Guid", ["require", "exports"], function (require, exports) {
12 | "use strict";
13 | Object.defineProperty(exports, "__esModule", { value: true });
14 | var CreateGuid = function () {
15 | var d = new Date().getTime();
16 | var guid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
17 | var r = (d + Math.random() * 16) % 16 | 0;
18 | d = Math.floor(d / 16);
19 | return (c === "x" ? r : (r & 0x3 | 0x8)).toString(16);
20 | });
21 | return guid;
22 | };
23 | exports.CreateGuid = CreateGuid;
24 | var CreateNoDashGuid = function () {
25 | return CreateGuid().replace(new RegExp("-", "g"), "").toUpperCase();
26 | };
27 | exports.CreateNoDashGuid = CreateNoDashGuid;
28 | });
29 | define("src/common/IDictionary", ["require", "exports"], function (require, exports) {
30 | "use strict";
31 | Object.defineProperty(exports, "__esModule", { value: true });
32 | });
33 | define("src/common/PlatformEvent", ["require", "exports", "src/common/Guid"], function (require, exports, Guid_1) {
34 | "use strict";
35 | Object.defineProperty(exports, "__esModule", { value: true });
36 | var EventType;
37 | (function (EventType) {
38 | EventType[EventType["Debug"] = 0] = "Debug";
39 | EventType[EventType["Info"] = 1] = "Info";
40 | EventType[EventType["Warning"] = 2] = "Warning";
41 | EventType[EventType["Error"] = 3] = "Error";
42 | })(EventType = exports.EventType || (exports.EventType = {}));
43 | var PlatformEvent = (function () {
44 | function PlatformEvent(eventName, eventType) {
45 | this.name = eventName;
46 | this.eventId = Guid_1.CreateNoDashGuid();
47 | this.eventTime = new Date().toISOString();
48 | this.eventType = eventType;
49 | this.metadata = {};
50 | }
51 | Object.defineProperty(PlatformEvent.prototype, "Name", {
52 | get: function () {
53 | return this.name;
54 | },
55 | enumerable: true,
56 | configurable: true
57 | });
58 | Object.defineProperty(PlatformEvent.prototype, "EventId", {
59 | get: function () {
60 | return this.eventId;
61 | },
62 | enumerable: true,
63 | configurable: true
64 | });
65 | Object.defineProperty(PlatformEvent.prototype, "EventTime", {
66 | get: function () {
67 | return this.eventTime;
68 | },
69 | enumerable: true,
70 | configurable: true
71 | });
72 | Object.defineProperty(PlatformEvent.prototype, "EventType", {
73 | get: function () {
74 | return this.eventType;
75 | },
76 | enumerable: true,
77 | configurable: true
78 | });
79 | Object.defineProperty(PlatformEvent.prototype, "Metadata", {
80 | get: function () {
81 | return this.metadata;
82 | },
83 | enumerable: true,
84 | configurable: true
85 | });
86 | return PlatformEvent;
87 | }());
88 | exports.PlatformEvent = PlatformEvent;
89 | });
90 | define("src/common/AudioSourceEvents", ["require", "exports", "src/common/PlatformEvent"], function (require, exports, PlatformEvent_1) {
91 | "use strict";
92 | Object.defineProperty(exports, "__esModule", { value: true });
93 | var AudioSourceEvent = (function (_super) {
94 | __extends(AudioSourceEvent, _super);
95 | function AudioSourceEvent(eventName, audioSourceId, eventType) {
96 | if (eventType === void 0) { eventType = PlatformEvent_1.EventType.Info; }
97 | var _this = _super.call(this, eventName, eventType) || this;
98 | _this.audioSourceId = audioSourceId;
99 | return _this;
100 | }
101 | Object.defineProperty(AudioSourceEvent.prototype, "AudioSourceId", {
102 | get: function () {
103 | return this.audioSourceId;
104 | },
105 | enumerable: true,
106 | configurable: true
107 | });
108 | return AudioSourceEvent;
109 | }(PlatformEvent_1.PlatformEvent));
110 | exports.AudioSourceEvent = AudioSourceEvent;
111 | var AudioSourceInitializingEvent = (function (_super) {
112 | __extends(AudioSourceInitializingEvent, _super);
113 | function AudioSourceInitializingEvent(audioSourceId) {
114 | return _super.call(this, "AudioSourceInitializingEvent", audioSourceId) || this;
115 | }
116 | return AudioSourceInitializingEvent;
117 | }(AudioSourceEvent));
118 | exports.AudioSourceInitializingEvent = AudioSourceInitializingEvent;
119 | var AudioSourceReadyEvent = (function (_super) {
120 | __extends(AudioSourceReadyEvent, _super);
121 | function AudioSourceReadyEvent(audioSourceId) {
122 | return _super.call(this, "AudioSourceReadyEvent", audioSourceId) || this;
123 | }
124 | return AudioSourceReadyEvent;
125 | }(AudioSourceEvent));
126 | exports.AudioSourceReadyEvent = AudioSourceReadyEvent;
127 | var AudioSourceOffEvent = (function (_super) {
128 | __extends(AudioSourceOffEvent, _super);
129 | function AudioSourceOffEvent(audioSourceId) {
130 | return _super.call(this, "AudioSourceOffEvent", audioSourceId) || this;
131 | }
132 | return AudioSourceOffEvent;
133 | }(AudioSourceEvent));
134 | exports.AudioSourceOffEvent = AudioSourceOffEvent;
135 | var AudioSourceErrorEvent = (function (_super) {
136 | __extends(AudioSourceErrorEvent, _super);
137 | function AudioSourceErrorEvent(audioSourceId, error) {
138 | var _this = _super.call(this, "AudioSourceErrorEvent", audioSourceId, PlatformEvent_1.EventType.Error) || this;
139 | _this.error = error;
140 | return _this;
141 | }
142 | Object.defineProperty(AudioSourceErrorEvent.prototype, "Error", {
143 | get: function () {
144 | return this.error;
145 | },
146 | enumerable: true,
147 | configurable: true
148 | });
149 | return AudioSourceErrorEvent;
150 | }(AudioSourceEvent));
151 | exports.AudioSourceErrorEvent = AudioSourceErrorEvent;
152 | var AudioStreamNodeEvent = (function (_super) {
153 | __extends(AudioStreamNodeEvent, _super);
154 | function AudioStreamNodeEvent(eventName, audioSourceId, audioNodeId) {
155 | var _this = _super.call(this, eventName, audioSourceId) || this;
156 | _this.audioNodeId = audioNodeId;
157 | return _this;
158 | }
159 | Object.defineProperty(AudioStreamNodeEvent.prototype, "AudioNodeId", {
160 | get: function () {
161 | return this.audioNodeId;
162 | },
163 | enumerable: true,
164 | configurable: true
165 | });
166 | return AudioStreamNodeEvent;
167 | }(AudioSourceEvent));
168 | exports.AudioStreamNodeEvent = AudioStreamNodeEvent;
169 | var AudioStreamNodeAttachingEvent = (function (_super) {
170 | __extends(AudioStreamNodeAttachingEvent, _super);
171 | function AudioStreamNodeAttachingEvent(audioSourceId, audioNodeId) {
172 | return _super.call(this, "AudioStreamNodeAttachingEvent", audioSourceId, audioNodeId) || this;
173 | }
174 | return AudioStreamNodeAttachingEvent;
175 | }(AudioStreamNodeEvent));
176 | exports.AudioStreamNodeAttachingEvent = AudioStreamNodeAttachingEvent;
177 | var AudioStreamNodeAttachedEvent = (function (_super) {
178 | __extends(AudioStreamNodeAttachedEvent, _super);
179 | function AudioStreamNodeAttachedEvent(audioSourceId, audioNodeId) {
180 | return _super.call(this, "AudioStreamNodeAttachedEvent", audioSourceId, audioNodeId) || this;
181 | }
182 | return AudioStreamNodeAttachedEvent;
183 | }(AudioStreamNodeEvent));
184 | exports.AudioStreamNodeAttachedEvent = AudioStreamNodeAttachedEvent;
185 | var AudioStreamNodeDetachedEvent = (function (_super) {
186 | __extends(AudioStreamNodeDetachedEvent, _super);
187 | function AudioStreamNodeDetachedEvent(audioSourceId, audioNodeId) {
188 | return _super.call(this, "AudioStreamNodeDetachedEvent", audioSourceId, audioNodeId) || this;
189 | }
190 | return AudioStreamNodeDetachedEvent;
191 | }(AudioStreamNodeEvent));
192 | exports.AudioStreamNodeDetachedEvent = AudioStreamNodeDetachedEvent;
193 | var AudioStreamNodeErrorEvent = (function (_super) {
194 | __extends(AudioStreamNodeErrorEvent, _super);
195 | function AudioStreamNodeErrorEvent(audioSourceId, audioNodeId, error) {
196 | var _this = _super.call(this, "AudioStreamNodeErrorEvent", audioSourceId, audioNodeId) || this;
197 | _this.error = error;
198 | return _this;
199 | }
200 | Object.defineProperty(AudioStreamNodeErrorEvent.prototype, "Error", {
201 | get: function () {
202 | return this.error;
203 | },
204 | enumerable: true,
205 | configurable: true
206 | });
207 | return AudioStreamNodeErrorEvent;
208 | }(AudioStreamNodeEvent));
209 | exports.AudioStreamNodeErrorEvent = AudioStreamNodeErrorEvent;
210 | });
211 | define("src/common/Error", ["require", "exports"], function (require, exports) {
212 | "use strict";
213 | Object.defineProperty(exports, "__esModule", { value: true });
214 | var ArgumentNullError = (function (_super) {
215 | __extends(ArgumentNullError, _super);
216 | function ArgumentNullError(argumentName) {
217 | var _this = _super.call(this, argumentName) || this;
218 | _this.name = "ArgumentNull";
219 | _this.message = argumentName;
220 | return _this;
221 | }
222 | return ArgumentNullError;
223 | }(Error));
224 | exports.ArgumentNullError = ArgumentNullError;
225 | var InvalidOperationError = (function (_super) {
226 | __extends(InvalidOperationError, _super);
227 | function InvalidOperationError(error) {
228 | var _this = _super.call(this, error) || this;
229 | _this.name = "InvalidOperation";
230 | _this.message = error;
231 | return _this;
232 | }
233 | return InvalidOperationError;
234 | }(Error));
235 | exports.InvalidOperationError = InvalidOperationError;
236 | var ObjectDisposedError = (function (_super) {
237 | __extends(ObjectDisposedError, _super);
238 | function ObjectDisposedError(objectName, error) {
239 | var _this = _super.call(this, error) || this;
240 | _this.name = objectName + "ObjectDisposed";
241 | _this.message = error;
242 | return _this;
243 | }
244 | return ObjectDisposedError;
245 | }(Error));
246 | exports.ObjectDisposedError = ObjectDisposedError;
247 | });
248 | define("src/common/ConnectionMessage", ["require", "exports", "src/common/Error", "src/common/Guid"], function (require, exports, Error_1, Guid_2) {
249 | "use strict";
250 | Object.defineProperty(exports, "__esModule", { value: true });
251 | var MessageType;
252 | (function (MessageType) {
253 | MessageType[MessageType["Text"] = 0] = "Text";
254 | MessageType[MessageType["Binary"] = 1] = "Binary";
255 | })(MessageType = exports.MessageType || (exports.MessageType = {}));
256 | var ConnectionMessage = (function () {
257 | function ConnectionMessage(messageType, body, headers, id) {
258 | this.body = null;
259 | if (messageType === MessageType.Text && body && !(typeof (body) === "string")) {
260 | throw new Error_1.InvalidOperationError("Payload must be a string");
261 | }
262 | if (messageType === MessageType.Binary && body && !(body instanceof ArrayBuffer)) {
263 | throw new Error_1.InvalidOperationError("Payload must be ArrayBuffer");
264 | }
265 | this.messageType = messageType;
266 | this.body = body;
267 | this.headers = headers ? headers : {};
268 | this.id = id ? id : Guid_2.CreateNoDashGuid();
269 | }
270 | Object.defineProperty(ConnectionMessage.prototype, "MessageType", {
271 | get: function () {
272 | return this.messageType;
273 | },
274 | enumerable: true,
275 | configurable: true
276 | });
277 | Object.defineProperty(ConnectionMessage.prototype, "Headers", {
278 | get: function () {
279 | return this.headers;
280 | },
281 | enumerable: true,
282 | configurable: true
283 | });
284 | Object.defineProperty(ConnectionMessage.prototype, "Body", {
285 | get: function () {
286 | return this.body;
287 | },
288 | enumerable: true,
289 | configurable: true
290 | });
291 | Object.defineProperty(ConnectionMessage.prototype, "TextBody", {
292 | get: function () {
293 | if (this.messageType === MessageType.Binary) {
294 | throw new Error_1.InvalidOperationError("Not supported for binary message");
295 | }
296 | return this.body;
297 | },
298 | enumerable: true,
299 | configurable: true
300 | });
301 | Object.defineProperty(ConnectionMessage.prototype, "BinaryBody", {
302 | get: function () {
303 | if (this.messageType === MessageType.Text) {
304 | throw new Error_1.InvalidOperationError("Not supported for text message");
305 | }
306 | return this.body;
307 | },
308 | enumerable: true,
309 | configurable: true
310 | });
311 | Object.defineProperty(ConnectionMessage.prototype, "Id", {
312 | get: function () {
313 | return this.id;
314 | },
315 | enumerable: true,
316 | configurable: true
317 | });
318 | return ConnectionMessage;
319 | }());
320 | exports.ConnectionMessage = ConnectionMessage;
321 | });
322 | define("src/common/ConnectionEvents", ["require", "exports", "src/common/PlatformEvent"], function (require, exports, PlatformEvent_2) {
323 | "use strict";
324 | Object.defineProperty(exports, "__esModule", { value: true });
325 | var ConnectionEvent = (function (_super) {
326 | __extends(ConnectionEvent, _super);
327 | function ConnectionEvent(eventName, connectionId, eventType) {
328 | if (eventType === void 0) { eventType = PlatformEvent_2.EventType.Info; }
329 | var _this = _super.call(this, eventName, eventType) || this;
330 | _this.connectionId = connectionId;
331 | return _this;
332 | }
333 | Object.defineProperty(ConnectionEvent.prototype, "ConnectionId", {
334 | get: function () {
335 | return this.connectionId;
336 | },
337 | enumerable: true,
338 | configurable: true
339 | });
340 | return ConnectionEvent;
341 | }(PlatformEvent_2.PlatformEvent));
342 | exports.ConnectionEvent = ConnectionEvent;
343 | var ConnectionStartEvent = (function (_super) {
344 | __extends(ConnectionStartEvent, _super);
345 | function ConnectionStartEvent(connectionId, uri, headers) {
346 | var _this = _super.call(this, "ConnectionStartEvent", connectionId) || this;
347 | _this.uri = uri;
348 | _this.headers = headers;
349 | return _this;
350 | }
351 | Object.defineProperty(ConnectionStartEvent.prototype, "Uri", {
352 | get: function () {
353 | return this.uri;
354 | },
355 | enumerable: true,
356 | configurable: true
357 | });
358 | Object.defineProperty(ConnectionStartEvent.prototype, "Headers", {
359 | get: function () {
360 | return this.headers;
361 | },
362 | enumerable: true,
363 | configurable: true
364 | });
365 | return ConnectionStartEvent;
366 | }(ConnectionEvent));
367 | exports.ConnectionStartEvent = ConnectionStartEvent;
368 | var ConnectionEstablishedEvent = (function (_super) {
369 | __extends(ConnectionEstablishedEvent, _super);
370 | function ConnectionEstablishedEvent(connectionId, metadata) {
371 | return _super.call(this, "ConnectionEstablishedEvent", connectionId) || this;
372 | }
373 | return ConnectionEstablishedEvent;
374 | }(ConnectionEvent));
375 | exports.ConnectionEstablishedEvent = ConnectionEstablishedEvent;
376 | var ConnectionClosedEvent = (function (_super) {
377 | __extends(ConnectionClosedEvent, _super);
378 | function ConnectionClosedEvent(connectionId, statusCode, reason) {
379 | var _this = _super.call(this, "ConnectionClosedEvent", connectionId, PlatformEvent_2.EventType.Warning) || this;
380 | _this.reason = reason;
381 | _this.statusCode = statusCode;
382 | return _this;
383 | }
384 | Object.defineProperty(ConnectionClosedEvent.prototype, "Reason", {
385 | get: function () {
386 | return this.reason;
387 | },
388 | enumerable: true,
389 | configurable: true
390 | });
391 | Object.defineProperty(ConnectionClosedEvent.prototype, "StatusCode", {
392 | get: function () {
393 | return this.statusCode;
394 | },
395 | enumerable: true,
396 | configurable: true
397 | });
398 | return ConnectionClosedEvent;
399 | }(ConnectionEvent));
400 | exports.ConnectionClosedEvent = ConnectionClosedEvent;
401 | var ConnectionEstablishErrorEvent = (function (_super) {
402 | __extends(ConnectionEstablishErrorEvent, _super);
403 | function ConnectionEstablishErrorEvent(connectionId, statuscode, reason) {
404 | var _this = _super.call(this, "ConnectionEstablishErrorEvent", connectionId, PlatformEvent_2.EventType.Error) || this;
405 | _this.statusCode = statuscode;
406 | _this.reason = reason;
407 | return _this;
408 | }
409 | Object.defineProperty(ConnectionEstablishErrorEvent.prototype, "Reason", {
410 | get: function () {
411 | return this.reason;
412 | },
413 | enumerable: true,
414 | configurable: true
415 | });
416 | Object.defineProperty(ConnectionEstablishErrorEvent.prototype, "StatusCode", {
417 | get: function () {
418 | return this.statusCode;
419 | },
420 | enumerable: true,
421 | configurable: true
422 | });
423 | return ConnectionEstablishErrorEvent;
424 | }(ConnectionEvent));
425 | exports.ConnectionEstablishErrorEvent = ConnectionEstablishErrorEvent;
426 | var ConnectionMessageReceivedEvent = (function (_super) {
427 | __extends(ConnectionMessageReceivedEvent, _super);
428 | function ConnectionMessageReceivedEvent(connectionId, networkReceivedTimeISO, message) {
429 | var _this = _super.call(this, "ConnectionMessageReceivedEvent", connectionId) || this;
430 | _this.networkReceivedTime = networkReceivedTimeISO;
431 | _this.message = message;
432 | return _this;
433 | }
434 | Object.defineProperty(ConnectionMessageReceivedEvent.prototype, "NetworkReceivedTime", {
435 | get: function () {
436 | return this.networkReceivedTime;
437 | },
438 | enumerable: true,
439 | configurable: true
440 | });
441 | Object.defineProperty(ConnectionMessageReceivedEvent.prototype, "Message", {
442 | get: function () {
443 | return this.message;
444 | },
445 | enumerable: true,
446 | configurable: true
447 | });
448 | return ConnectionMessageReceivedEvent;
449 | }(ConnectionEvent));
450 | exports.ConnectionMessageReceivedEvent = ConnectionMessageReceivedEvent;
451 | var ConnectionMessageSentEvent = (function (_super) {
452 | __extends(ConnectionMessageSentEvent, _super);
453 | function ConnectionMessageSentEvent(connectionId, networkSentTimeISO, message) {
454 | var _this = _super.call(this, "ConnectionMessageSentEvent", connectionId) || this;
455 | _this.networkSentTime = networkSentTimeISO;
456 | _this.message = message;
457 | return _this;
458 | }
459 | Object.defineProperty(ConnectionMessageSentEvent.prototype, "NetworkSentTime", {
460 | get: function () {
461 | return this.networkSentTime;
462 | },
463 | enumerable: true,
464 | configurable: true
465 | });
466 | Object.defineProperty(ConnectionMessageSentEvent.prototype, "Message", {
467 | get: function () {
468 | return this.message;
469 | },
470 | enumerable: true,
471 | configurable: true
472 | });
473 | return ConnectionMessageSentEvent;
474 | }(ConnectionEvent));
475 | exports.ConnectionMessageSentEvent = ConnectionMessageSentEvent;
476 | });
477 | define("src/common/ConnectionOpenResponse", ["require", "exports"], function (require, exports) {
478 | "use strict";
479 | Object.defineProperty(exports, "__esModule", { value: true });
480 | var ConnectionOpenResponse = (function () {
481 | function ConnectionOpenResponse(statusCode, reason) {
482 | this.statusCode = statusCode;
483 | this.reason = reason;
484 | }
485 | Object.defineProperty(ConnectionOpenResponse.prototype, "StatusCode", {
486 | get: function () {
487 | return this.statusCode;
488 | },
489 | enumerable: true,
490 | configurable: true
491 | });
492 | Object.defineProperty(ConnectionOpenResponse.prototype, "Reason", {
493 | get: function () {
494 | return this.reason;
495 | },
496 | enumerable: true,
497 | configurable: true
498 | });
499 | return ConnectionOpenResponse;
500 | }());
501 | exports.ConnectionOpenResponse = ConnectionOpenResponse;
502 | });
503 | define("src/common/IDetachable", ["require", "exports"], function (require, exports) {
504 | "use strict";
505 | Object.defineProperty(exports, "__esModule", { value: true });
506 | });
507 | define("src/common/IDisposable", ["require", "exports"], function (require, exports) {
508 | "use strict";
509 | Object.defineProperty(exports, "__esModule", { value: true });
510 | });
511 | define("src/common/IEventSource", ["require", "exports"], function (require, exports) {
512 | "use strict";
513 | Object.defineProperty(exports, "__esModule", { value: true });
514 | });
515 | define("src/common/EventSource", ["require", "exports", "src/common/Error", "src/common/Guid"], function (require, exports, Error_2, Guid_3) {
516 | "use strict";
517 | Object.defineProperty(exports, "__esModule", { value: true });
518 | var EventSource = (function () {
519 | function EventSource(metadata) {
520 | var _this = this;
521 | this.eventListeners = {};
522 | this.isDisposed = false;
523 | this.OnEvent = function (event) {
524 | if (_this.IsDisposed()) {
525 | throw (new Error_2.ObjectDisposedError("EventSource"));
526 | }
527 | if (_this.Metadata) {
528 | for (var paramName in _this.Metadata) {
529 | if (paramName) {
530 | if (event.Metadata) {
531 | if (!event.Metadata[paramName]) {
532 | event.Metadata[paramName] = _this.Metadata[paramName];
533 | }
534 | }
535 | }
536 | }
537 | }
538 | for (var eventId in _this.eventListeners) {
539 | if (eventId && _this.eventListeners[eventId]) {
540 | _this.eventListeners[eventId](event);
541 | }
542 | }
543 | };
544 | this.Attach = function (onEventCallback) {
545 | var id = Guid_3.CreateNoDashGuid();
546 | _this.eventListeners[id] = onEventCallback;
547 | return {
548 | Detach: function () {
549 | delete _this.eventListeners[id];
550 | },
551 | };
552 | };
553 | this.AttachListener = function (listener) {
554 | return _this.Attach(listener.OnEvent);
555 | };
556 | this.IsDisposed = function () {
557 | return _this.isDisposed;
558 | };
559 | this.Dispose = function () {
560 | _this.eventListeners = null;
561 | _this.isDisposed = true;
562 | };
563 | this.metadata = metadata;
564 | }
565 | Object.defineProperty(EventSource.prototype, "Metadata", {
566 | get: function () {
567 | return this.metadata;
568 | },
569 | enumerable: true,
570 | configurable: true
571 | });
572 | return EventSource;
573 | }());
574 | exports.EventSource = EventSource;
575 | });
576 | define("src/common/Events", ["require", "exports", "src/common/Error", "src/common/EventSource"], function (require, exports, Error_3, EventSource_1) {
577 | "use strict";
578 | Object.defineProperty(exports, "__esModule", { value: true });
579 | var Events = (function () {
580 | function Events() {
581 | }
582 | Object.defineProperty(Events, "Instance", {
583 | get: function () {
584 | return Events.instance;
585 | },
586 | enumerable: true,
587 | configurable: true
588 | });
589 | Events.instance = new EventSource_1.EventSource();
590 | Events.SetEventSource = function (eventSource) {
591 | if (!eventSource) {
592 | throw new Error_3.ArgumentNullError("eventSource");
593 | }
594 | Events.instance = eventSource;
595 | };
596 | return Events;
597 | }());
598 | exports.Events = Events;
599 | });
600 | define("src/common/Promise", ["require", "exports", "src/common/Error"], function (require, exports, Error_4) {
601 | "use strict";
602 | Object.defineProperty(exports, "__esModule", { value: true });
603 | var PromiseState;
604 | (function (PromiseState) {
605 | PromiseState[PromiseState["None"] = 0] = "None";
606 | PromiseState[PromiseState["Resolved"] = 1] = "Resolved";
607 | PromiseState[PromiseState["Rejected"] = 2] = "Rejected";
608 | })(PromiseState = exports.PromiseState || (exports.PromiseState = {}));
609 | var PromiseResult = (function () {
610 | function PromiseResult(promiseResultEventSource) {
611 | var _this = this;
612 | this.ThrowIfError = function () {
613 | if (_this.IsError) {
614 | throw _this.Error;
615 | }
616 | };
617 | promiseResultEventSource.On(function (result) {
618 | if (!_this.isCompleted) {
619 | _this.isCompleted = true;
620 | _this.isError = false;
621 | _this.result = result;
622 | }
623 | }, function (error) {
624 | if (!_this.isCompleted) {
625 | _this.isCompleted = true;
626 | _this.isError = true;
627 | _this.error = error;
628 | }
629 | });
630 | }
631 | Object.defineProperty(PromiseResult.prototype, "IsCompleted", {
632 | get: function () {
633 | return this.isCompleted;
634 | },
635 | enumerable: true,
636 | configurable: true
637 | });
638 | Object.defineProperty(PromiseResult.prototype, "IsError", {
639 | get: function () {
640 | return this.isError;
641 | },
642 | enumerable: true,
643 | configurable: true
644 | });
645 | Object.defineProperty(PromiseResult.prototype, "Error", {
646 | get: function () {
647 | return this.error;
648 | },
649 | enumerable: true,
650 | configurable: true
651 | });
652 | Object.defineProperty(PromiseResult.prototype, "Result", {
653 | get: function () {
654 | return this.result;
655 | },
656 | enumerable: true,
657 | configurable: true
658 | });
659 | return PromiseResult;
660 | }());
661 | exports.PromiseResult = PromiseResult;
662 | var PromiseResultEventSource = (function () {
663 | function PromiseResultEventSource() {
664 | var _this = this;
665 | this.SetResult = function (result) {
666 | _this.onSetResult(result);
667 | };
668 | this.SetError = function (error) {
669 | _this.onSetError(error);
670 | };
671 | this.On = function (onSetResult, onSetError) {
672 | _this.onSetResult = onSetResult;
673 | _this.onSetError = onSetError;
674 | };
675 | }
676 | return PromiseResultEventSource;
677 | }());
678 | exports.PromiseResultEventSource = PromiseResultEventSource;
679 | var PromiseHelper = (function () {
680 | function PromiseHelper() {
681 | }
682 | PromiseHelper.WhenAll = function (promises) {
683 | if (!promises || promises.length === 0) {
684 | throw new Error_4.ArgumentNullError("promises");
685 | }
686 | var deferred = new Deferred();
687 | var errors = [];
688 | var completedPromises = 0;
689 | var checkForCompletion = function () {
690 | completedPromises++;
691 | if (completedPromises === promises.length) {
692 | if (errors.length === 0) {
693 | deferred.Resolve(true);
694 | }
695 | else {
696 | deferred.Reject(errors.join(", "));
697 | }
698 | }
699 | };
700 | for (var _i = 0, promises_1 = promises; _i < promises_1.length; _i++) {
701 | var promise = promises_1[_i];
702 | promise.On(function (r) {
703 | checkForCompletion();
704 | }, function (e) {
705 | errors.push(e);
706 | checkForCompletion();
707 | });
708 | }
709 | return deferred.Promise();
710 | };
711 | PromiseHelper.FromResult = function (result) {
712 | var deferred = new Deferred();
713 | deferred.Resolve(result);
714 | return deferred.Promise();
715 | };
716 | PromiseHelper.FromError = function (error) {
717 | var deferred = new Deferred();
718 | deferred.Reject(error);
719 | return deferred.Promise();
720 | };
721 | return PromiseHelper;
722 | }());
723 | exports.PromiseHelper = PromiseHelper;
724 | var Promise = (function () {
725 | function Promise(sink) {
726 | var _this = this;
727 | this.Result = function () {
728 | return _this.sink.Result;
729 | };
730 | this.ContinueWith = function (continuationCallback) {
731 | if (!continuationCallback) {
732 | throw new Error_4.ArgumentNullError("continuationCallback");
733 | }
734 | var continuationDeferral = new Deferred();
735 | _this.sink.on(function (r) {
736 | try {
737 | var coninuationResult = continuationCallback(_this.sink.Result);
738 | continuationDeferral.Resolve(coninuationResult);
739 | }
740 | catch (e) {
741 | continuationDeferral.Reject("'Unhandled callback error: " + e + "'");
742 | }
743 | }, function (error) {
744 | try {
745 | var coninuationResult = continuationCallback(_this.sink.Result);
746 | continuationDeferral.Resolve(coninuationResult);
747 | }
748 | catch (e) {
749 | continuationDeferral.Reject("'Unhandled callback error: " + e + ". InnerError: " + error + "'");
750 | }
751 | });
752 | return continuationDeferral.Promise();
753 | };
754 | this.OnSuccessContinueWith = function (continuationCallback) {
755 | if (!continuationCallback) {
756 | throw new Error_4.ArgumentNullError("continuationCallback");
757 | }
758 | var continuationDeferral = new Deferred();
759 | _this.sink.on(function (r) {
760 | try {
761 | var coninuationResult = continuationCallback(r);
762 | continuationDeferral.Resolve(coninuationResult);
763 | }
764 | catch (e) {
765 | continuationDeferral.Reject("'Unhandled callback error: " + e + "'");
766 | }
767 | }, function (error) {
768 | continuationDeferral.Reject("'Unhandled callback error: " + error + "'");
769 | });
770 | return continuationDeferral.Promise();
771 | };
772 | this.ContinueWithPromise = function (continuationCallback) {
773 | if (!continuationCallback) {
774 | throw new Error_4.ArgumentNullError("continuationCallback");
775 | }
776 | var continuationDeferral = new Deferred();
777 | _this.sink.on(function (r) {
778 | try {
779 | var continuationPromise = continuationCallback(_this.sink.Result);
780 | if (!continuationPromise) {
781 | throw new Error("'Contuniation callback did not return promise'");
782 | }
783 | continuationPromise.On(function (coninuationResult) {
784 | continuationDeferral.Resolve(coninuationResult);
785 | }, function (e) {
786 | continuationDeferral.Reject(e);
787 | });
788 | }
789 | catch (e) {
790 | continuationDeferral.Reject("'Unhandled callback error: " + e + "'");
791 | }
792 | }, function (error) {
793 | try {
794 | var continuationPromise = continuationCallback(_this.sink.Result);
795 | if (!continuationPromise) {
796 | throw new Error("Contuniation callback did not return promise");
797 | }
798 | continuationPromise.On(function (coninuationResult) {
799 | continuationDeferral.Resolve(coninuationResult);
800 | }, function (e) {
801 | continuationDeferral.Reject(e);
802 | });
803 | }
804 | catch (e) {
805 | continuationDeferral.Reject("'Unhandled callback error: " + e + ". InnerError: " + error + "'");
806 | }
807 | });
808 | return continuationDeferral.Promise();
809 | };
810 | this.OnSuccessContinueWithPromise = function (continuationCallback) {
811 | if (!continuationCallback) {
812 | throw new Error_4.ArgumentNullError("continuationCallback");
813 | }
814 | var continuationDeferral = new Deferred();
815 | _this.sink.on(function (r) {
816 | try {
817 | var continuationPromise = continuationCallback(r);
818 | if (!continuationPromise) {
819 | throw new Error("Contuniation callback did not return promise");
820 | }
821 | continuationPromise.On(function (coninuationResult) {
822 | continuationDeferral.Resolve(coninuationResult);
823 | }, function (e) {
824 | continuationDeferral.Reject(e);
825 | });
826 | }
827 | catch (e) {
828 | continuationDeferral.Reject("'Unhandled callback error: " + e + "'");
829 | }
830 | }, function (error) {
831 | continuationDeferral.Reject("'Unhandled callback error: " + error + ".'");
832 | });
833 | return continuationDeferral.Promise();
834 | };
835 | this.On = function (successCallback, errorCallback) {
836 | if (!successCallback) {
837 | throw new Error_4.ArgumentNullError("successCallback");
838 | }
839 | if (!errorCallback) {
840 | throw new Error_4.ArgumentNullError("errorCallback");
841 | }
842 | _this.sink.on(successCallback, errorCallback);
843 | return _this;
844 | };
845 | this.Finally = function (callback) {
846 | if (!callback) {
847 | throw new Error_4.ArgumentNullError("callback");
848 | }
849 | var callbackWrapper = function (_) {
850 | callback();
851 | };
852 | return _this.On(callbackWrapper, callbackWrapper);
853 | };
854 | this.sink = sink;
855 | }
856 | return Promise;
857 | }());
858 | exports.Promise = Promise;
859 | var Deferred = (function () {
860 | function Deferred() {
861 | var _this = this;
862 | this.State = function () {
863 | return _this.sink.State;
864 | };
865 | this.Promise = function () {
866 | return _this.promise;
867 | };
868 | this.Resolve = function (result) {
869 | _this.sink.Resolve(result);
870 | return _this;
871 | };
872 | this.Reject = function (error) {
873 | _this.sink.Reject(error);
874 | return _this;
875 | };
876 | this.sink = new Sink();
877 | this.promise = new Promise(this.sink);
878 | }
879 | return Deferred;
880 | }());
881 | exports.Deferred = Deferred;
882 | var Sink = (function () {
883 | function Sink() {
884 | var _this = this;
885 | this.state = PromiseState.None;
886 | this.promiseResult = null;
887 | this.promiseResultEvents = null;
888 | this.successHandlers = [];
889 | this.errorHandlers = [];
890 | this.Resolve = function (result) {
891 | if (_this.state !== PromiseState.None) {
892 | throw new Error("'Cannot resolve a completed promise'");
893 | }
894 | _this.state = PromiseState.Resolved;
895 | _this.promiseResultEvents.SetResult(result);
896 | for (var i = 0; i < _this.successHandlers.length; i++) {
897 | _this.ExecuteSuccessCallback(result, _this.successHandlers[i], _this.errorHandlers[i]);
898 | }
899 | _this.DetachHandlers();
900 | };
901 | this.Reject = function (error) {
902 | if (_this.state !== PromiseState.None) {
903 | throw new Error("'Cannot reject a completed promise'");
904 | }
905 | _this.state = PromiseState.Rejected;
906 | _this.promiseResultEvents.SetError(error);
907 | for (var _i = 0, _a = _this.errorHandlers; _i < _a.length; _i++) {
908 | var errorHandler = _a[_i];
909 | _this.ExecuteErrorCallback(error, errorHandler);
910 | }
911 | _this.DetachHandlers();
912 | };
913 | this.on = function (successCallback, errorCallback) {
914 | if (successCallback == null) {
915 | successCallback = function (r) { return; };
916 | }
917 | if (_this.state === PromiseState.None) {
918 | _this.successHandlers.push(successCallback);
919 | _this.errorHandlers.push(errorCallback);
920 | }
921 | else {
922 | if (_this.state === PromiseState.Resolved) {
923 | _this.ExecuteSuccessCallback(_this.promiseResult.Result, successCallback, errorCallback);
924 | }
925 | else if (_this.state === PromiseState.Rejected) {
926 | _this.ExecuteErrorCallback(_this.promiseResult.Error, errorCallback);
927 | }
928 | _this.DetachHandlers();
929 | }
930 | };
931 | this.ExecuteSuccessCallback = function (result, successCallback, errorCallback) {
932 | try {
933 | successCallback(result);
934 | }
935 | catch (e) {
936 | _this.ExecuteErrorCallback("'Unhandled callback error: " + e + "'", errorCallback);
937 | }
938 | };
939 | this.ExecuteErrorCallback = function (error, errorCallback) {
940 | if (errorCallback) {
941 | try {
942 | errorCallback(error);
943 | }
944 | catch (e) {
945 | throw new Error("'Unhandled callback error: " + e + ". InnerError: " + error + "'");
946 | }
947 | }
948 | else {
949 | throw new Error("'Unhandled error: " + error + "'");
950 | }
951 | };
952 | this.DetachHandlers = function () {
953 | _this.errorHandlers = [];
954 | _this.successHandlers = [];
955 | };
956 | this.promiseResultEvents = new PromiseResultEventSource();
957 | this.promiseResult = new PromiseResult(this.promiseResultEvents);
958 | }
959 | Object.defineProperty(Sink.prototype, "State", {
960 | get: function () {
961 | return this.state;
962 | },
963 | enumerable: true,
964 | configurable: true
965 | });
966 | Object.defineProperty(Sink.prototype, "Result", {
967 | get: function () {
968 | return this.promiseResult;
969 | },
970 | enumerable: true,
971 | configurable: true
972 | });
973 | return Sink;
974 | }());
975 | exports.Sink = Sink;
976 | });
977 | define("src/common/List", ["require", "exports", "src/common/Error"], function (require, exports, Error_5) {
978 | "use strict";
979 | Object.defineProperty(exports, "__esModule", { value: true });
980 | var List = (function () {
981 | function List(list) {
982 | var _this = this;
983 | this.subscriptionIdCounter = 0;
984 | this.addSubscriptions = {};
985 | this.removeSubscriptions = {};
986 | this.disposedSubscriptions = {};
987 | this.disposeReason = null;
988 | this.Get = function (itemIndex) {
989 | _this.ThrowIfDisposed();
990 | return _this.list[itemIndex];
991 | };
992 | this.First = function () {
993 | return _this.Get(0);
994 | };
995 | this.Last = function () {
996 | return _this.Get(_this.Length() - 1);
997 | };
998 | this.Add = function (item) {
999 | _this.ThrowIfDisposed();
1000 | _this.InsertAt(_this.list.length, item);
1001 | };
1002 | this.InsertAt = function (index, item) {
1003 | _this.ThrowIfDisposed();
1004 | if (index === 0) {
1005 | _this.list.unshift(item);
1006 | }
1007 | else if (index === _this.list.length) {
1008 | _this.list.push(item);
1009 | }
1010 | else {
1011 | _this.list.splice(index, 0, item);
1012 | }
1013 | _this.TriggerSubscriptions(_this.addSubscriptions);
1014 | };
1015 | this.RemoveFirst = function () {
1016 | _this.ThrowIfDisposed();
1017 | return _this.RemoveAt(0);
1018 | };
1019 | this.RemoveLast = function () {
1020 | _this.ThrowIfDisposed();
1021 | return _this.RemoveAt(_this.Length() - 1);
1022 | };
1023 | this.RemoveAt = function (index) {
1024 | _this.ThrowIfDisposed();
1025 | return _this.Remove(index, 1)[0];
1026 | };
1027 | this.Remove = function (index, count) {
1028 | _this.ThrowIfDisposed();
1029 | var removedElements = _this.list.splice(index, count);
1030 | _this.TriggerSubscriptions(_this.removeSubscriptions);
1031 | return removedElements;
1032 | };
1033 | this.Clear = function () {
1034 | _this.ThrowIfDisposed();
1035 | _this.Remove(0, _this.Length());
1036 | };
1037 | this.Length = function () {
1038 | _this.ThrowIfDisposed();
1039 | return _this.list.length;
1040 | };
1041 | this.OnAdded = function (addedCallback) {
1042 | _this.ThrowIfDisposed();
1043 | var subscriptionId = _this.subscriptionIdCounter++;
1044 | _this.addSubscriptions[subscriptionId] = addedCallback;
1045 | return {
1046 | Detach: function () {
1047 | delete _this.addSubscriptions[subscriptionId];
1048 | },
1049 | };
1050 | };
1051 | this.OnRemoved = function (removedCallback) {
1052 | _this.ThrowIfDisposed();
1053 | var subscriptionId = _this.subscriptionIdCounter++;
1054 | _this.removeSubscriptions[subscriptionId] = removedCallback;
1055 | return {
1056 | Detach: function () {
1057 | delete _this.removeSubscriptions[subscriptionId];
1058 | },
1059 | };
1060 | };
1061 | this.OnDisposed = function (disposedCallback) {
1062 | _this.ThrowIfDisposed();
1063 | var subscriptionId = _this.subscriptionIdCounter++;
1064 | _this.disposedSubscriptions[subscriptionId] = disposedCallback;
1065 | return {
1066 | Detach: function () {
1067 | delete _this.disposedSubscriptions[subscriptionId];
1068 | },
1069 | };
1070 | };
1071 | this.Join = function (seperator) {
1072 | _this.ThrowIfDisposed();
1073 | return _this.list.join(seperator);
1074 | };
1075 | this.ToArray = function () {
1076 | var cloneCopy = Array();
1077 | _this.list.forEach(function (val) {
1078 | cloneCopy.push(val);
1079 | });
1080 | return cloneCopy;
1081 | };
1082 | this.Any = function (callback) {
1083 | _this.ThrowIfDisposed();
1084 | if (callback) {
1085 | return _this.Where(callback).Length() > 0;
1086 | }
1087 | else {
1088 | return _this.Length() > 0;
1089 | }
1090 | };
1091 | this.All = function (callback) {
1092 | _this.ThrowIfDisposed();
1093 | return _this.Where(callback).Length() === _this.Length();
1094 | };
1095 | this.ForEach = function (callback) {
1096 | _this.ThrowIfDisposed();
1097 | for (var i = 0; i < _this.Length(); i++) {
1098 | callback(_this.list[i], i);
1099 | }
1100 | };
1101 | this.Select = function (callback) {
1102 | _this.ThrowIfDisposed();
1103 | var selectList = [];
1104 | for (var i = 0; i < _this.list.length; i++) {
1105 | selectList.push(callback(_this.list[i], i));
1106 | }
1107 | return new List(selectList);
1108 | };
1109 | this.Where = function (callback) {
1110 | _this.ThrowIfDisposed();
1111 | var filteredList = new List();
1112 | for (var i = 0; i < _this.list.length; i++) {
1113 | if (callback(_this.list[i], i)) {
1114 | filteredList.Add(_this.list[i]);
1115 | }
1116 | }
1117 | return filteredList;
1118 | };
1119 | this.OrderBy = function (compareFn) {
1120 | _this.ThrowIfDisposed();
1121 | var clonedArray = _this.ToArray();
1122 | var orderedArray = clonedArray.sort(compareFn);
1123 | return new List(orderedArray);
1124 | };
1125 | this.OrderByDesc = function (compareFn) {
1126 | _this.ThrowIfDisposed();
1127 | return _this.OrderBy(function (a, b) { return compareFn(b, a); });
1128 | };
1129 | this.Clone = function () {
1130 | _this.ThrowIfDisposed();
1131 | return new List(_this.ToArray());
1132 | };
1133 | this.Concat = function (list) {
1134 | _this.ThrowIfDisposed();
1135 | return new List(_this.list.concat(list.ToArray()));
1136 | };
1137 | this.ConcatArray = function (array) {
1138 | _this.ThrowIfDisposed();
1139 | return new List(_this.list.concat(array));
1140 | };
1141 | this.IsDisposed = function () {
1142 | return _this.list == null;
1143 | };
1144 | this.Dispose = function (reason) {
1145 | if (!_this.IsDisposed()) {
1146 | _this.disposeReason = reason;
1147 | _this.list = null;
1148 | _this.addSubscriptions = null;
1149 | _this.removeSubscriptions = null;
1150 | _this.TriggerSubscriptions(_this.disposedSubscriptions);
1151 | }
1152 | };
1153 | this.ThrowIfDisposed = function () {
1154 | if (_this.IsDisposed()) {
1155 | throw new Error_5.ObjectDisposedError("List", _this.disposeReason);
1156 | }
1157 | };
1158 | this.TriggerSubscriptions = function (subscriptions) {
1159 | if (subscriptions) {
1160 | for (var subscriptionId in subscriptions) {
1161 | if (subscriptionId) {
1162 | subscriptions[subscriptionId]();
1163 | }
1164 | }
1165 | }
1166 | };
1167 | this.list = [];
1168 | if (list) {
1169 | for (var _i = 0, list_1 = list; _i < list_1.length; _i++) {
1170 | var item = list_1[_i];
1171 | this.list.push(item);
1172 | }
1173 | }
1174 | }
1175 | return List;
1176 | }());
1177 | exports.List = List;
1178 | });
1179 | define("src/common/Queue", ["require", "exports", "src/common/Error", "src/common/List", "src/common/Promise"], function (require, exports, Error_6, List_1, Promise_1) {
1180 | "use strict";
1181 | Object.defineProperty(exports, "__esModule", { value: true });
1182 | var SubscriberType;
1183 | (function (SubscriberType) {
1184 | SubscriberType[SubscriberType["Dequeue"] = 0] = "Dequeue";
1185 | SubscriberType[SubscriberType["Peek"] = 1] = "Peek";
1186 | })(SubscriberType || (SubscriberType = {}));
1187 | var Queue = (function () {
1188 | function Queue(list) {
1189 | var _this = this;
1190 | this.promiseStore = new List_1.List();
1191 | this.isDrainInProgress = false;
1192 | this.isDisposing = false;
1193 | this.disposeReason = null;
1194 | this.Enqueue = function (item) {
1195 | _this.ThrowIfDispose();
1196 | _this.EnqueueFromPromise(Promise_1.PromiseHelper.FromResult(item));
1197 | };
1198 | this.EnqueueFromPromise = function (promise) {
1199 | _this.ThrowIfDispose();
1200 | _this.promiseStore.Add(promise);
1201 | promise.Finally(function () {
1202 | while (_this.promiseStore.Length() > 0) {
1203 | if (!_this.promiseStore.First().Result().IsCompleted) {
1204 | break;
1205 | }
1206 | else {
1207 | var p = _this.promiseStore.RemoveFirst();
1208 | if (!p.Result().IsError) {
1209 | _this.list.Add(p.Result().Result);
1210 | }
1211 | else {
1212 | }
1213 | }
1214 | }
1215 | });
1216 | };
1217 | this.Dequeue = function () {
1218 | _this.ThrowIfDispose();
1219 | var deferredSubscriber = new Promise_1.Deferred();
1220 | _this.subscribers.Add({ deferral: deferredSubscriber, type: SubscriberType.Dequeue });
1221 | _this.Drain();
1222 | return deferredSubscriber.Promise();
1223 | };
1224 | this.Peek = function () {
1225 | _this.ThrowIfDispose();
1226 | var deferredSubscriber = new Promise_1.Deferred();
1227 | _this.subscribers.Add({ deferral: deferredSubscriber, type: SubscriberType.Peek });
1228 | _this.Drain();
1229 | return deferredSubscriber.Promise();
1230 | };
1231 | this.Length = function () {
1232 | _this.ThrowIfDispose();
1233 | return _this.list.Length();
1234 | };
1235 | this.IsDisposed = function () {
1236 | return _this.subscribers == null;
1237 | };
1238 | this.DrainAndDispose = function (pendingItemProcessor, reason) {
1239 | if (!_this.IsDisposed() && !_this.isDisposing) {
1240 | _this.disposeReason = reason;
1241 | _this.isDisposing = true;
1242 | while (_this.subscribers.Length() > 0) {
1243 | var subscriber = _this.subscribers.RemoveFirst();
1244 | subscriber.deferral.Reject("Disposed");
1245 | }
1246 | for (var _i = 0, _a = _this.detachables; _i < _a.length; _i++) {
1247 | var detachable = _a[_i];
1248 | detachable.Detach();
1249 | }
1250 | if (_this.promiseStore.Length() > 0 && pendingItemProcessor) {
1251 | return Promise_1.PromiseHelper
1252 | .WhenAll(_this.promiseStore.ToArray())
1253 | .ContinueWith(function () {
1254 | _this.subscribers = null;
1255 | _this.list.ForEach(function (item, index) {
1256 | pendingItemProcessor(item);
1257 | });
1258 | _this.list = null;
1259 | return true;
1260 | });
1261 | }
1262 | else {
1263 | _this.subscribers = null;
1264 | _this.list = null;
1265 | }
1266 | }
1267 | return Promise_1.PromiseHelper.FromResult(true);
1268 | };
1269 | this.Dispose = function (reason) {
1270 | _this.DrainAndDispose(null, reason);
1271 | };
1272 | this.Drain = function () {
1273 | if (!_this.isDrainInProgress && !_this.isDisposing) {
1274 | _this.isDrainInProgress = true;
1275 | while (_this.list.Length() > 0 && _this.subscribers.Length() > 0 && !_this.isDisposing) {
1276 | var subscriber = _this.subscribers.RemoveFirst();
1277 | if (subscriber.type === SubscriberType.Peek) {
1278 | subscriber.deferral.Resolve(_this.list.First());
1279 | }
1280 | else {
1281 | var dequeuedItem = _this.list.RemoveFirst();
1282 | subscriber.deferral.Resolve(dequeuedItem);
1283 | }
1284 | }
1285 | _this.isDrainInProgress = false;
1286 | }
1287 | };
1288 | this.ThrowIfDispose = function () {
1289 | if (_this.IsDisposed()) {
1290 | if (_this.disposeReason) {
1291 | throw new Error_6.InvalidOperationError(_this.disposeReason);
1292 | }
1293 | throw new Error_6.ObjectDisposedError("Queue");
1294 | }
1295 | else if (_this.isDisposing) {
1296 | throw new Error_6.InvalidOperationError("Queue disposing");
1297 | }
1298 | };
1299 | this.list = list ? list : new List_1.List();
1300 | this.detachables = [];
1301 | this.subscribers = new List_1.List();
1302 | this.detachables.push(this.list.OnAdded(this.Drain));
1303 | }
1304 | return Queue;
1305 | }());
1306 | exports.Queue = Queue;
1307 | });
1308 | define("src/common/Stream", ["require", "exports", "src/common/Error", "src/common/Guid", "src/common/Queue"], function (require, exports, Error_7, Guid_4, Queue_1) {
1309 | "use strict";
1310 | Object.defineProperty(exports, "__esModule", { value: true });
1311 | var Stream = (function () {
1312 | function Stream(streamId) {
1313 | var _this = this;
1314 | this.readerIdCounter = 1;
1315 | this.isEnded = false;
1316 | this.Write = function (buffer) {
1317 | _this.ThrowIfClosed();
1318 | _this.WriteStreamChunk({
1319 | Buffer: buffer,
1320 | IsEnd: false,
1321 | });
1322 | };
1323 | this.GetReader = function () {
1324 | var readerId = _this.readerIdCounter;
1325 | _this.readerIdCounter++;
1326 | var readerQueue = new Queue_1.Queue();
1327 | var currentLength = _this.streambuffer.length;
1328 | _this.readerQueues[readerId] = readerQueue;
1329 | for (var i = 0; i < currentLength; i++) {
1330 | readerQueue.Enqueue(_this.streambuffer[i]);
1331 | }
1332 | return new StreamReader(_this.id, readerQueue, function () {
1333 | delete _this.readerQueues[readerId];
1334 | });
1335 | };
1336 | this.Close = function () {
1337 | if (!_this.isEnded) {
1338 | _this.WriteStreamChunk({
1339 | Buffer: null,
1340 | IsEnd: true,
1341 | });
1342 | _this.isEnded = true;
1343 | }
1344 | };
1345 | this.WriteStreamChunk = function (streamChunk) {
1346 | _this.ThrowIfClosed();
1347 | _this.streambuffer.push(streamChunk);
1348 | for (var readerId in _this.readerQueues) {
1349 | if (!_this.readerQueues[readerId].IsDisposed()) {
1350 | try {
1351 | _this.readerQueues[readerId].Enqueue(streamChunk);
1352 | }
1353 | catch (e) {
1354 | }
1355 | }
1356 | }
1357 | };
1358 | this.ThrowIfClosed = function () {
1359 | if (_this.isEnded) {
1360 | throw new Error_7.InvalidOperationError("Stream closed");
1361 | }
1362 | };
1363 | this.id = streamId ? streamId : Guid_4.CreateNoDashGuid();
1364 | this.streambuffer = [];
1365 | this.readerQueues = {};
1366 | }
1367 | Object.defineProperty(Stream.prototype, "IsClosed", {
1368 | get: function () {
1369 | return this.isEnded;
1370 | },
1371 | enumerable: true,
1372 | configurable: true
1373 | });
1374 | Object.defineProperty(Stream.prototype, "Id", {
1375 | get: function () {
1376 | return this.id;
1377 | },
1378 | enumerable: true,
1379 | configurable: true
1380 | });
1381 | return Stream;
1382 | }());
1383 | exports.Stream = Stream;
1384 | var StreamReader = (function () {
1385 | function StreamReader(streamId, readerQueue, onClose) {
1386 | var _this = this;
1387 | this.isClosed = false;
1388 | this.Read = function () {
1389 | if (_this.IsClosed) {
1390 | throw new Error_7.InvalidOperationError("StreamReader closed");
1391 | }
1392 | return _this.readerQueue
1393 | .Dequeue()
1394 | .OnSuccessContinueWith(function (streamChunk) {
1395 | if (streamChunk.IsEnd) {
1396 | _this.readerQueue.Dispose("End of stream reached");
1397 | }
1398 | return streamChunk;
1399 | });
1400 | };
1401 | this.Close = function () {
1402 | if (!_this.isClosed) {
1403 | _this.isClosed = true;
1404 | _this.readerQueue.Dispose("StreamReader closed");
1405 | _this.onClose();
1406 | }
1407 | };
1408 | this.readerQueue = readerQueue;
1409 | this.onClose = onClose;
1410 | this.streamId = streamId;
1411 | }
1412 | Object.defineProperty(StreamReader.prototype, "IsClosed", {
1413 | get: function () {
1414 | return this.isClosed;
1415 | },
1416 | enumerable: true,
1417 | configurable: true
1418 | });
1419 | Object.defineProperty(StreamReader.prototype, "StreamId", {
1420 | get: function () {
1421 | return this.streamId;
1422 | },
1423 | enumerable: true,
1424 | configurable: true
1425 | });
1426 | return StreamReader;
1427 | }());
1428 | exports.StreamReader = StreamReader;
1429 | });
1430 | define("src/common/IAudioSource", ["require", "exports"], function (require, exports) {
1431 | "use strict";
1432 | Object.defineProperty(exports, "__esModule", { value: true });
1433 | });
1434 | define("src/common/IConnection", ["require", "exports"], function (require, exports) {
1435 | "use strict";
1436 | Object.defineProperty(exports, "__esModule", { value: true });
1437 | var ConnectionState;
1438 | (function (ConnectionState) {
1439 | ConnectionState[ConnectionState["None"] = 0] = "None";
1440 | ConnectionState[ConnectionState["Connected"] = 1] = "Connected";
1441 | ConnectionState[ConnectionState["Connecting"] = 2] = "Connecting";
1442 | ConnectionState[ConnectionState["Disconnected"] = 3] = "Disconnected";
1443 | })(ConnectionState = exports.ConnectionState || (exports.ConnectionState = {}));
1444 | });
1445 | define("src/common/IKeyValueStorage", ["require", "exports"], function (require, exports) {
1446 | "use strict";
1447 | Object.defineProperty(exports, "__esModule", { value: true });
1448 | });
1449 | define("src/common/InMemoryStorage", ["require", "exports", "src/common/Error"], function (require, exports, Error_8) {
1450 | "use strict";
1451 | Object.defineProperty(exports, "__esModule", { value: true });
1452 | var InMemoryStorage = (function () {
1453 | function InMemoryStorage() {
1454 | var _this = this;
1455 | this.store = {};
1456 | this.Get = function (key) {
1457 | if (!key) {
1458 | throw new Error_8.ArgumentNullError("key");
1459 | }
1460 | return _this.store[key];
1461 | };
1462 | this.GetOrAdd = function (key, valueToAdd) {
1463 | if (!key) {
1464 | throw new Error_8.ArgumentNullError("key");
1465 | }
1466 | if (_this.store[key] === undefined) {
1467 | _this.store[key] = valueToAdd;
1468 | }
1469 | return _this.store[key];
1470 | };
1471 | this.Set = function (key, value) {
1472 | if (!key) {
1473 | throw new Error_8.ArgumentNullError("key");
1474 | }
1475 | _this.store[key] = value;
1476 | };
1477 | this.Remove = function (key) {
1478 | if (!key) {
1479 | throw new Error_8.ArgumentNullError("key");
1480 | }
1481 | if (_this.store[key] !== undefined) {
1482 | delete _this.store[key];
1483 | }
1484 | };
1485 | }
1486 | return InMemoryStorage;
1487 | }());
1488 | exports.InMemoryStorage = InMemoryStorage;
1489 | });
1490 | define("src/common/ITimer", ["require", "exports"], function (require, exports) {
1491 | "use strict";
1492 | Object.defineProperty(exports, "__esModule", { value: true });
1493 | });
1494 | define("src/common/RawWebsocketMessage", ["require", "exports", "src/common/ConnectionMessage", "src/common/Error", "src/common/Guid"], function (require, exports, ConnectionMessage_1, Error_9, Guid_5) {
1495 | "use strict";
1496 | Object.defineProperty(exports, "__esModule", { value: true });
1497 | var RawWebsocketMessage = (function () {
1498 | function RawWebsocketMessage(messageType, payload, id) {
1499 | this.payload = null;
1500 | if (!payload) {
1501 | throw new Error_9.ArgumentNullError("payload");
1502 | }
1503 | if (messageType === ConnectionMessage_1.MessageType.Binary && !(payload instanceof ArrayBuffer)) {
1504 | throw new Error_9.InvalidOperationError("Payload must be ArrayBuffer");
1505 | }
1506 | if (messageType === ConnectionMessage_1.MessageType.Text && !(typeof (payload) === "string")) {
1507 | throw new Error_9.InvalidOperationError("Payload must be a string");
1508 | }
1509 | this.messageType = messageType;
1510 | this.payload = payload;
1511 | this.id = id ? id : Guid_5.CreateNoDashGuid();
1512 | }
1513 | Object.defineProperty(RawWebsocketMessage.prototype, "MessageType", {
1514 | get: function () {
1515 | return this.messageType;
1516 | },
1517 | enumerable: true,
1518 | configurable: true
1519 | });
1520 | Object.defineProperty(RawWebsocketMessage.prototype, "Payload", {
1521 | get: function () {
1522 | return this.payload;
1523 | },
1524 | enumerable: true,
1525 | configurable: true
1526 | });
1527 | Object.defineProperty(RawWebsocketMessage.prototype, "TextContent", {
1528 | get: function () {
1529 | if (this.messageType === ConnectionMessage_1.MessageType.Binary) {
1530 | throw new Error_9.InvalidOperationError("Not supported for binary message");
1531 | }
1532 | return this.payload;
1533 | },
1534 | enumerable: true,
1535 | configurable: true
1536 | });
1537 | Object.defineProperty(RawWebsocketMessage.prototype, "BinaryContent", {
1538 | get: function () {
1539 | if (this.messageType === ConnectionMessage_1.MessageType.Text) {
1540 | throw new Error_9.InvalidOperationError("Not supported for text message");
1541 | }
1542 | return this.payload;
1543 | },
1544 | enumerable: true,
1545 | configurable: true
1546 | });
1547 | Object.defineProperty(RawWebsocketMessage.prototype, "Id", {
1548 | get: function () {
1549 | return this.id;
1550 | },
1551 | enumerable: true,
1552 | configurable: true
1553 | });
1554 | return RawWebsocketMessage;
1555 | }());
1556 | exports.RawWebsocketMessage = RawWebsocketMessage;
1557 | });
1558 | define("src/common/IWebsocketMessageFormatter", ["require", "exports"], function (require, exports) {
1559 | "use strict";
1560 | Object.defineProperty(exports, "__esModule", { value: true });
1561 | });
1562 | define("src/common/RiffPcmEncoder", ["require", "exports"], function (require, exports) {
1563 | "use strict";
1564 | Object.defineProperty(exports, "__esModule", { value: true });
1565 | var RiffPcmEncoder = (function () {
1566 | function RiffPcmEncoder(actualSampleRate, desiredSampleRate) {
1567 | var _this = this;
1568 | this.channelCount = 1;
1569 | this.Encode = function (isFirstAudioFrame, actualAudioFrame) {
1570 | var audioFrame = _this.DownSampleAudioFrame(actualAudioFrame, _this.actualSampleRate, _this.desiredSampleRate);
1571 | var audioLength = audioFrame.length * 2;
1572 | if (!isFirstAudioFrame) {
1573 | var buffer_1 = new ArrayBuffer(audioLength);
1574 | var view_1 = new DataView(buffer_1);
1575 | _this.FloatTo16BitPCM(view_1, 0, audioFrame);
1576 | return buffer_1;
1577 | }
1578 | var buffer = new ArrayBuffer(44 + audioLength);
1579 | var bitsPerSample = 16;
1580 | var bytesPerSample = bitsPerSample / 8;
1581 | var fileLength = 0;
1582 | var view = new DataView(buffer);
1583 | _this.SetString(view, 0, "RIFF");
1584 | view.setUint32(4, fileLength, true);
1585 | _this.SetString(view, 8, "WAVEfmt ");
1586 | view.setUint32(16, 16, true);
1587 | view.setUint16(20, 1, true);
1588 | view.setUint16(22, _this.channelCount, true);
1589 | view.setUint32(24, _this.desiredSampleRate, true);
1590 | view.setUint32(28, _this.desiredSampleRate * _this.channelCount * bytesPerSample, true);
1591 | view.setUint16(32, _this.channelCount * bytesPerSample, true);
1592 | view.setUint16(34, bitsPerSample, true);
1593 | _this.SetString(view, 36, "data");
1594 | view.setUint32(40, fileLength, true);
1595 | _this.FloatTo16BitPCM(view, 44, audioFrame);
1596 | return buffer;
1597 | };
1598 | this.SetString = function (view, offset, str) {
1599 | for (var i = 0; i < str.length; i++) {
1600 | view.setUint8(offset + i, str.charCodeAt(i));
1601 | }
1602 | };
1603 | this.FloatTo16BitPCM = function (view, offset, input) {
1604 | for (var i = 0; i < input.length; i++, offset += 2) {
1605 | var s = Math.max(-1, Math.min(1, input[i]));
1606 | view.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true);
1607 | }
1608 | };
1609 | this.DownSampleAudioFrame = function (audioFrame, actualSampleRate, desiredSamplerate) {
1610 | if (desiredSamplerate === actualSampleRate || desiredSamplerate > actualSampleRate) {
1611 | return audioFrame;
1612 | }
1613 | var sampleRateRatio = actualSampleRate / desiredSamplerate;
1614 | var newLength = Math.round(audioFrame.length / sampleRateRatio);
1615 | var downSampledAudioFrame = new Float32Array(newLength);
1616 | var offsetResult = 0;
1617 | var offsetBuffer = 0;
1618 | while (offsetResult < downSampledAudioFrame.length) {
1619 | var nextOffsetBuffer = Math.round((offsetResult + 1) * sampleRateRatio);
1620 | var accum = 0;
1621 | var count = 0;
1622 | for (var i = offsetBuffer; i < nextOffsetBuffer && i < audioFrame.length; i++) {
1623 | accum += audioFrame[i];
1624 | count++;
1625 | }
1626 | downSampledAudioFrame[offsetResult] = accum / count;
1627 | offsetResult++;
1628 | offsetBuffer = nextOffsetBuffer;
1629 | }
1630 | return downSampledAudioFrame;
1631 | };
1632 | this.actualSampleRate = actualSampleRate;
1633 | this.desiredSampleRate = desiredSampleRate;
1634 | }
1635 | return RiffPcmEncoder;
1636 | }());
1637 | exports.RiffPcmEncoder = RiffPcmEncoder;
1638 | });
1639 | define("src/common/Storage", ["require", "exports", "src/common/Error", "src/common/InMemoryStorage"], function (require, exports, Error_10, InMemoryStorage_1) {
1640 | "use strict";
1641 | Object.defineProperty(exports, "__esModule", { value: true });
1642 | var Storage = (function () {
1643 | function Storage() {
1644 | }
1645 | Object.defineProperty(Storage, "Session", {
1646 | get: function () {
1647 | return Storage.sessionStorage;
1648 | },
1649 | enumerable: true,
1650 | configurable: true
1651 | });
1652 | Object.defineProperty(Storage, "Local", {
1653 | get: function () {
1654 | return Storage.localStorage;
1655 | },
1656 | enumerable: true,
1657 | configurable: true
1658 | });
1659 | Storage.sessionStorage = new InMemoryStorage_1.InMemoryStorage();
1660 | Storage.localStorage = new InMemoryStorage_1.InMemoryStorage();
1661 | Storage.SetSessionStorage = function (sessionStorage) {
1662 | if (!sessionStorage) {
1663 | throw new Error_10.ArgumentNullError("sessionStorage");
1664 | }
1665 | Storage.sessionStorage = sessionStorage;
1666 | };
1667 | Storage.SetLocalStorage = function (localStorage) {
1668 | if (!localStorage) {
1669 | throw new Error_10.ArgumentNullError("localStorage");
1670 | }
1671 | Storage.localStorage = localStorage;
1672 | };
1673 | return Storage;
1674 | }());
1675 | exports.Storage = Storage;
1676 | });
1677 | define("src/common/Exports", ["require", "exports", "src/common/AudioSourceEvents", "src/common/ConnectionEvents", "src/common/ConnectionMessage", "src/common/ConnectionOpenResponse", "src/common/Error", "src/common/Events", "src/common/EventSource", "src/common/Guid", "src/common/IConnection", "src/common/InMemoryStorage", "src/common/List", "src/common/PlatformEvent", "src/common/Promise", "src/common/Queue", "src/common/RawWebsocketMessage", "src/common/RiffPcmEncoder", "src/common/Storage", "src/common/Stream"], function (require, exports, AudioSourceEvents_1, ConnectionEvents_1, ConnectionMessage_2, ConnectionOpenResponse_1, Error_11, Events_1, EventSource_2, Guid_6, IConnection_1, InMemoryStorage_2, List_2, PlatformEvent_3, Promise_2, Queue_2, RawWebsocketMessage_1, RiffPcmEncoder_1, Storage_1, Stream_1) {
1678 | "use strict";
1679 | function __export(m) {
1680 | for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
1681 | }
1682 | Object.defineProperty(exports, "__esModule", { value: true });
1683 | __export(AudioSourceEvents_1);
1684 | __export(ConnectionEvents_1);
1685 | __export(ConnectionMessage_2);
1686 | __export(ConnectionOpenResponse_1);
1687 | __export(Error_11);
1688 | __export(Events_1);
1689 | __export(EventSource_2);
1690 | __export(Guid_6);
1691 | __export(IConnection_1);
1692 | __export(InMemoryStorage_2);
1693 | __export(List_2);
1694 | __export(PlatformEvent_3);
1695 | __export(Promise_2);
1696 | __export(Queue_2);
1697 | __export(RawWebsocketMessage_1);
1698 | __export(RiffPcmEncoder_1);
1699 | __export(Storage_1);
1700 | __export(Stream_1);
1701 | });
1702 | define("src/common.browser/ConsoleLoggingListener", ["require", "exports", "src/common/Exports"], function (require, exports, Exports_1) {
1703 | "use strict";
1704 | Object.defineProperty(exports, "__esModule", { value: true });
1705 | var ConsoleLoggingListener = (function () {
1706 | function ConsoleLoggingListener(logLevelFilter) {
1707 | if (logLevelFilter === void 0) { logLevelFilter = Exports_1.EventType.Warning; }
1708 | var _this = this;
1709 | this.OnEvent = function (event) {
1710 | if (event.EventType >= _this.logLevelFilter) {
1711 | var log = _this.ToString(event);
1712 | switch (event.EventType) {
1713 | case Exports_1.EventType.Debug:
1714 | console.debug(log);
1715 | break;
1716 | case Exports_1.EventType.Info:
1717 | console.info(log);
1718 | break;
1719 | case Exports_1.EventType.Warning:
1720 | console.warn(log);
1721 | break;
1722 | case Exports_1.EventType.Error:
1723 | console.error(log);
1724 | break;
1725 | default:
1726 | console.log(log);
1727 | break;
1728 | }
1729 | }
1730 | };
1731 | this.ToString = function (event) {
1732 | var logFragments = [
1733 | "" + event.EventTime,
1734 | "" + event.Name,
1735 | ];
1736 | for (var prop in event) {
1737 | if (prop && event.hasOwnProperty(prop) && prop !== "eventTime" && prop !== "eventType" && prop !== "eventId" && prop !== "name" && prop !== "constructor") {
1738 | var value = event[prop];
1739 | var valueToLog = "