-
-
Notifications
You must be signed in to change notification settings - Fork 734
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
Add sharded listener provider #595
Conversation
* | ||
* @return The {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder} instance. Useful for chaining. | ||
*/ | ||
public DefaultShardManagerBuilder addShardedEventListenerProvider(final Function<Integer, Object> shardedListenerProvider) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Requesting update on this |
* @throws java.lang.IllegalArgumentException | ||
* If the provided listener provider {@code null}. | ||
*/ | ||
default void addEventListeners(final IntFunction<Object> eventListenerProvider) |
There was a problem hiding this comment.
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)
|
|
* @throws java.lang.IllegalArgumentException | ||
* If the provided shard filter is {@code null}. | ||
*/ | ||
default void removeEventListeners(final IntPredicate shardFilter) |
There was a problem hiding this comment.
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>>)
…ners by shard ids
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())) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
@@ -342,6 +342,11 @@ public void addEventListeners(IntFunction<Object> eventListenerProvider) { | |||
this.listenerProviders.add(eventListenerProvider); | |||
} | |||
|
|||
@Override | |||
public void removeEventListenersProvider(IntFunction<Object> eventListenerProvider) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
Sorry for all the delay on this, I'll try to get to this PR this week. |
Can't blame you, forgot to update the title :) |
this.getShardCache().forEach(jda -> | ||
{ | ||
Object listener = eventListenerProvider.apply(jda.getShardInfo().getShardId()); | ||
Checks.notNull(listener, "listener"); |
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Pull Request Etiquette
There are several guidelines you should follow in order for your
Pull Request to be merged.
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
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.