Created
January 11, 2018 22:28
-
-
Save whitfin/ea72129043fd31ec427e4c3d88255480 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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