postaljs / postal.js

JavaScript pub/sub library supporting advanced subscription features, and several helpful add-ons.

Home Page:http://ifandelse.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multiple subscriptions fired from single publish in version 1.0.10 and greater

DylanTusler opened this issue · comments

We were using postal 1.0.8 and updated to 1.0.11 and suddenly we were getting multiple subscription hits for a single publish. Working backwards, the issue appears to have been introduced at 1.0.10. and is present in 1.0.11 and 2.0.0 as well.

A single publish event is causing some of our subscribe methods to be invoked multiple times instead of once.

For the moment we've reverted to 1.0.9 which seems to be working fine.

busFactory.publish("commsstarted", {});

is causing:

busFactory.subscribe("commsstarted", function() { // snipped });

to be invoked twice.

There are two separate places in our codebase where the commsstarted event is subscribed. At the other location, the subscriber is only invoked once.

Another single publish event is causing one subscriber to be invoked twice and another to be invoked three times!

I looked at the diff between 1.0.9 and 1.0.10 and tried reverting the change where "this" was removed around line 609 of postal.js and this fixed our problem in 1.0.10. I think this is the change referred to as: + * Fixed issue 'this' context issue in postal.subscribe

Here's the change I made to get it working again:

        _.each( _.keys( cache ), function( cacheKey ) {
            if ( cacheKey.substr( 0, channelLen ) === subDef.channel ) {
                getCacher(
                    cacheKey.split( _config.cacheKeyDelimiter )[1],
                    this.cache,  // added this back here
                    cacheKey )( subDef );
            }
        }, this );  // and here

I've dug into this at length, and discovered that we were accidentally referencing the controller that sets up many of our event subscriptions twice on the index.html page. This wasn't a problem prior to 1.0.10, but due to the change mentioned above it became a problem.

After removing the duplicate reference to the controller, 1.0.10 and 1.0.11 now work with our site.

I'll leave this here in case anyone else has the same issue.

@DylanTusler thanks for following up to share what you found - glad you got it worked out.