Skip to content
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

fix: Don't emit deviceLeave event for devices that already left #965

Merged
merged 1 commit into from
Mar 9, 2024
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
9 changes: 6 additions & 3 deletions src/controller/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,14 @@ class Controller extends events.EventEmitter {
debug.log(`Device leave '${payload.ieeeAddr}'`);

const device = Device.byIeeeAddr(payload.ieeeAddr);
if (device) {
debug.log(`Removing device from database '${payload.ieeeAddr}'`);
device.removeFromDatabase();
if (!device) {
debug.log(`Device leave is from unknown device '${payload.ieeeAddr}'`);
return;
}

debug.log(`Removing device from database '${payload.ieeeAddr}'`);
device.removeFromDatabase();

const data: Events.DeviceLeavePayload = {ieeeAddr: payload.ieeeAddr};
this.selfAndDeviceEmit(device, Events.Events.deviceLeave, data);
}
Expand Down
4 changes: 2 additions & 2 deletions test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,8 +936,8 @@ describe('Controller', () => {

// leaves another time when not in database
await mockAdapterEvents['deviceLeave']({networkAddress: 129, ieeeAddr: '0x129'});
expect(events.deviceLeave.length).toBe(2);
expect(events.deviceLeave[1]).toStrictEqual({ieeeAddr: '0x129'});
expect(events.deviceLeave.length).toBe(1);
expect(events.deviceLeave[0]).toStrictEqual({ieeeAddr: '0x129'});
});

it('Start with reset should clear database', async () => {
Expand Down