Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Add no-floating-promises options to tslint in order to ensure all promises are properly handled #1112

Merged
merged 2 commits into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions appbuilder/device-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export class DeviceEmitter extends EventEmitter {
this.attachApplicationChangedHandlers(device);

// await: Do not await as this will require to mark the lambda with async keyword, but there's no way to await the lambda itself.
/* tslint:disable:no-floating-promises */
device.openDeviceLogStream();
/* tslint:enable:no-floating-promises */
});

this.$devicesService.on(DeviceDiscoveryEventNames.DEVICE_LOST, (device: Mobile.IDevice) => {
Expand Down
3 changes: 2 additions & 1 deletion helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export function settlePromises<T>(promises: Promise<T>[]): Promise<T[]> {
if (settledPromisesCount === length) {
errors.length ? reject(new Error(`Multiple errors were thrown:${EOL}${errors.map(e => e.message || e).join(EOL)}`)) : resolve(results);
}
});
})
.catch();
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class HttpClient implements Server.IHttpClient {
return response;
}

private async setResponseResult(result: IPromiseActions<Server.IResponse>, timerId: number, resultData: { response?: Server.IRequestResponseData, body?: string, err?: Error }): Promise<void> {
private setResponseResult(result: IPromiseActions<Server.IResponse>, timerId: number, resultData: { response?: Server.IRequestResponseData, body?: string, err?: Error }): void {
if (timerId) {
clearTimeout(timerId);
timerId = null;
Expand Down
16 changes: 9 additions & 7 deletions progress-indicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ export class ProgressIndicator implements IProgressIndicator {
let isFulfilled = false;

const tempPromise = new Promise<T>((resolve, reject) => {
promise.then((res) => {
isFulfilled = true;
resolve(res);
}, (err) => {
isFulfilled = true;
reject(err);
});
promise
.then(res => {
isFulfilled = true;
resolve(res);
})
.catch(err => {
isFulfilled = true;
reject(err);
});
});

if (!isInteractive()) {
Expand Down
2 changes: 1 addition & 1 deletion services/livesync-service-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class LiveSyncServiceBase implements ILiveSyncServiceBase {
const livesyncData = this.livesyncData[platformName];
await batch.syncFiles(async (filesToSync: string[]) => {
await this.$liveSyncProvider.preparePlatformForSync(platformName, projectId);
this.syncCore([livesyncData], filesToSync);
await this.syncCore([livesyncData], filesToSync);
});
}
} catch (err) {
Expand Down
6 changes: 3 additions & 3 deletions services/livesync/sync-batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class SyncBatch {
}
}

public addFile(file: string): void {
public async addFile(file: string): Promise<void> {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
Expand All @@ -36,12 +36,12 @@ export class SyncBatch {
this.syncQueue.push(file);

if (!this.syncInProgress) {
this.timer = setTimeout(() => {
this.timer = setTimeout(async () => {
if (this.syncQueue.length > 0) {
this.$logger.trace("Syncing %s", this.syncQueue.join(", "));
try {
this.syncInProgress = true;
this.done();
await this.done();
} finally {
this.syncInProgress = false;
}
Expand Down
6 changes: 3 additions & 3 deletions test/unit-tests/android-application-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,23 +243,23 @@ describe("android-application-manager", () => {
it("should stop the logcat helper", async () => {
setup();

androidApplicationManager.stopApplication(validStartOptions);
await androidApplicationManager.stopApplication(validStartOptions);

assert.equal(logcatHelper.StopCallCount, 1);
});

it("should stop the application", async () => {
setup();

androidApplicationManager.stopApplication(validStartOptions);
await androidApplicationManager.stopApplication(validStartOptions);

assert.isTrue(androidDebugBridge.calledStopApplication);
});

it("should reset the current pid", async () => {
setup();

androidApplicationManager.stopApplication(validStartOptions);
await androidApplicationManager.stopApplication(validStartOptions);

assert.equal(deviceLogProvider.currentDevicePids[validDeviceIdentifier], null);
});
Expand Down
6 changes: 4 additions & 2 deletions test/unit-tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,12 @@ describe("helpers", () => {
helpers.settlePromises<any>(testData.input)
.then(res => {
assert.deepEqual(res, testData.expectedResult);
}, err => {
})
.catch(err => {
assert.deepEqual(err.message, testData.expectedError);
})
.then(done, done);
.then(done)
.catch(done);
});
});

Expand Down
24 changes: 10 additions & 14 deletions test/unit-tests/mobile/android/logcat-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ function createTestInjector(): IInjector {
return injector;
}

function startLogcatHelper(injector: IInjector, startOptions: { deviceIdentifier: string, pid?: string }) {
const logcatHelper = injector.resolve<LogcatHelper>("logcatHelper");
/* tslint:disable:no-floating-promises */
logcatHelper.start(startOptions);
/* tslint:enable:no-floating-promises */
}

describe("logcat-helper", () => {
const validIdentifier = "valid-identifier";
let injector: IInjector;
Expand All @@ -105,10 +112,7 @@ describe("logcat-helper", () => {
}
});

const logcatHelper = injector.resolve<LogcatHelper>("logcatHelper");
logcatHelper.start({
deviceIdentifier: validIdentifier
});
startLogcatHelper(injector, { deviceIdentifier: validIdentifier });
});

it("should pass the pid filter to the adb process", (done: mocha.Done) => {
Expand All @@ -123,11 +127,7 @@ describe("logcat-helper", () => {
}
});

const logcatHelper = injector.resolve<LogcatHelper>("logcatHelper");
logcatHelper.start({
deviceIdentifier: validIdentifier,
pid: expectedPid
});
startLogcatHelper(injector, { deviceIdentifier: validIdentifier, pid: expectedPid });
});

it("should not pass the pid filter to the adb process when Android version is less than 7", (done: mocha.Done) => {
Expand All @@ -152,11 +152,7 @@ describe("logcat-helper", () => {
}
});

const logcatHelper = injector.resolve<LogcatHelper>("logcatHelper");
logcatHelper.start({
deviceIdentifier: validIdentifier,
pid: expectedPid
});
startLogcatHelper(injector, { deviceIdentifier: validIdentifier, pid: expectedPid });
});

it("should start a single adb process when called multiple times with the same identifier", async () => {
Expand Down
Loading