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

Stapes.on returning invalid e.scope #19

Closed
contra opened this Issue Oct 8, 2012 · 6 comments

Comments

Projects
None yet
2 participants
@contra

contra commented Oct 8, 2012

For some reason e.scope is always the same no matter which module is emitting the event. It seems like after the first emit of the event the scope gets locked and used for all future events

@hay

This comment has been minimized.

Show comment
Hide comment
@hay

hay Oct 9, 2012

Owner

Does this problem occur with all events, or just the global ones coming from Stapes.on? I know it's a problem with the latter one. A testcase would be helpful...

Owner

hay commented Oct 9, 2012

Does this problem occur with all events, or just the global ones coming from Stapes.on? I know it's a problem with the latter one. A testcase would be helpful...

@hay hay closed this Oct 9, 2012

@hay hay reopened this Oct 9, 2012

@contra

This comment has been minimized.

Show comment
Hide comment
@contra

contra Oct 9, 2012

Are there any existing tests for the events system? Right now I'm only getting the issue with Stapes.on

contra commented Oct 9, 2012

Are there any existing tests for the events system? Right now I'm only getting the issue with Stapes.on

@contra

This comment has been minimized.

Show comment
Hide comment
@contra

contra Oct 9, 2012

I see you added tests for event scope but I extended it a bit

test("event scope", function() {
    var module1 = Stapes.create();
    var module2 = Stapes.create();
    var firstDone = false;

    module1.on('eventscope', function(data, e) {
        ok(e.scope === module1, "Scope of event should be the emitting model");
    });

    module2.on('eventscope', function(data, e) {
        ok(e.scope === module2, "Scope of event should be the emitting model");
    });

    Stapes.on('eventscope', function(data, e) {
        if (firstDone) {
            ok(e.scope !== module1, "Scope of event from global Stapes object should not be the mixed with other models");
            ok(e.scope === module2, "Scope of event from global Stapes object should be the emitting model");
        } else {
            ok(e.scope === module1, "Scope of event from global Stapes object should be the emitting model");
            firstDone = true;
        }
    });

    Stapes.on('all', function(data, e) {
        if (e.type === "eventscope") {
            // Prevent other events from other tests getting here
            ok(e.scope === module1, "Scope event from on 'all' handler on Stapes.on should be emitting model");
        }
    });

    module1.emit('eventscope');
    module2.emit('eventscope');
});

I'm emitting eventscope twice and on the second one the scope is still set to module1 even though it is being emitted from module2.

contra commented Oct 9, 2012

I see you added tests for event scope but I extended it a bit

test("event scope", function() {
    var module1 = Stapes.create();
    var module2 = Stapes.create();
    var firstDone = false;

    module1.on('eventscope', function(data, e) {
        ok(e.scope === module1, "Scope of event should be the emitting model");
    });

    module2.on('eventscope', function(data, e) {
        ok(e.scope === module2, "Scope of event should be the emitting model");
    });

    Stapes.on('eventscope', function(data, e) {
        if (firstDone) {
            ok(e.scope !== module1, "Scope of event from global Stapes object should not be the mixed with other models");
            ok(e.scope === module2, "Scope of event from global Stapes object should be the emitting model");
        } else {
            ok(e.scope === module1, "Scope of event from global Stapes object should be the emitting model");
            firstDone = true;
        }
    });

    Stapes.on('all', function(data, e) {
        if (e.type === "eventscope") {
            // Prevent other events from other tests getting here
            ok(e.scope === module1, "Scope event from on 'all' handler on Stapes.on should be emitting model");
        }
    });

    module1.emit('eventscope');
    module2.emit('eventscope');
});

I'm emitting eventscope twice and on the second one the scope is still set to module1 even though it is being emitted from module2.

@contra

This comment has been minimized.

Show comment
Hide comment
@contra

contra Oct 9, 2012

It looks like the issue is https://github.com/hay/stapes/blob/master/stapes.js#L271 event.scope = module1 and this = module2 but it prefers event.scope over this. I think the issue is that when module1 gets called you are setting event.scope to the scope of module1 then when module2 gets called its using event.scope over this because it was previously set to this of module1. The problem can be solved by cloning the event object before modifying it

        emitEvents : function(type, data, explicitType, explicitGuid) {
            explicitType = explicitType || false;
            explicitGuid = explicitGuid || this._guid;

            util.each(_.eventHandlers[explicitGuid][type], function(event) {
                var scope = (event.scope) ? event.scope : this;
                var nevent = util.clone(event);
                if (explicitType) {
                    nevent.type = explicitType;
                }
                nevent.scope = scope;
                event.handler.call(scope, data, nevent);
            }, this);
        },

I won't bother you will a pull req because you'll probably want to reimpliment the tests/fix your own way

contra commented Oct 9, 2012

It looks like the issue is https://github.com/hay/stapes/blob/master/stapes.js#L271 event.scope = module1 and this = module2 but it prefers event.scope over this. I think the issue is that when module1 gets called you are setting event.scope to the scope of module1 then when module2 gets called its using event.scope over this because it was previously set to this of module1. The problem can be solved by cloning the event object before modifying it

        emitEvents : function(type, data, explicitType, explicitGuid) {
            explicitType = explicitType || false;
            explicitGuid = explicitGuid || this._guid;

            util.each(_.eventHandlers[explicitGuid][type], function(event) {
                var scope = (event.scope) ? event.scope : this;
                var nevent = util.clone(event);
                if (explicitType) {
                    nevent.type = explicitType;
                }
                nevent.scope = scope;
                event.handler.call(scope, data, nevent);
            }, this);
        },

I won't bother you will a pull req because you'll probably want to reimpliment the tests/fix your own way

@hay

This comment has been minimized.

Show comment
Hide comment
@hay

hay Oct 10, 2012

Owner

Thanks for expanding the test and finding the cause. I'll have a look and hopefully can fix it easily.

Owner

hay commented Oct 10, 2012

Thanks for expanding the test and finding the cause. I'll have a look and hopefully can fix it easily.

@hay

This comment has been minimized.

Show comment
Hide comment
@hay

hay Oct 14, 2012

Owner

This issue has been fixed in master. Thanks for your comments and test code!

Owner

hay commented Oct 14, 2012

This issue has been fixed in master. Thanks for your comments and test code!

@hay hay closed this Oct 14, 2012

hay added a commit that referenced this issue Oct 14, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment