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

Extend listener removal in BusHelper #37

Merged
merged 3 commits into from Feb 19, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/BusHelper.js
Expand Up @@ -97,6 +97,14 @@ class BusHelper extends EventEmitter {
return this._ifaceProxy[methodName](...args)
}

removeListeners () {
this.removeAllListeners('PropertiesChanged')
if (this._propsProxy !== null) {
this._propsProxy.removeAllListeners('PropertiesChanged')
Copy link
Owner

Choose a reason for hiding this comment

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

@tuxedoxt I understand that you want to delete this listener because it generate a memory leak, but I think that if you do this in this way, you creates a different bug.
What if the user connects again this device? No code line will subscribe again on 'PropertiesChanged' events, so the library will not work correctly.

I think that this bug requires a different fix.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback. I see what you mean.

As I see it, additionally setting

this._ready = false

should make _prepare reattach listener. However also does other things, like assigns completely new _propsProxy and _ifaceProxy objects. Would more clean-up be needed there or is this feasible?

this._ready = false
}
}

static buildChildren (path, nodes) {
if (path === '/') path = ''
const children = new Set()
Expand Down
2 changes: 1 addition & 1 deletion src/Device.js
Expand Up @@ -119,7 +119,7 @@ class Device extends EventEmitter {
*/
async disconnect () {
await this.helper.callMethod('Disconnect')
this.helper.removeAllListeners('PropertiesChanged') // might be improved
this.helper.removeListeners()
}

/**
Expand Down
28 changes: 28 additions & 0 deletions test/BusHelper.spec.js
Expand Up @@ -125,3 +125,31 @@ test('propsEvents', async () => {
await helper.set('VirtualProperty', buildTypedValue('string', 'bar'))
await expect(res).resolves.toMatchObject({ VirtualProperty: { signature: 's', value: 'bar' } })
})

test('removeListeners', async () => {
const helper = new BusHelper(dbus, TEST_NAME, TEST_OBJECT, TEST_IFACE, { useProps: true, usePropsEvents: true })

const dummyCb = () => {}

// Init with listener on helper (directly attached dummyCb) and _propsProxy (through method call triggered _prepare)
helper.on('PropertiesChanged', dummyCb)
await helper.callMethod('Echo', 'ping')
expect(helper.listenerCount('PropertiesChanged')).toBeGreaterThan(0)
expect(helper._propsProxy.listenerCount('PropertiesChanged')).toBeGreaterThan(0)

// Test remove
helper.removeListeners()
expect(helper.listenerCount('PropertiesChanged')).toBe(0)
expect(helper._propsProxy.listenerCount('PropertiesChanged')).toBe(0)

// Test reuse after remove (same initialization as before)
helper.on('PropertiesChanged', dummyCb)
await helper.callMethod('Echo', 'ping')
expect(helper.listenerCount('PropertiesChanged')).toBeGreaterThan(0)
expect(helper._propsProxy.listenerCount('PropertiesChanged')).toBeGreaterThan(0)

// Remove second time
helper.removeListeners()
expect(helper.listenerCount('PropertiesChanged')).toBe(0)
expect(helper._propsProxy.listenerCount('PropertiesChanged')).toBe(0)
})
1 change: 1 addition & 0 deletions test/Device.spec.js
Expand Up @@ -13,6 +13,7 @@ jest.doMock('../src/BusHelper', () => {
this.waitPropChange = jest.fn()
this.children = jest.fn()
this.callMethod = jest.fn()
this.removeListeners = jest.fn()
}
}
})
Expand Down