add comments
This commit is contained in:
@@ -7,6 +7,12 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
|
||||
/**
|
||||
* The event emitter which is ported from the JavaScript module.
|
||||
*
|
||||
* @see <a href="https://github.com/component/emitter">https://github.com/component/emitter</a>
|
||||
*/
|
||||
public class Emitter {
|
||||
|
||||
private ConcurrentMap<String, ConcurrentLinkedQueue<Listener>> callbacks
|
||||
@@ -15,6 +21,12 @@ public class Emitter {
|
||||
private ConcurrentMap<Listener, Listener> onceCallbacks = new ConcurrentHashMap<Listener, Listener>();
|
||||
|
||||
|
||||
/**
|
||||
* Listens on the event.
|
||||
* @param event event name.
|
||||
* @param fn
|
||||
* @return a reference to to this object.
|
||||
*/
|
||||
public Emitter on(String event, Listener fn) {
|
||||
ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.get(event);
|
||||
if (callbacks == null) {
|
||||
@@ -28,6 +40,13 @@ public class Emitter {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a one time listener for the event.
|
||||
*
|
||||
* @param event
|
||||
* @param fn
|
||||
* @return a reference to to this object.
|
||||
*/
|
||||
public Emitter once(final String event, final Listener fn) {
|
||||
Listener on = new Listener() {
|
||||
@Override
|
||||
@@ -42,12 +61,23 @@ public class Emitter {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all registered listeners.
|
||||
*
|
||||
* @return a reference to to this object.
|
||||
*/
|
||||
public Emitter off() {
|
||||
this.callbacks.clear();
|
||||
this.onceCallbacks.clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all listeners of the specified event.
|
||||
*
|
||||
* @param event
|
||||
* @return a reference to to this object.
|
||||
*/
|
||||
public Emitter off(String event) {
|
||||
ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.remove(event);
|
||||
if (callbacks != null) {
|
||||
@@ -58,6 +88,13 @@ public class Emitter {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the listener.
|
||||
*
|
||||
* @param event
|
||||
* @param fn
|
||||
* @return a reference to to this object.
|
||||
*/
|
||||
public Emitter off(String event, Listener fn) {
|
||||
ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.get(event);
|
||||
if (callbacks != null) {
|
||||
@@ -67,6 +104,13 @@ public class Emitter {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes each of listeners with the given args.
|
||||
*
|
||||
* @param event
|
||||
* @param args
|
||||
* @return a reference to to this object.
|
||||
*/
|
||||
public Emitter emit(String event, Object... args) {
|
||||
ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.get(event);
|
||||
if (callbacks != null) {
|
||||
@@ -78,12 +122,24 @@ public class Emitter {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of listeners for the specified event.
|
||||
*
|
||||
* @param event
|
||||
* @return a reference to to this object.
|
||||
*/
|
||||
public List<Listener> listeners(String event) {
|
||||
ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.get(event);
|
||||
return callbacks != null ?
|
||||
new ArrayList<Listener>(callbacks) : new ArrayList<Listener>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this emitter has listeners for the specified event.
|
||||
*
|
||||
* @param event
|
||||
* @return a reference to to this object.
|
||||
*/
|
||||
public boolean hasListeners(String event) {
|
||||
return !this.listeners(event).isEmpty();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user