-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathapplication-manager-base.ts
149 lines (118 loc) · 6.51 KB
/
application-manager-base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { EventEmitter } from "events";
import { TARGET_FRAMEWORK_IDENTIFIERS } from "../constants";
export abstract class ApplicationManagerBase extends EventEmitter implements Mobile.IDeviceApplicationManager {
private lastInstalledAppIdentifiers: string[];
private lastAvailableDebuggableApps: Mobile.IDeviceApplicationInformation[];
private lastAvailableDebuggableAppViews: IDictionary<Mobile.IDebugWebViewInfo[]> = {};
constructor(protected $logger: ILogger,
protected $hooksService: IHooksService,
protected $deviceLogProvider: Mobile.IDeviceLogProvider) {
super();
}
public async setTransferredAppFiles(files: string[]): Promise<void> {
for (const file of files) {
await this.$deviceLogProvider.setSourceFileLocation(file);
}
}
public async reinstallApplication(appIdentifier: string, packageFilePath: string, buildData?: IBuildData): Promise<void> {
const isApplicationInstalled = await this.isApplicationInstalled(appIdentifier);
if (isApplicationInstalled) {
await this.uninstallApplication(appIdentifier);
}
await this.installApplication(packageFilePath, appIdentifier, buildData);
}
public async restartApplication(appData: Mobile.IApplicationData): Promise<void> {
await this.stopApplication(appData);
await this.startApplication(appData);
}
public async isApplicationInstalled(appIdentifier: string): Promise<boolean> {
await this.checkForApplicationUpdates();
return _.includes(this.lastInstalledAppIdentifiers, appIdentifier);
}
private checkForApplicationUpdatesPromise: Promise<void>;
public async checkForApplicationUpdates(): Promise<void> {
if (!this.checkForApplicationUpdatesPromise) {
this.checkForApplicationUpdatesPromise = new Promise<void>(async (resolve, reject) => {
let isFulfilled = false;
// As this method is called on 500ms, but it's execution may last much longer
// use locking, so the next executions will not get into the body, while the first one is still working.
// In case we do not break the next executions, we'll report each app as newly installed several times.
try {
const currentlyInstalledAppIdentifiers = await this.getInstalledApplications();
const previouslyInstalledAppIdentifiers = this.lastInstalledAppIdentifiers || [];
const newAppIdentifiers = _.difference(currentlyInstalledAppIdentifiers, previouslyInstalledAppIdentifiers);
const removedAppIdentifiers = _.difference(previouslyInstalledAppIdentifiers, currentlyInstalledAppIdentifiers);
this.lastInstalledAppIdentifiers = currentlyInstalledAppIdentifiers;
_.each(newAppIdentifiers, appIdentifier => this.emit("applicationInstalled", appIdentifier));
_.each(removedAppIdentifiers, appIdentifier => this.emit("applicationUninstalled", appIdentifier));
await this.checkForAvailableDebuggableAppsChanges();
} catch (err) {
isFulfilled = true;
reject(err);
} finally {
this.checkForApplicationUpdatesPromise = null;
if (!isFulfilled) {
resolve();
}
}
});
}
return this.checkForApplicationUpdatesPromise;
}
public async tryStartApplication(appData: Mobile.IApplicationData): Promise<void> {
try {
await this.startApplication(appData);
} catch (err) {
this.$logger.trace(`Unable to start application ${appData.appId} with name ${appData.projectName}. Error is: ${err.message}`);
}
}
public abstract async installApplication(packageFilePath: string, appIdentifier?: string, buildData?: IBuildData): Promise<void>;
public abstract async uninstallApplication(appIdentifier: string): Promise<void>;
public abstract async startApplication(appData: Mobile.IApplicationData): Promise<void>;
public abstract async stopApplication(appData: Mobile.IApplicationData): Promise<void>;
public abstract async getInstalledApplications(): Promise<string[]>;
public abstract async getDebuggableApps(): Promise<Mobile.IDeviceApplicationInformation[]>;
public abstract async getDebuggableAppViews(appIdentifiers: string[]): Promise<IDictionary<Mobile.IDebugWebViewInfo[]>>;
private async checkForAvailableDebuggableAppsChanges(): Promise<void> {
const currentlyAvailableDebuggableApps = await this.getDebuggableApps();
const previouslyAvailableDebuggableApps = this.lastAvailableDebuggableApps || [];
const newAvailableDebuggableApps = _.differenceBy(currentlyAvailableDebuggableApps, previouslyAvailableDebuggableApps, "appIdentifier");
const notAvailableAppsForDebugging = _.differenceBy(previouslyAvailableDebuggableApps, currentlyAvailableDebuggableApps, "appIdentifier");
this.lastAvailableDebuggableApps = currentlyAvailableDebuggableApps;
_.each(newAvailableDebuggableApps, (appInfo: Mobile.IDeviceApplicationInformation) => {
this.emit("debuggableAppFound", appInfo);
});
_.each(notAvailableAppsForDebugging, (appInfo: Mobile.IDeviceApplicationInformation) => {
this.emit("debuggableAppLost", appInfo);
if (_.has(this.lastAvailableDebuggableAppViews, appInfo.appIdentifier)) {
// Prevent emitting debuggableViewLost when application cannot be debugged anymore.
delete this.lastAvailableDebuggableAppViews[appInfo.appIdentifier];
}
});
const cordovaDebuggableAppIdentifiers = _(currentlyAvailableDebuggableApps)
.filter(c => c.framework === TARGET_FRAMEWORK_IDENTIFIERS.Cordova)
.map(c => c.appIdentifier)
.value();
const currentlyAvailableAppViews = await this.getDebuggableAppViews(cordovaDebuggableAppIdentifiers);
_.each(currentlyAvailableAppViews, (currentlyAvailableViews, appIdentifier) => {
const previouslyAvailableViews = this.lastAvailableDebuggableAppViews[appIdentifier];
const newAvailableViews = _.differenceBy(currentlyAvailableViews, previouslyAvailableViews, "id");
const notAvailableViews = _.differenceBy(previouslyAvailableViews, currentlyAvailableViews, "id");
_.each(notAvailableViews, debugWebViewInfo => {
this.emit("debuggableViewLost", appIdentifier, debugWebViewInfo);
});
_.each(newAvailableViews, debugWebViewInfo => {
this.emit("debuggableViewFound", appIdentifier, debugWebViewInfo);
});
// Determine which of the views had changed since last check and raise debuggableViewChanged event for them:
const keptViews = _.differenceBy(currentlyAvailableViews, newAvailableViews, "id");
_.each(keptViews, view => {
const previousTimeViewInfo = _.find(previouslyAvailableViews, previousView => previousView.id === view.id);
if (!_.isEqual(view, previousTimeViewInfo)) {
this.emit("debuggableViewChanged", appIdentifier, view);
}
});
this.lastAvailableDebuggableAppViews[appIdentifier] = currentlyAvailableViews;
});
}
}