Skip to content

Commit 83b98df

Browse files
authored
Merge pull request #1 from swnck/feature/async
Feature/async
2 parents a915112 + 192c16f commit 83b98df

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void onListenYourFavoriteEvent(YourFavoriteEvent event) {
164164

165165
## Future Plans 🛌
166166

167-
- [ ] Asynchronous events
167+
- [x] Asynchronous events
168168
- [x] Event cancellation
169169
- [x] Event listener registration
170170
- [x] Event listener deregistration

src/main/java/at/jkvn/eventlib/EventLib.java

+10-15
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,26 @@ public class EventLib {
3232

3333
@SneakyThrows
3434
public static void call(Event event) {
35-
if (isAutomatic()) {
36-
invokeMethods(getMethods(), event, false);
37-
return;
38-
}
39-
40-
(new ArrayList<>(listeners)).forEach(listener -> {
41-
invokeMethods(Arrays.stream(listener.getClass().getMethods()).toList(), event, false);
42-
});
35+
callEvent(event);
4336
}
4437

4538
@SneakyThrows
4639
public static void callAsync(Event event) {
40+
executor.execute(() -> callEvent(event));
41+
}
42+
43+
private static void callEvent(Event event) {
4744
if (isAutomatic()) {
48-
invokeMethods(getMethods(), event, true);
45+
invokeMethods(getMethods(), event);
4946
return;
5047
}
5148

5249
(new ArrayList<>(listeners)).forEach(listener -> {
53-
invokeMethods(Arrays.stream(listener.getClass().getMethods()).toList(), event, true);
50+
invokeMethods(Arrays.stream(listener.getClass().getMethods()).toList(), event);
5451
});
5552
}
5653

57-
private static void invokeMethods(List<Method> methods, Event event, boolean async) {
54+
private static void invokeMethods(List<Method> methods, Event event) {
5855
methods.stream()
5956
.filter(method -> method.getParameterCount() == 1
6057
&& method.getParameterTypes()[0].isAssignableFrom(event.getClass()))
@@ -63,11 +60,9 @@ private static void invokeMethods(List<Method> methods, Event event, boolean asy
6360
return priority != null ? priority.value().ordinal() : EventPriority.NORMAL.ordinal();
6461
}))
6562
.forEach(method -> {
66-
if (async) {
67-
executor.execute(() -> invokeSafely(method, event));
68-
return;
63+
if (!event.isCancelled()) {
64+
invokeSafely(method, event);
6965
}
70-
invokeSafely(method, event);
7166
});
7267
}
7368

0 commit comments

Comments
 (0)