UNPKG

react-native-tv-ws-protocol

Version:
138 lines (117 loc) 4.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _reactNative = require("react-native"); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } class TvWsProtocol { constructor() { _defineProperty(this, "TvWsProtocol", void 0); _defineProperty(this, "onmessage", void 0); _defineProperty(this, "onconnect", void 0); _defineProperty(this, "ondisconnect", void 0); _defineProperty(this, "onerror", void 0); _defineProperty(this, "nativeEvent", void 0); _defineProperty(this, "eventSubscriptions", []); _defineProperty(this, "initListener", () => { try { let onMessageSubscription = this.nativeEvent.addListener('message', data => { try { if (typeof this.onmessage === 'function') { this.onmessage(data); } } catch (err) { this.error(err); } }); this.eventSubscriptions.push(onMessageSubscription); let onErrorSubscription = this.nativeEvent.addListener('error', message => { try { if (typeof this.onerror === 'function') { this.onerror(message); } } catch (err) { this.error(err); } }); this.eventSubscriptions.push(onErrorSubscription); let onDisconnectSubscription = this.nativeEvent.addListener('disconnect', () => { try { if (typeof this.ondisconnect === 'function') { this.ondisconnect(); } } catch (err) { this.error(err); } }); this.eventSubscriptions.push(onDisconnectSubscription); let onConnectSubscription = this.nativeEvent.addListener('connect', () => { try { if (typeof this.onconnect === 'function') { this.onconnect(); } } catch (err) { this.error(err); } }); this.eventSubscriptions.push(onConnectSubscription); } catch (err) { this.error(err); } }); _defineProperty(this, "create", (uri, options) => { try { this.TvWsProtocol.create(uri, options); } catch (err) { this.error(err); } }); _defineProperty(this, "send", message => { try { this.TvWsProtocol.send(message); } catch (err) { this.error(err); } }); _defineProperty(this, "isConnected", () => { try { var _this$TvWsProtocol; return (_this$TvWsProtocol = this.TvWsProtocol) === null || _this$TvWsProtocol === void 0 ? void 0 : _this$TvWsProtocol.isConnected(); } catch (err) { this.error(err); } }); _defineProperty(this, "close", () => { try { var _this$eventSubscripti, _this$eventSubscripti2; (_this$eventSubscripti = this.eventSubscriptions) === null || _this$eventSubscripti === void 0 ? void 0 : (_this$eventSubscripti2 = _this$eventSubscripti.map) === null || _this$eventSubscripti2 === void 0 ? void 0 : _this$eventSubscripti2.call(_this$eventSubscripti, sub => { sub === null || sub === void 0 ? void 0 : sub.remove(); }); this.TvWsProtocol.close(); } catch (err) { this.error(err); } }); _defineProperty(this, "error", message => { try { if (typeof this.onerror === 'function') { this.onerror(message); } } catch (err) { console.log(err); } }); try { this.TvWsProtocol = _reactNative.NativeModules.TvWsProtocol; this.nativeEvent = new _reactNative.NativeEventEmitter(this.TvWsProtocol); this.initListener(); } catch (err) { this.nativeEvent = new _reactNative.NativeEventEmitter(null); this.error(err); } } } var _default = TvWsProtocol; exports.default = _default; //# sourceMappingURL=index.js.map