CMDAPIBetterCaching #39
@ -50,4 +50,13 @@ public interface AbstractTypeMapper<K, T> extends AbstractValidator<K, T> {
|
||||
default Collection<String> tabCompletes(K sender, PreviousArguments previousArguments, String s) {
|
||||
return tabCompletes(sender, previousArguments.userArgs, s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize the cache key by sender and user provided argument. <br>
|
||||
* Note: The CommandSender can be null! <br>
|
||||
* Returning null and the empty string are equivalent.
|
||||
*/
|
||||
default String normalize(K sender, String s) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
@ -165,7 +165,7 @@ class CommandPart<T> {
|
||||
}
|
||||
|
||||
private Collection<String> tabCompletes(T sender, String[] args, List<Object> mappedArgs, int startIndex) {
|
||||
return TabCompletionCache.tabComplete(sender, typeMapper, command, () -> {
|
||||
return TabCompletionCache.tabComplete(sender, args[startIndex], (AbstractTypeMapper<Object, ?>) typeMapper, command, () -> {
|
||||
try {
|
||||
return typeMapper.tabCompletes(sender, new PreviousArguments(Arrays.copyOf(args, startIndex), mappedArgs.toArray()), args[startIndex]);
|
||||
} catch (Throwable e) {
|
||||
|
@ -51,6 +51,7 @@ public class TabCompletionCache {
|
||||
@AllArgsConstructor
|
||||
private static class Key {
|
||||
private Object sender;
|
||||
private String arg;
|
||||
private AbstractTypeMapper<?, ?> typeMapper;
|
||||
}
|
||||
|
||||
@ -61,9 +62,11 @@ public class TabCompletionCache {
|
||||
private Collection<String> tabCompletions;
|
||||
}
|
||||
|
||||
Collection<String> tabComplete(Object sender, AbstractTypeMapper<?, ?> typeMapper, AbstractSWCommand<?> command, Supplier<Collection<String>> tabCompleteSupplier) {
|
||||
Collection<String> tabComplete(Object sender, String arg, AbstractTypeMapper<Object, ?> typeMapper, AbstractSWCommand<?> command, Supplier<Collection<String>> tabCompleteSupplier) {
|
||||
if (!cached.contains(typeMapper)) return tabCompleteSupplier.get();
|
||||
Key key = global.contains(typeMapper) ? new Key(null, typeMapper) : new Key(sender, typeMapper);
|
||||
String normalizedArg = typeMapper.normalize(sender, arg);
|
||||
if (normalizedArg == null) normalizedArg = "";
|
||||
Key key = global.contains(typeMapper) ? new Key(null, normalizedArg, typeMapper) : new Key(sender, normalizedArg, typeMapper);
|
||||
TabCompletions tabCompletions = tabCompletionCache.computeIfAbsent(key, ignore -> {
|
||||
return new TabCompletions(command, System.currentTimeMillis(), tabCompleteSupplier.get());
|
||||
});
|
||||
|
@ -41,7 +41,6 @@ public class CacheCommand extends TestSWCommand {
|
||||
@Cached
|
||||
@Mapper(value = "int", local = true)
|
||||
public AbstractTypeMapper<String, Integer> typeMapper() {
|
||||
System.out.println("TypeMapper register");
|
||||
return new TestTypeMapper<Integer>() {
|
||||
@Override
|
||||
public Integer map(String sender, PreviousArguments previousArguments, String s) {
|
||||
@ -52,6 +51,11 @@ public class CacheCommand extends TestSWCommand {
|
||||
public Collection<String> tabCompletes(String sender, PreviousArguments previousArguments, String s) {
|
||||
return Arrays.asList(count.getAndIncrement() + "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String normalize(String sender, String s) {
|
||||
return "";
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -36,6 +36,14 @@ public class CacheCommandTest {
|
||||
assertThat(tabCompletions1, is(equalTo(tabCompletions2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCachingWithDifferentMessages() {
|
||||
CacheCommand cmd = new CacheCommand();
|
||||
List<String> tabCompletions1 = cmd.tabComplete("test", "", new String[]{""});
|
||||
List<String> tabCompletions2 = cmd.tabComplete("test", "", new String[]{"0"});
|
||||
assertThat(tabCompletions1, is(equalTo(tabCompletions2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCachingWithDifferentSenders() {
|
||||
CacheCommand cmd = new CacheCommand();
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren