Skip to content

fix(preview-api): raise deviceLost event after timeout of 5 seconds #4150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 21, 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DeviceDiscoveryEventNames, DEVICE_LOG_EVENT_NAME } from "../../../../co

export class PreviewDevicesService extends EventEmitter implements IPreviewDevicesService {
private connectedDevices: Device[] = [];
private deviceLostTimers: IDictionary<NodeJS.Timer> = {};

constructor(private $previewAppLogProvider: IPreviewAppLogProvider,
private $previewAppPluginsService: IPreviewAppPluginsService) {
Expand All @@ -23,7 +24,7 @@ export class PreviewDevicesService extends EventEmitter implements IPreviewDevic

_(this.connectedDevices)
.reject(d => _.find(devices, device => d.id === device.id))
.each(device => this.raiseDeviceLost(device));
.each(device => this.raiseDeviceLostAfterTimeout(device));
}

public getDeviceById(id: string): Device {
Expand All @@ -45,6 +46,10 @@ export class PreviewDevicesService extends EventEmitter implements IPreviewDevic
}

private raiseDeviceFound(device: Device) {
if (this.deviceLostTimers[device.id]) {
clearTimeout(this.deviceLostTimers[device.id]);
}

this.emit(DeviceDiscoveryEventNames.DEVICE_FOUND, device);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be in else (we could also add a test for such case)?

this.connectedDevices.push(device);
}
Expand All @@ -53,5 +58,15 @@ export class PreviewDevicesService extends EventEmitter implements IPreviewDevic
this.emit(DeviceDiscoveryEventNames.DEVICE_LOST, device);
_.remove(this.connectedDevices, d => d.id === device.id);
}

private raiseDeviceLostAfterTimeout(device: Device) {
if (!this.deviceLostTimers[device.id]) {
const timeoutId = setTimeout(() => {
this.raiseDeviceLost(device);
clearTimeout(timeoutId);
}, 5 * 1000);
this.deviceLostTimers[device.id] = timeoutId;
}
}
}
$injector.register("previewDevicesService", PreviewDevicesService);
27 changes: 26 additions & 1 deletion test/services/preview-devices-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Device } from "nativescript-preview-sdk";
import { assert } from "chai";
import { DeviceDiscoveryEventNames } from "../../lib/common/constants";
import { LoggerStub, ErrorsStub } from "../stubs";
import * as sinon from "sinon";

let foundDevices: Device[] = [];
let lostDevices: Device[] = [];
Expand Down Expand Up @@ -38,8 +39,9 @@ function resetDevices() {
}

describe("PreviewDevicesService", () => {
describe("onDevicesPresence", () => {
describe("getConnectedDevices", () => {
let previewDevicesService: IPreviewDevicesService = null;
let clock: sinon.SinonFakeTimers = null;
beforeEach(() => {
const injector = createTestInjector();
previewDevicesService = injector.resolve("previewDevicesService");
Expand All @@ -49,11 +51,13 @@ describe("PreviewDevicesService", () => {
previewDevicesService.on(DeviceDiscoveryEventNames.DEVICE_LOST, device => {
lostDevices.push(device);
});
clock = sinon.useFakeTimers();
});

afterEach(() => {
previewDevicesService.removeAllListeners();
resetDevices();
clock.restore();
});

it("should add new device", () => {
Expand Down Expand Up @@ -101,6 +105,7 @@ describe("PreviewDevicesService", () => {
resetDevices();

previewDevicesService.updateConnectedDevices([]);
clock.tick(5000);

assert.deepEqual(foundDevices, []);
assert.deepEqual(lostDevices, [device1]);
Expand All @@ -116,10 +121,30 @@ describe("PreviewDevicesService", () => {
resetDevices();

previewDevicesService.updateConnectedDevices([device2]);
clock.tick(5000);

assert.deepEqual(previewDevicesService.getConnectedDevices(), [device2]);
assert.deepEqual(foundDevices, [device2]);
assert.deepEqual(lostDevices, [device1]);
});
it("shouldn't emit deviceFound or deviceLost when preview app is restarted on device", () => {
const device1 = createDevice("device1");

previewDevicesService.updateConnectedDevices([device1]);

assert.deepEqual(previewDevicesService.getConnectedDevices(), [device1]);
assert.deepEqual(foundDevices, [device1]);
assert.deepEqual(lostDevices, []);
resetDevices();

// preview app is restarted
previewDevicesService.updateConnectedDevices([]);
clock.tick(500);
previewDevicesService.updateConnectedDevices([device1]);

assert.deepEqual(foundDevices, []);
assert.deepEqual(lostDevices, []);
assert.deepEqual(previewDevicesService.getConnectedDevices(), [device1]);
});
});
});