Skip to content
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

listen example is incorrect #170

Closed
ronag opened this issue Jul 6, 2016 · 8 comments
Closed

listen example is incorrect #170

ronag opened this issue Jul 6, 2016 · 8 comments
Milestone

Comments

@ronag
Copy link

ronag commented Jul 6, 2016

The following example doesn't work as expected since isSubscribed would never be false since the listener itself has the last reference...

var raceHorseRecords = {};

client.record.listen( 'raceHorse/.*', function( match, isSubscribed ) {
    /*
         + match = 'raceHorse/fast-betty'
         + isSubscribed = true
     */
    var horseName = match.split( '/' )[ 1 ],
        updateRecord = function( data ){
            raceHorseRecords[ match ].set( data );
        };

    if( isSubscribed ) {
        raceHorseRecords[ match ] = client.record.getRecord( match );

        // assuming we have a raceHorseDataProvider class
        raceHorseDataProvider.subscribe( horseName, updateRecord );
    } else {
        raceHorseRecords[ match ].discard();
        delete raceHorseRecords[ match ];
        raceHorseDataProvider.unsubscribe( horseName, updateRecord );
    }
});

// This function will now be called whenever a client requests a record
// with a matching name, e.g.
client.record.getRecord( 'raceHorse/fast-betty' );
@ronag
Copy link
Author

ronag commented Jul 6, 2016

See, #155

@ronag
Copy link
Author

ronag commented Jul 15, 2016

@yasserf: Any feedback on this?

@ronag
Copy link
Author

ronag commented Jul 15, 2016

I think isSubscribed should be replaced with numSubscribers. That way one can get around the issue by detecting numSubscribers === 1 in case the listener has its own reference.

@ronag
Copy link
Author

ronag commented Aug 26, 2016

@yasserf: Please remind me how this is resolved with the new listen functionality?

@yasserf
Copy link
Contributor

yasserf commented Aug 26, 2016

The publisher will be told the subscription was discarded even if the publisher is also the subscriber but the last one in the cluster. This means a publisher should not normally depend on subscribing to something listening in the same client outside of the listen callback. It will work, but with known side effects.

@ronag
Copy link
Author

ronag commented Aug 26, 2016

@yasserf: That just made my head spin... so it will work but not really?

@ronag
Copy link
Author

ronag commented Aug 26, 2016

Ah, so it will receive isSubscribed=false even if it is subscribing itself to the record. Essentially a listener will not be counted in the subscriber count. That sounds totally reasonable. What are these 'known side effects'?

@yasserf
Copy link
Contributor

yasserf commented Aug 27, 2016

The provider is considered a subscriber if it subscribes to the record outside of the listen function, but if someone else subscribes and unsubscribes that provider will get a subscription stopped, even though it is still subscribed outside of the listen. That's mainly done to reduce operations on hot paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants