Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhoenixOrigin committed May 13, 2024
1 parent 3f59417 commit 0b99b01
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 60 deletions.
38 changes: 6 additions & 32 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,17 @@ It creates "channels" which can be fired events upon. You can assign each callba
# Usage [![Javadoc](https://img.shields.io/badge/JavaDoc-Online-green)](https://phoenixorigin.github.io/EventEmitter/javadoc/)

## Add library
### Maven
```xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>


<dependency>
<groupId>com.github.phoenix</groupId>
<artifactId>eventemitter</artifactId>
<version>LATEST</version>
</dependency>
```
### Gradle Groovy
### Gradle
#### Groovy
```groovy
repositories {
maven {url 'https://jitpack.io'}
}
dependencies {
implementation "com.github.phoenix:eventemitter:+"
implementation 'io.github.phoenixorigin:eventemitter:1.0.0'
}
```
### Gradle Kotlin
#### Kotlin
```kotlin
repositories {
mavenCentral()
maven(url = "https://jitpack.io")
}
dependencies {
implementation("com.github.phoenix:eventemitter:+")
implementation("io.github.phoenixorigin:eventemitter:1.0.0")
}
```
## Using it
Expand Down Expand Up @@ -101,8 +79,4 @@ String eventHandlerUUID = eventEmitter.on("channel", ((channel, event) -> {
}));
// Unregister the instance
eventEmitter.off(eventHandlerUUID);
```
# Contributing
This is a one time project thing that I am probably never gonna look at again so if you want to make a change just make a PR and I will probably merge it
# Credits
Idea + some of the code- https://github.com/jafarlihi/eemit
```
1 change: 1 addition & 0 deletions src/main/java/net/phoenix/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/**
* Event class. All custom events should extend this.
*/
@SuppressWarnings("unused")
public abstract class Event {
void cancel() {cancelled = true;}
boolean cancelled = false;
Expand Down
80 changes: 52 additions & 28 deletions src/main/java/net/phoenix/EventEmitter.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package net.phoenix;

import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
* Standard event emitter class
* @param <T> Event type. Either should be a class that extends Event OR a generic type (EventEmitter&lt;? extends Event&gt;)
*/
@SuppressWarnings("unused")
public class EventEmitter<T extends Event> {

private final HashMap<String, LinkedList<Pair<String, Callback<T>>>> callbacks = new HashMap<>();
Expand Down Expand Up @@ -50,17 +53,44 @@ public int getValue() {
* @param object Instance of event to send
* @throws IllegalArgumentException Throws exception if the event passed is different to the EventHandler type
*/
public void emit(String channel, Event object) {
public void emit(String channel, T object) {
if (!isMatchingEventType(object)) {
throw new IllegalArgumentException("Object type does not match the EventEmitter type parameter.");
}
List<Pair<String, Callback<T>> > sortedCallbacks = getSortedCallbacks(channel);
for (Pair<String, Callback<T>> callbackPair : sortedCallbacks) {
if (object.cancelled) return;
callbackPair.getSecond().call(channel, (T) object);
callbackPair.second().call(channel, object);
}
}

/**
* Awaits for a method under the global channel
*
* @throws IllegalArgumentException Throws exception if the event passed is different to the EventHandler type
*/
public String on(Callback<T> callback) {
return on("global", callback);
}

/**
* Awaits for a method under the global channel
*
* @throws IllegalArgumentException Throws exception if the event passed is different to the EventHandler type
*/
public String on(Callback<T> callback, Priority priority) {
return on("global", callback, priority);
}

/**
* Awaits for a method under the global channel
*
* @throws IllegalArgumentException Throws exception if the event passed is different to the EventHandler type
*/
public void emit(T object) {
emit("global", object);
}


/**
* Register an event handler with default priority of LOW
Expand Down Expand Up @@ -99,6 +129,22 @@ public String on(String channel, Callback<T> callback, Priority priority) {
return uuid;
}

/**
* Awaits for a method under the global channel
*
* @throws InterruptedException Throws exception if the thread is interrupted
*/
public T await(String channel) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<T> e = new AtomicReference<>();
this.on(channel, (ch, event) -> {
latch.countDown();
e.set(event);
}, Priority.TOP);
latch.await();
return e.get();
}


/**
* Disable a eventHandler
Expand All @@ -109,7 +155,7 @@ public void off(String uuid) {
for (String channel : callbacks.keySet()) {
LinkedList<Pair<String, Callback<T>> > callbackList = callbacks.get(channel);
if (callbackList != null) {
callbackList.removeIf(pair -> pair.getFirst().equals(uuid));
callbackList.removeIf(pair -> pair.first().equals(uuid));
}
}
}
Expand All @@ -123,7 +169,7 @@ private List<Pair<String, Callback<T>>> getSortedCallbacks(String channel) {
List<Pair<String, Callback<T>>> callbackPairList = callbacks.get(channel);
if (callbackPairList != null) {
return callbackPairList.stream()
.sorted((pair1, pair2) -> Integer.compare(pair2.getPriority().getValue(), pair1.getPriority().getValue()))
.sorted((pair1, pair2) -> Integer.compare(pair2.priority().getValue(), pair1.priority().getValue()))
.collect(Collectors.toList());
}
return Collections.emptyList();
Expand All @@ -141,31 +187,9 @@ private boolean isMatchingEventType(Event object) {

/**
* Stores pairs of code
*
* @param <T1> UUID of callback
* @param <T2> Callback
*/
static class Pair<T1, T2> {

private T1 first;
private T2 second;
private Priority priority;

public Pair(T1 first, T2 second, Priority priority) {
this.first = first;
this.second = second;
this.priority = priority;
}

public T1 getFirst() {
return first;
}

public T2 getSecond() {
return second;
}

public Priority getPriority() {
return priority;
}
}
record Pair<T1, T2>(T1 first, T2 second, Priority priority) { }
}

0 comments on commit 0b99b01

Please sign in to comment.