Skip to content

Commit 7471fbf

Browse files
committed
Fixed possible MaxListenersExceededWarning
1 parent 5e15218 commit 7471fbf

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/eufysecurity.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
7272
[dataType: string]: NodeJS.Timeout;
7373
} = {};
7474

75-
private stationsLoaded = false;
76-
private devicesLoaded = false;
7775
private loadingEmitter = new EventEmitter();
76+
private stationsLoaded?: Promise<void>;
77+
private devicesLoaded?: Promise<void>;
7878

7979
private constructor(config: EufySecurityConfig, log: Logger = dummyLogger) {
8080
super();
@@ -287,8 +287,8 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
287287
}
288288

289289
private async updateStation(hub: StationListResponse): Promise<void> {
290-
if (!this.stationsLoaded)
291-
await waitForEvent(this.loadingEmitter, "stations loaded");
290+
if (this.stationsLoaded)
291+
await this.stationsLoaded;
292292
if (Object.keys(this.stations).includes(hub.station_sn)) {
293293
this.stations[hub.station_sn].update(hub, this.stations[hub.station_sn] !== undefined && !this.stations[hub.station_sn].isIntegratedDevice() && this.stations[hub.station_sn].isConnected());
294294
if (!this.stations[hub.station_sn].isConnected() && !this.stations[hub.station_sn].isEnergySavingDevice()) {
@@ -337,17 +337,17 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
337337
}
338338

339339
private async updateDevice(device: DeviceListResponse): Promise<void> {
340-
if (!this.devicesLoaded)
341-
await waitForEvent(this.loadingEmitter, "devices loaded");
340+
if (this.devicesLoaded)
341+
await this.devicesLoaded;
342342
if (Object.keys(this.devices).includes(device.device_sn))
343343
this.devices[device.device_sn].update(device, this.stations[device.station_sn] !== undefined && !this.stations[device.station_sn].isIntegratedDevice() && this.stations[device.station_sn].isConnected())
344344
else
345345
this.log.debug(`Device with this serial ${device.device_sn} doesn't exists and couldn't be updated!`);
346346
}
347347

348348
public async getDevices(): Promise<Array<Device>> {
349-
if (!this.devicesLoaded)
350-
await waitForEvent(this.loadingEmitter, "devices loaded");
349+
if (this.devicesLoaded)
350+
await this.devicesLoaded;
351351
const arr: Array<Device> = [];
352352
Object.keys(this.devices).forEach((serialNumber: string) => {
353353
arr.push(this.devices[serialNumber]);
@@ -356,8 +356,8 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
356356
}
357357

358358
public async getDevicesFromStation(stationSN: string): Promise<Array<Device>> {
359-
if (!this.devicesLoaded)
360-
await waitForEvent(this.loadingEmitter, "devices loaded");
359+
if (this.devicesLoaded)
360+
await this.devicesLoaded;
361361
const arr: Array<Device> = [];
362362
Object.keys(this.devices).forEach((serialNumber: string) => {
363363
if (this.devices[serialNumber].getStationSerial() === stationSN)
@@ -367,16 +367,16 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
367367
}
368368

369369
public async getDevice(deviceSN: string): Promise<Device> {
370-
if (!this.devicesLoaded)
371-
await waitForEvent(this.loadingEmitter, "devices loaded");
370+
if (this.devicesLoaded)
371+
await this.devicesLoaded;
372372
if (Object.keys(this.devices).includes(deviceSN))
373373
return this.devices[deviceSN];
374374
throw new DeviceNotFoundError(`Device with this serial ${deviceSN} doesn't exists!`);
375375
}
376376

377377
public async getStationDevice(stationSN: string, channel: number): Promise<Device> {
378-
if (!this.devicesLoaded)
379-
await waitForEvent(this.loadingEmitter, "devices loaded");
378+
if (this.devicesLoaded)
379+
await this.devicesLoaded;
380380
for (const device of Object.values(this.devices)) {
381381
if ((device.getStationSerial() === stationSN && device.getChannel() === channel) || (device.getStationSerial() === stationSN && device.getSerial() === stationSN)) {
382382
return device;
@@ -386,8 +386,8 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
386386
}
387387

388388
public async getStations(): Promise<Array<Station>> {
389-
if (!this.stationsLoaded)
390-
await waitForEvent(this.loadingEmitter, "stations loaded");
389+
if (this.stationsLoaded)
390+
await this.stationsLoaded;
391391
const arr: Array<Station> = [];
392392
Object.keys(this.stations).forEach((serialNumber: string) => {
393393
arr.push(this.stations[serialNumber]);
@@ -396,8 +396,8 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
396396
}
397397

398398
public async getStation(stationSN: string): Promise<Station> {
399-
if (!this.stationsLoaded)
400-
await waitForEvent(this.loadingEmitter, "stations loaded");
399+
if (this.stationsLoaded)
400+
await this.stationsLoaded;
401401
if (Object.keys(this.stations).includes(stationSN))
402402
return this.stations[stationSN];
403403
throw new StationNotFoundError(`No station with serial number: ${stationSN}!`);
@@ -438,7 +438,7 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
438438
if (stationsSNs.includes(hub.station_sn)) {
439439
this.updateStation(hub);
440440
} else {
441-
this.stationsLoaded = false;
441+
this.stationsLoaded = waitForEvent<void>(this.loadingEmitter, "stations loaded");
442442
let ipAddress: string | undefined;
443443
if (this.config.stationIPAddresses !== undefined) {
444444
ipAddress = this.config.stationIPAddresses[hub.station_sn];
@@ -499,12 +499,12 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
499499
}
500500
}
501501
Promise.all(promises).then(() => {
502-
this.stationsLoaded = true;
503502
this.loadingEmitter.emit("stations loaded");
503+
this.stationsLoaded = undefined;
504504
});
505505
if (promises.length === 0) {
506-
this.stationsLoaded = true;
507506
this.loadingEmitter.emit("stations loaded");
507+
this.stationsLoaded = undefined;
508508
}
509509
for (const stationSN of stationsSNs) {
510510
if (!newStationsSNs.includes(stationSN)) {
@@ -565,7 +565,7 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
565565
if (deviceSNs.includes(device.device_sn)) {
566566
this.updateDevice(device);
567567
} else {
568-
this.devicesLoaded = false;
568+
this.devicesLoaded = waitForEvent<void>(this.loadingEmitter, "devices loaded");
569569
let new_device: Promise<Device>;
570570

571571
if (Device.isIndoorCamera(device.device_type)) {
@@ -647,12 +647,12 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
647647
this.log.error("Error trying to connect to station afte device loaded", error);
648648
});
649649
});
650-
this.devicesLoaded = true;
651650
this.loadingEmitter.emit("devices loaded");
651+
this.devicesLoaded = undefined;
652652
});
653653
if (promises.length === 0) {
654-
this.devicesLoaded = true;
655654
this.loadingEmitter.emit("devices loaded");
655+
this.devicesLoaded = undefined;
656656
}
657657
for (const deviceSN of deviceSNs) {
658658
if (!newDeviceSNs.includes(deviceSN)) {

0 commit comments

Comments
 (0)