Skip to content

"TypeError: Handler is not a function" when unsubscribing listeners inside of a callback #1

@davethegr8

Description

@davethegr8

in src/pubsub.js at line 30, you are assigning i and l before the initialization of the triggering loop, leading to a case where you can cause a fatal TypeError if your handlers unsubscribe function from the same event that cause them. This can be problematic if you use logic control to define which callbacks to execute when.

    for (var i = 0, l = handlers[event].length; i < l; i++) {
        handlers[event][i].apply(ctx, ctx.args);
    }

This code reproduces the issue. Notice that "Done." is never logged.

PubSub.subscribe('test', function () {
    console.log('function 1 called');
});

PubSub.subscribe('test', function () {
    console.log('function 2 called');
    console.log('removing listeners for "test"');
    PubSub.unsubscribe('test');
});

PubSub.subscribe('test', function () {
    console.log('function 3 called');
});

PubSub.publish('test');

console.log('Done.');

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions