Skip to content

Instantly share code, notes, and snippets.

@whitfin
Created January 11, 2018 22:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whitfin/ea72129043fd31ec427e4c3d88255480 to your computer and use it in GitHub Desktop.
Save whitfin/ea72129043fd31ec427e4c3d88255480 to your computer and use it in GitHub Desktop.
package com.appcelerator.binding;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Broadcaster {
private static final Map<String, List<Listener>> LISTENERS = new ConcurrentHashMap<>();
public synchronized static void register(String event, Listener listener) {
List<Listener> listeners = LISTENERS.get(event);
if (listeners == null) {
listeners = Collections.emptyList();
LISTENERS.put(event, listeners);
}
listeners.add(listener);
}
public static void send(String event, Message message) {
List<Listener> listeners = LISTENERS.get(event);
if (listeners == null) {
return;
}
for (Listener listener : listeners) {
listener.onMessage(event, message);
}
}
public interface Listener {
void onMessage(String event, Message message);
}
public class Message {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment