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

Add the topic to the callback method before executing the callback #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dcneiner
Copy link
Contributor

This allows the developer to access the current topic being handled by the callback without breaking existing implementations of amplify pub/sub. The example in the readme shows how to access this new property.

Also update the readme to clarify that multiple topics can be subscribed in a single subscribe call.

This allows the developer to access the current topic being handled
by the callback without breaking existing implementations of
amplify pub/sub. The example in the readme shows how to access this
new property.

Also update the readme to clarify that multiple topics can be
subscribed in a single subscribe call.
* [`context`]: What `this` will be when the callback is invoked.
* `callback`: Function to invoke when the message is published.
* [`priority`]: Priority relative to other subscriptions for the same message. Lower values have higher priority. Default is 10.

> Returning `false` from a subscription will prevent any additional subscriptions from being invoked and will cause `amplify.publish` to return `false`.

You can find out which topic is currently being processed by accessing the `topic` property on your callback method. This is especially helpful when dealing with multiple topics in a single subscription. *Important: `topic` is only available for the duration of the callback function’s execution. If you defer/throttle or otherwise delay accessing the topic, it may no longer be the correct topic.* The easiest way to access this property is to use a named callback method.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/processed/published/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use ’ anywhere else; should probably just use a regular quote.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes made as requested. Good point about the ’ it was my obsessive compulsive nature kicking in!

@scottgonzalez
Copy link
Contributor

Other than the two tiny readme comments, this looks good to me. I'll leave it open for a bit to allow discussion.

@ifandelse
Copy link

Glad to see this going in. I think this is a win for amplify users!

@RedWolves
Copy link
Contributor

+1 Doug and I had a discussion about this earlier today. My suggestion was to add topic to amplify so amplify.topic would be how you'd access it.

@scottgonzalez
Copy link
Contributor

@RedWolves I like that idea, probably more than the one in this PR.

@dcneiner
Copy link
Contributor Author

I am in favor of that method as well. Should it be amplify.topic or amplify.currentTopic? Also, @RedWolves do you want to submit a separate pull request – or should I update this one to reflect using amplify instead of the callback?

@dcneiner
Copy link
Contributor Author

To clarify, I think using a global variable like this emphasizes the transitory nature of being able to access it. "If you don't reference it during the callback, it won't be there when you need it". I think that would be easier to understand with this approach vs. local callback variables.

@RedWolves
Copy link
Contributor

Yeah I'll work on a pull request tonight and submit it. do we want to do .topic or .currentTopic?

@dcneiner
Copy link
Contributor Author

I like both for different reasons. topic is shorter. currentTopic might be clearer.

I vote for topic.

@scottgonzalez
Copy link
Contributor

I'm fine with either.

@jdsharp
Copy link
Contributor

jdsharp commented Apr 23, 2012

I'm not a big fan of amplify.topic. It doesn't sit well with me that you'd have to access an API outside of the current context to determine context. The topic is very specific to the handler being triggered and should be associated as much. (Unless I'm misunderstanding)

@RedWolves
Copy link
Contributor

I put this together the other night https://gist.github.com/8af1ca8a678bcbe4e19c I have to agree with JD ... now that I see it implemented I am not a fan of the approach. Personally, I am still in favor of my first pull request (#47) with attaching topic to the end of args. Seems like any issues that arise could be defeated with documentation.

@jakerella
Copy link
Contributor

Since this PR is well out of date, and given theat we no longer have docs in the README, I'm inclined to port this one-line change to a new PR, with the test, and open a separate issue on the documentation site.

Any concerns there @dcneiner?

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

Successfully merging this pull request may close these issues.

6 participants