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

User extensions are loaded multiple times, stop() is not called #9520

Closed
okastl opened this issue Nov 5, 2021 · 4 comments
Closed

User extensions are loaded multiple times, stop() is not called #9520

okastl opened this issue Nov 5, 2021 · 4 comments
Labels
problem Something isn't working

Comments

@okastl
Copy link

okastl commented Nov 5, 2021

What happened

When creating a user extension, changing and saving the extension in the web interface will cause the extension to be loaded multiple times. This is different to the behavior before the typescript refactor. The stop() function of the extension is not called.

What did you expect to happen

The extension should be stopped and the changed version should be loaded.

How to reproduce it (minimal and precise)

Install the attached test.js extension.
On every state change you will see "State changed Number 1" in the log.
Edit the extension with the web interface and save it:
async onStateChange(data) { console.log("State changed Number 2"); }
On every state change you will see "State changed Number 1" and "State changed Number 2" in the log.

Edit the extension with the web interface and save it:
async onStateChange(data) { console.log("State changed Number 3"); }
On every state change you will see "State changed Number 1", "State changed Number 2" and "State changed Number 3" in the log.
Note, that the stop() function is not called.

Related problem, if you exit z2m, stop() is called, but an exception is thrown:
´Failed to call 'TestExtension' 'stop' (TypeError: this.eventBus.removeListenersExtension is not a function
at TestExtension.stop (D:\NB\z2m\zigbee2mqtt\dist\util\externally-loaded.js:15:23)´

test.zip

See also: #7642

Debug info

Zigbee2MQTT version: 1.22.0-dev commit: a09aa42
Adapter hardware: CC26X2R1 (slaesh)
Adapter firmware version: 20210708

@okastl okastl added the problem Something isn't working label Nov 5, 2021
@Koenkk
Copy link
Owner

Koenkk commented Nov 5, 2021

  • I've fixed the starting bug
  • Regarding stop failing, the api has changed slightly, you should change this.eventBus.removeListenersExtension(this.constructor.name); to this.eventBus.removeListeners(this);

Changes will be available in the dev branch in a few hours from now. (https://www.zigbee2mqtt.io/advanced/more/switch-to-dev-branch.html)

@okastl
Copy link
Author

okastl commented Nov 5, 2021

  • I've fixed the starting bug

Just tried 1.22.0-dev commit: 2399ec0, which I believe should contain your fix, but the behavior is the same.

  • Regarding stop failing, the api has changed slightly, you should change this.eventBus.removeListenersExtension(this.constructor.name); to this.eventBus.removeListeners(this);

Thanks, this fixes the exception on exit.

@Koenkk
Copy link
Owner

Koenkk commented Nov 7, 2021

Ah, I see the eventbus.on call is also wrong, so this should work:

class TestExtension {
    constructor(zigbee, mqtt, state, publishEntityState, eventBus, settings, logger) {
        logger.info('Loaded Test');
        this.eventBus = eventBus;
        this.eventBus.on('stateChange', this.onStateChange.bind(this), this);
    }

    async onStateChange(data) {
        console.log("State changed Number 1");

    }

    async stop() {
        console.log("async stop called"); 
        this.eventBus.removeListeners(this);
    }
}

module.exports = TestExtension;

@okastl
Copy link
Author

okastl commented Nov 7, 2021

Ah, I see the eventbus.on call is also wrong, so this should work:
...

Indeed, it does! Thank you!

@okastl okastl closed this as completed Nov 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
problem Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants