Skip to content

Commit 655687c

Browse files
committed
fix(preview-api): raise deviceLost event after timeout of 5 seconds
In case when some `.js` file is changed, preview app is restarted on device e.g preview app is stopped and started again. When the preview app is stopped, Pubnub reports the device as lost and when preview app is started, Pubnub reports the device as found. With this fix, we want to delay the emitting of deviceLost event. This way we'll give a chance to find the device before reporting it as lost.
1 parent 986673d commit 655687c

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lib/services/livesync/playground/devices/preview-devices-service.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DeviceDiscoveryEventNames, DEVICE_LOG_EVENT_NAME } from "../../../../co
44

55
export class PreviewDevicesService extends EventEmitter implements IPreviewDevicesService {
66
private connectedDevices: Device[] = [];
7+
private deviceTimers: IDictionary<NodeJS.Timer> = {};
78

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

2425
_(this.connectedDevices)
2526
.reject(d => _.find(devices, device => d.id === device.id))
26-
.each(device => this.raiseDeviceLost(device));
27+
.each(device => this.raiseDeviceLostAfterTimeout(device));
2728
}
2829

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

4748
private raiseDeviceFound(device: Device) {
49+
if (this.deviceTimers[device.id]) {
50+
clearTimeout(this.deviceTimers[device.id]);
51+
}
52+
4853
this.emit(DeviceDiscoveryEventNames.DEVICE_FOUND, device);
4954
this.connectedDevices.push(device);
5055
}
@@ -53,5 +58,15 @@ export class PreviewDevicesService extends EventEmitter implements IPreviewDevic
5358
this.emit(DeviceDiscoveryEventNames.DEVICE_LOST, device);
5459
_.remove(this.connectedDevices, d => d.id === device.id);
5560
}
61+
62+
private raiseDeviceLostAfterTimeout(device: Device) {
63+
if (!this.deviceTimers[device.id]) {
64+
const timeoutId = setTimeout(() => {
65+
this.raiseDeviceLost(device);
66+
clearTimeout(timeoutId);
67+
}, 5 * 1000);
68+
this.deviceTimers[device.id] = timeoutId;
69+
}
70+
}
5671
}
5772
$injector.register("previewDevicesService", PreviewDevicesService);

0 commit comments

Comments
 (0)