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

.publishSync() to return the return of subscribers #92

Closed
m3talstorm opened this issue May 6, 2016 · 4 comments
Closed

.publishSync() to return the return of subscribers #92

m3talstorm opened this issue May 6, 2016 · 4 comments

Comments

@m3talstorm
Copy link

m3talstorm commented May 6, 2016

Would it be in the spirit of PubSub to add the ability to capture the return value of subscribers when doing synchronous publishes? Like so:

PubSub.subscribe('event', function(data) {
    return 1;
});

PubSub.subscribe('event', function(data) {
    return 2;
});

PubSub.subscribe('event', function(data) {
    return 3;
});

PubSub.subscribe('event.something', function(data) {
    return 4;
});

var results = PubSub.publishSync('event.something', { data: 1 })

> results
> [1, 2, 3, 4]

This does have some use cases, such as in a plugin/hook architecture. A way of doing this currently would be for each subscriber to touch a global variable, which doesn't seem all that nice.

I would suggest

        if ( sync === true ){
            deliver();

to change to:

        if ( sync === true ){
            return deliver();

and then each callSubscriber inside of deliverMessage to append to a list and return that, then subsequently deliverMessage to concatenate the lists. Returning an object may be better, with the keys denoting the message name. Eg:

> results
> {
    event: [1, 2, 3],
    event.something: [4],
}

The signature of .publishSync() would change slightly, but code doing a truthy/falsy check on its current return value (true/false) should still continue to work.

Thoughts?

@8427003
Copy link

8427003 commented Oct 25, 2016

+1

@JasonTypesCodes
Copy link

I know this is an old thread but my .02 on this is if the publisher (or anyone else) is interested in the work a subscriber is doing, that subscriber should publish on a different topic (event) with its results.

@mroderick
Copy link
Owner

I agree with @VoltiSubito's approach to solving this challenge.

@m3talstorm
Copy link
Author

Seems like the this defeats the purpose of having a synchronous function if that value is returned asynchronously.

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

4 participants