react-native-tv-ws-protocol
Version:
wrap websocket protocol to control the smart TVs
130 lines (111 loc) • 3.96 kB
JavaScript
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; }
import { NativeModules, NativeEventEmitter } from 'react-native';
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 = NativeModules.TvWsProtocol;
this.nativeEvent = new NativeEventEmitter(this.TvWsProtocol);
this.initListener();
} catch (err) {
this.nativeEvent = new NativeEventEmitter(null);
this.error(err);
}
}
}
export default TvWsProtocol;
//# sourceMappingURL=index.js.map