UNPKG

mobile-cli-lib

Version:
110 lines (109 loc) 5.75 kB
"use strict"; var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var events_1 = require("events"); var DeviceEmitter = (function (_super) { __extends(DeviceEmitter, _super); function DeviceEmitter($androidDeviceDiscovery, $iOSDeviceDiscovery, $iOSSimulatorDiscovery, $devicesService, $deviceLogProvider, $companionAppsService, $projectConstants, $logger) { _super.call(this); this.$androidDeviceDiscovery = $androidDeviceDiscovery; this.$iOSDeviceDiscovery = $iOSDeviceDiscovery; this.$iOSSimulatorDiscovery = $iOSSimulatorDiscovery; this.$devicesService = $devicesService; this.$deviceLogProvider = $deviceLogProvider; this.$companionAppsService = $companionAppsService; this.$projectConstants = $projectConstants; this.$logger = $logger; } Object.defineProperty(DeviceEmitter.prototype, "companionAppIdentifiers", { get: function () { if (!this._companionAppIdentifiers) { this._companionAppIdentifiers = this.$companionAppsService.getAllCompanionAppIdentifiers(); } return this._companionAppIdentifiers; }, enumerable: true, configurable: true }); DeviceEmitter.prototype.initialize = function () { var _this = this; return (function () { try { _this.$androidDeviceDiscovery.ensureAdbServerStarted().wait(); } catch (err) { _this.$logger.warn("Unable to start adb server. Error message is: " + err.message); } _this.$androidDeviceDiscovery.on("deviceFound", function (device) { _this.emit("deviceFound", device.deviceInfo); _this.attachApplicationChangedHandlers(device); device.openDeviceLogStream(); }); _this.$androidDeviceDiscovery.on("deviceLost", function (device) { _this.emit("deviceLost", device.deviceInfo); }); _this.$iOSDeviceDiscovery.on("deviceFound", function (device) { _this.emit("deviceFound", device.deviceInfo); _this.attachApplicationChangedHandlers(device); device.openDeviceLogStream(); }); _this.$iOSDeviceDiscovery.on("deviceLost", function (device) { _this.emit("deviceLost", device.deviceInfo); }); _this.$iOSSimulatorDiscovery.on("deviceFound", function (device) { _this.emit("deviceFound", device.deviceInfo); device.openDeviceLogStream(); _this.attachApplicationChangedHandlers(device); }); _this.$iOSSimulatorDiscovery.on("deviceLost", function (device) { _this.emit("deviceLost", device.deviceInfo); }); _this.$devicesService.initialize({ skipInferPlatform: true }).wait(); _this.$deviceLogProvider.on("data", function (identifier, data) { _this.emit('deviceLogData', identifier, data.toString()); }); }).future()(); }; DeviceEmitter.prototype.attachApplicationChangedHandlers = function (device) { var _this = this; device.applicationManager.on("applicationInstalled", function (appIdentifier) { _this.emit("applicationInstalled", device.deviceInfo.identifier, appIdentifier); _this.checkCompanionAppChanged(device, appIdentifier, "companionAppInstalled"); }); device.applicationManager.on("applicationUninstalled", function (appIdentifier) { _this.emit("applicationUninstalled", device.deviceInfo.identifier, appIdentifier); _this.checkCompanionAppChanged(device, appIdentifier, "companionAppUninstalled"); }); device.applicationManager.on("debuggableAppFound", function (debuggableAppInfo) { _this.emit("debuggableAppFound", debuggableAppInfo); }); device.applicationManager.on("debuggableAppLost", function (debuggableAppInfo) { _this.emit("debuggableAppLost", debuggableAppInfo); }); device.applicationManager.on("debuggableViewFound", function (appIdentifier, debuggableWebViewInfo) { _this.emit("debuggableViewFound", device.deviceInfo.identifier, appIdentifier, debuggableWebViewInfo); }); device.applicationManager.on("debuggableViewLost", function (appIdentifier, debuggableWebViewInfo) { _this.emit("debuggableViewLost", device.deviceInfo.identifier, appIdentifier, debuggableWebViewInfo); }); device.applicationManager.on("debuggableViewChanged", function (appIdentifier, debuggableWebViewInfo) { _this.emit("debuggableViewChanged", device.deviceInfo.identifier, appIdentifier, debuggableWebViewInfo); }); }; DeviceEmitter.prototype.checkCompanionAppChanged = function (device, applicationName, eventName) { var _this = this; var devicePlatform = device.deviceInfo.platform.toLowerCase(); _.each(this.companionAppIdentifiers, function (platformsCompanionAppIdentifiers, framework) { if (applicationName === platformsCompanionAppIdentifiers[devicePlatform]) { _this.emit(eventName, device.deviceInfo.identifier, framework); return false; } }); }; return DeviceEmitter; }(events_1.EventEmitter)); exports.DeviceEmitter = DeviceEmitter; $injector.register("deviceEmitter", DeviceEmitter);