RangerMauve / mqtt-emitter

Emit MQTT events to listeners using mqtt-regex for routing
MIT License
10 stars 6 forks source link

once throws an error when removing listener #1

Closed encojosh closed 8 years ago

encojosh commented 8 years ago
function once(topic, handler) {
    once_handler.handler = handler;
    this.on(topic, once_handler);

    return this;

    function once_handler(data, params) {
        handler.call(this, data, params);
        this.removeListener(topic, once_handler);  //this has no method removeListener
    }
}

The this object refers to the once_handler function and not the mqtt-emitter class. You need closure here.

function once(topic, handler) {
    var self = this;
        once_handler.handler = handler;
    this.on(topic, once_handler);

    return this;

    function once_handler(data, params) {
        handler.call(self, data, params);
        self.removeListener(topic, once_handler);  //this has no method removeListener
    }
}
RangerMauve commented 8 years ago

Thanks for reporting this! I'm going to be a bit busy the next few days, but if you'd like to submit a PR I should be able to get it released by Sunday night. Else I'll try to get to it some time at the beginning of next week.

RangerMauve commented 8 years ago

Sorry I forgot to get to it before! Closed in #2 Thanks to @Zone-Ghost