mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-27 17:00:14 +08:00
Fix ListenerHost: inappropriate receiver for invoke for Java methods. (#499)
* Fix the abnormal behavior of ListenerHost. * Rename test unit name
This commit is contained in:
parent
f8eed41d62
commit
2161b67775
@ -8,33 +8,33 @@
|
||||
|
||||
package net.mamoe.mirai.javatest;
|
||||
|
||||
import kotlin.coroutines.CoroutineContext;
|
||||
import kotlin.coroutines.EmptyCoroutineContext;
|
||||
import kotlinx.coroutines.CoroutineScope;
|
||||
import kotlinx.coroutines.CoroutineScopeKt;
|
||||
import net.mamoe.mirai.event.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class SimpleListenerHostTest {
|
||||
@Test
|
||||
public void test() {
|
||||
public void testJavaSimpleListenerHostWork() {
|
||||
AtomicBoolean called = new AtomicBoolean();
|
||||
final SimpleListenerHost host = new SimpleListenerHost() {
|
||||
@EventHandler
|
||||
public void testListen(
|
||||
AbstractEvent event
|
||||
) {
|
||||
System.out.println(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleException(@NotNull CoroutineContext context, @NotNull Throwable exception) {
|
||||
exception.printStackTrace();
|
||||
called.set(true);
|
||||
}
|
||||
};
|
||||
CoroutineScope scope = CoroutineScopeKt.CoroutineScope(EmptyCoroutineContext.INSTANCE);
|
||||
Events.registerEvents(scope, host);
|
||||
EventKt.broadcast(new AbstractEvent() {
|
||||
});
|
||||
if (!called.get()) {
|
||||
throw new AssertionError("JavaTest: SimpleListenerHost Failed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -161,11 +161,11 @@ internal fun Method.registerEvent(
|
||||
if (annotation.ignoreCancelled) {
|
||||
if ((this as? CancellableEvent)?.isCancelled != true) {
|
||||
withContext(Dispatchers.IO) {
|
||||
this@registerEvent.invoke(owner, this)
|
||||
this@registerEvent.invoke(owner, this@subscribeAlways)
|
||||
}
|
||||
}
|
||||
} else withContext(Dispatchers.IO) {
|
||||
this@registerEvent.invoke(owner, this)
|
||||
this@registerEvent.invoke(owner, this@subscribeAlways)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -179,11 +179,11 @@ internal fun Method.registerEvent(
|
||||
if (annotation.ignoreCancelled) {
|
||||
if ((this as? CancellableEvent)?.isCancelled != true) {
|
||||
withContext(Dispatchers.IO) {
|
||||
this@registerEvent.invoke(owner, this) as ListeningStatus
|
||||
this@registerEvent.invoke(owner, this@subscribe) as ListeningStatus
|
||||
}
|
||||
} else ListeningStatus.LISTENING
|
||||
} else withContext(Dispatchers.IO) {
|
||||
this@registerEvent.invoke(owner, this) as ListeningStatus
|
||||
this@registerEvent.invoke(owner, this@subscribe) as ListeningStatus
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user