I
I
Imp2018-11-20 13:26:42
JavaScript
Imp, 2018-11-20 13:26:42

How to remove event listeners?

There is, for example, the following code.

class Account {
    constructor(data) {
        this.user = new User();
        this.registerEvents();
        this.logOn({login: this.user.login, password: this.user.password});
        return this;
    }

    registerEvents() {
        this.user.on('loggedOn', () => {
            console.log('Login success!');
        })
        
        this.user.on('friendList', () => {
            //... -- обработал данные, и хочу завершить работу
            this.destruction();
        })

    }    

    logOn(data) {
        this.user.logOn(data);
    }

    destruction() {
        this.user.removeAllListeners();
        this.user.logOff();
        this.user = null;
    }
}

The problem is that after the destruction(); The software is still listening to events.
Yes, I understand - you can use not .on() but .once() but sometimes it happens that I need to process the data several times.
How to clear the class in general, so that there are no leaks?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2018-11-20
@Stalker_RED

https://nodejs.org/api/events.html#events_event_re...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question