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 sharded listener provider #595

Merged
merged 10 commits into from
Mar 7, 2018

Conversation

schnapster
Copy link
Contributor

@schnapster schnapster commented Jan 9, 2018

Pull Request Etiquette

There are several guidelines you should follow in order for your
Pull Request to be merged.

  • I have checked the PRs for upcoming features/bug fixes.
  • I have read the contributing guidelines a long time ago lol.

It is sometimes better to include more changes in a single commit.
If you find yourself having an overwhelming amount of commits, you
can rebase your branch.

Description

This PR adds a feature to the DefaultShardManager.
This will allow the ShardManager and ShardManagerBuilder to be provided with a function which provides listeners based on shard id.
This is helpful when the user wishes for an approach where each shard gets it's own listener object of a certain kind, instead of having a single central listener object of a certain kind for all shards.

ToDos

  • Figure out removal of listener provider including any potentially created listeners with it from built shard manager.

Input is welcome on how to write a convenience method on how to handle removing existing listeners created by a provider when the provider is removed from a shard manager.

*
* @return The {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder} instance. Useful for chaining.
*/
public DefaultShardManagerBuilder addShardedEventListenerProvider(final Function<Integer, Object> shardedListenerProvider)
Copy link
Member

Choose a reason for hiding this comment

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

Calling something on ShardManager a ShardedEventListener is pretty unnecessary.
Everything you add to a ShardManager is "sharded" so it should just be addEventListenerProvider

* @throws java.lang.IllegalArgumentException
* If the provided listener provider {@code null}.
*/
default void addShardedEventListeners(final Function<Integer, Object> shardedEventListenerProvider)
Copy link
Member

Choose a reason for hiding this comment

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

This is missing a removeEventListeners(IntPredicate)

Copy link
Member

Choose a reason for hiding this comment

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

Could we use IntFunction for these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is missing a removeEventListeners(IntPredicate)

Which removes all listeners from a specific shard, or other shards? I like the idea, and I'll gladly add that if you confirm. Just making sure, because we're also missing a method that removes the listeners provided by a listener provider from all shards.

@MinnDevelopment MinnDevelopment added this to the Release 3.6 milestone Feb 2, 2018
@MinnDevelopment
Copy link
Member

Requesting update on this

* @throws java.lang.IllegalArgumentException
* If the provided listener provider {@code null}.
*/
default void addEventListeners(final IntFunction<Object> eventListenerProvider)
Copy link
Member

@MinnDevelopment MinnDevelopment Feb 4, 2018

Choose a reason for hiding this comment

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

Can we also get the same as removeEventListeners(IntFunction<Collection<Object>> predicate)

@schnapster
Copy link
Contributor Author

default void removeEventListeners(IntFunction<Collection<Object>> predicate) clashes with default void removeEventListeners(IntFunction<Object> predicate), both methods have the same erasure. Do we want both? How to name them in that case? Or is there another possibility?

@MinnDevelopment
Copy link
Member

removeEventListeners(IntFunction<Object> predicate) is to remove the provider from the manager, isn't it?

* @throws java.lang.IllegalArgumentException
* If the provided shard filter is {@code null}.
*/
default void removeEventListeners(final IntPredicate shardFilter)
Copy link
Member

Choose a reason for hiding this comment

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

I don't like this method, I suggested something different on discord removeEventListeners(IntFunction<Collection<Object>>)

@schnapster
Copy link
Contributor Author

Sorry, I derped about what we talked over in Discord. This should look now more like it.

{
Checks.notNull(eventListenersProvider, "event listeners provider");
this.getShardCache().forEach(jda ->
jda.removeEventListener(eventListenersProvider.apply(jda.getShardInfo().getShardId()))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This does not work when an EventListenersProvider doesn't always return the same listener instance for the a shard id (unless a proper equals() method has been implemented)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also the method names are not consistent: There's removeEventListenersProvider, removeEventListenerProvider and removeEventListeners

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removeEventListenersProvider looks like a typo and should be removeEventListenerProvider

As for ShardManager#removeEventListeners, can you do a suggestion how it should be called?

This does not work when an EventListenersProvider doesn't always return the same listener instance for the a shard id (unless a proper equals() method has been implemented)

image
Getting mixed signals here.

@@ -342,6 +342,11 @@ public void addEventListeners(IntFunction<Object> eventListenerProvider) {
this.listenerProviders.add(eventListenerProvider);
}

@Override
public void removeEventListenersProvider(IntFunction<Object> eventListenerProvider) {
Copy link
Member

Choose a reason for hiding this comment

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

bracket on new line

* @throws java.lang.IllegalArgumentException
* If the provided listener provider is {@code null}.
*/
void removeEventListenerProvider(IntFunction<Object> eventListenerProvider);
Copy link
Member

Choose a reason for hiding this comment

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

Make this a nop default for backward compatibility

*
* @return The {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder} instance. Useful for chaining.
*/
public DefaultShardManagerBuilder removeEventListenerProvider(final IntFunction<Object> listenerProvider)
Copy link
Member

@MinnDevelopment MinnDevelopment Feb 10, 2018

Choose a reason for hiding this comment

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

Make this vararg, same for add Decided this is not needed

@MinnDevelopment MinnDevelopment added the status: completed has been completed but is not yet released label Mar 5, 2018
@MinnDevelopment
Copy link
Member

Sorry for all the delay on this, I'll try to get to this PR this week.

@schnapster schnapster changed the title [WIP] Add sharded listener provider Add sharded listener provider Mar 6, 2018
@schnapster
Copy link
Contributor Author

Can't blame you, forgot to update the title :)

this.getShardCache().forEach(jda ->
{
Object listener = eventListenerProvider.apply(jda.getShardInfo().getShardId());
Checks.notNull(listener, "listener");
Copy link
Member

Choose a reason for hiding this comment

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

Could we skip on null?
like if (listener != null) jda.addEventListener(listener);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

@MinnDevelopment MinnDevelopment left a comment

Choose a reason for hiding this comment

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

lgtm

@MinnDevelopment MinnDevelopment merged commit 0befe74 into discord-jda:development Mar 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: completed has been completed but is not yet released type: enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants