UNPKG

2.21 kBJavaScriptView Raw
1(function (factory) {
2 if (typeof module === "object" && typeof module.exports === "object") {
3 var v = factory(require, exports);
4 if (v !== undefined) module.exports = v;
5 }
6 else if (typeof define === "function" && define.amd) {
7 define(["require", "exports", "./channels/WebSocket", "./channels/Http"], factory);
8 }
9})(function (require, exports) {
10 "use strict";
11 Object.defineProperty(exports, "__esModule", { value: true });
12 var WebSocket_1 = require("./channels/WebSocket");
13 var Http_1 = require("./channels/Http");
14 var Channel = (function () {
15 function Channel(options) {
16 this.options = options;
17 }
18 Channel.prototype.sendMessage = function (name, data) {
19 var _this = this;
20 return this._initialize().then(function () {
21 return _this._channel.sendMessage(name, data);
22 });
23 };
24 Channel.prototype._initialize = function () {
25 var _this = this;
26 if (!this._initialized) {
27 this._initialized = new Promise(function (resolve) {
28 if (_this.options.port) {
29 try {
30 _this._channel = new WebSocket_1.default(_this.options);
31 _this._channel.sendMessage('remoteStatus', 'ping').then(function () {
32 resolve();
33 }, function (_error) {
34 _this._channel = new Http_1.default(_this.options);
35 resolve();
36 });
37 }
38 catch (error) {
39 _this._channel = new Http_1.default(_this.options);
40 resolve();
41 }
42 }
43 else {
44 _this._channel = new Http_1.default(_this.options);
45 resolve();
46 }
47 });
48 }
49 return this._initialized;
50 };
51 return Channel;
52 }());
53 exports.default = Channel;
54});
55//# sourceMappingURL=Channel.js.map
\No newline at end of file