101 lines
3.6 KiB
Java
101 lines
3.6 KiB
Java
package com.google.firebase.components;
|
|
|
|
import com.google.firebase.events.Event;
|
|
import com.google.firebase.events.EventHandler;
|
|
import com.google.firebase.events.Publisher;
|
|
import com.google.firebase.events.Subscriber;
|
|
import java.util.ArrayDeque;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.Map;
|
|
import java.util.Queue;
|
|
import java.util.Set;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.Executor;
|
|
|
|
/* loaded from: classes.dex */
|
|
class EventBus implements Subscriber, Publisher {
|
|
private final Executor defaultExecutor;
|
|
private final Map<Class<?>, ConcurrentHashMap<EventHandler<Object>, Executor>> handlerMap = new HashMap();
|
|
private Queue<Event<?>> pendingEvents = new ArrayDeque();
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public EventBus(Executor executor) {
|
|
this.defaultExecutor = executor;
|
|
}
|
|
|
|
public void publish(final Event<?> event) {
|
|
Preconditions.checkNotNull(event);
|
|
synchronized (this) {
|
|
Queue<Event<?>> queue = this.pendingEvents;
|
|
if (queue != null) {
|
|
queue.add(event);
|
|
return;
|
|
}
|
|
for (final Map.Entry<EventHandler<Object>, Executor> entry : getHandlers(event)) {
|
|
entry.getValue().execute(new Runnable(entry, event) { // from class: com.google.firebase.components.EventBus$$ExternalSyntheticLambda0
|
|
public final Map.Entry f$0;
|
|
public final Event f$1;
|
|
|
|
@Override // java.lang.Runnable
|
|
public final void run() {
|
|
((EventHandler) this.f$0.getKey()).handle(this.f$1);
|
|
}
|
|
|
|
{
|
|
this.f$0 = entry;
|
|
this.f$1 = event;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
private Set<Map.Entry<EventHandler<Object>, Executor>> getHandlers(Event<?> event) {
|
|
Set<Map.Entry<EventHandler<Object>, Executor>> emptySet;
|
|
synchronized (this) {
|
|
ConcurrentHashMap<EventHandler<Object>, Executor> concurrentHashMap = this.handlerMap.get(event.getType());
|
|
emptySet = concurrentHashMap == null ? Collections.emptySet() : concurrentHashMap.entrySet();
|
|
}
|
|
return emptySet;
|
|
}
|
|
|
|
@Override // com.google.firebase.events.Subscriber
|
|
public <T> void subscribe(Class<T> cls, Executor executor, EventHandler<? super T> eventHandler) {
|
|
synchronized (this) {
|
|
Preconditions.checkNotNull(cls);
|
|
Preconditions.checkNotNull(eventHandler);
|
|
Preconditions.checkNotNull(executor);
|
|
if (!this.handlerMap.containsKey(cls)) {
|
|
this.handlerMap.put(cls, new ConcurrentHashMap<>());
|
|
}
|
|
this.handlerMap.get(cls).put(eventHandler, executor);
|
|
}
|
|
}
|
|
|
|
@Override // com.google.firebase.events.Subscriber
|
|
public <T> void subscribe(Class<T> cls, EventHandler<? super T> eventHandler) {
|
|
subscribe(cls, this.defaultExecutor, eventHandler);
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public void enablePublishingAndFlushPending() {
|
|
Queue<Event<?>> queue;
|
|
synchronized (this) {
|
|
queue = this.pendingEvents;
|
|
if (queue != null) {
|
|
this.pendingEvents = null;
|
|
} else {
|
|
queue = null;
|
|
}
|
|
}
|
|
if (queue != null) {
|
|
Iterator<Event<?>> it = queue.iterator();
|
|
while (it.hasNext()) {
|
|
publish(it.next());
|
|
}
|
|
}
|
|
}
|
|
}
|