2021-06-27 20:44:35 +00:00
package com.discord.utilities.lazy.subscriptions ;
import android.content.Context ;
import com.discord.utilities.rx.ObservableExtensionsKt ;
import d0.t.n0 ;
import d0.z.d.m ;
import j0.p.a ;
import java.util.HashMap ;
import java.util.Iterator ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
import java.util.concurrent.TimeUnit ;
import kotlin.Unit ;
import kotlin.jvm.functions.Function0 ;
import kotlin.jvm.functions.Function1 ;
import kotlin.jvm.functions.Function3 ;
import kotlin.jvm.internal.DefaultConstructorMarker ;
import rx.Observable ;
import rx.Scheduler ;
import rx.Subscription ;
/* compiled from: GuildMemberSubscriptionsManager.kt */
public final class GuildMemberSubscriptionsManager {
private final Scheduler delayScheduler ;
private Subscription delayedFlushSubscription ;
private final Function3 < Long , Set < Long > , Boolean , Unit > onChange ;
private final HashMap < Long , Map < Long , Integer > > pendingUnsubscriptions ;
private final Function1 < Function0 < Unit > , Unit > requestFlushUnsubscriptions ;
private final HashMap < Long , Map < Long , Integer > > subscriptions ;
/* JADX DEBUG: Multi-variable search result rejected for r2v0, resolved type: kotlin.jvm.functions.Function3<? super java.lang.Long, ? super java.util.Set<java.lang.Long>, ? super java.lang.Boolean, kotlin.Unit> */
/* JADX DEBUG: Multi-variable search result rejected for r3v0, resolved type: kotlin.jvm.functions.Function1<? super kotlin.jvm.functions.Function0<kotlin.Unit>, kotlin.Unit> */
/* JADX WARN: Multi-variable type inference failed */
public GuildMemberSubscriptionsManager ( Function3 < ? super Long , ? super Set < Long > , ? super Boolean , Unit > function3 , Function1 < ? super Function0 < Unit > , Unit > function1 , Scheduler scheduler ) {
m . checkNotNullParameter ( function3 , " onChange " ) ;
m . checkNotNullParameter ( function1 , " requestFlushUnsubscriptions " ) ;
m . checkNotNullParameter ( scheduler , " delayScheduler " ) ;
this . onChange = function3 ;
this . requestFlushUnsubscriptions = function1 ;
this . delayScheduler = scheduler ;
this . subscriptions = new HashMap < > ( ) ;
this . pendingUnsubscriptions = new HashMap < > ( ) ;
}
/* JADX WARNING: Illegal instructions before constructor call */
public /* synthetic */ GuildMemberSubscriptionsManager ( Function3 function3 , Function1 function1 , Scheduler scheduler , int i , DefaultConstructorMarker defaultConstructorMarker ) {
this ( function3 , function1 , scheduler ) ;
if ( ( i & 4 ) ! = 0 ) {
scheduler = a . a ( ) ;
m . checkNotNullExpressionValue ( scheduler , " Schedulers.computation() " ) ;
}
}
public static final /* synthetic */ void access$flushUnsubscriptions ( GuildMemberSubscriptionsManager guildMemberSubscriptionsManager ) {
guildMemberSubscriptionsManager . flushUnsubscriptions ( ) ;
}
public static final /* synthetic */ Subscription access$getDelayedFlushSubscription$p ( GuildMemberSubscriptionsManager guildMemberSubscriptionsManager ) {
return guildMemberSubscriptionsManager . delayedFlushSubscription ;
}
public static final /* synthetic */ Function1 access$getRequestFlushUnsubscriptions$p ( GuildMemberSubscriptionsManager guildMemberSubscriptionsManager ) {
return guildMemberSubscriptionsManager . requestFlushUnsubscriptions ;
}
public static final /* synthetic */ void access$setDelayedFlushSubscription$p ( GuildMemberSubscriptionsManager guildMemberSubscriptionsManager , Subscription subscription ) {
guildMemberSubscriptionsManager . delayedFlushSubscription = subscription ;
}
private final void flushUnsubscriptions ( ) {
if ( ! this . pendingUnsubscriptions . isEmpty ( ) ) {
for ( Map . Entry < Long , Map < Long , Integer > > entry : this . pendingUnsubscriptions . entrySet ( ) ) {
long longValue = entry . getKey ( ) . longValue ( ) ;
Map < Long , Integer > value = entry . getValue ( ) ;
Map < Long , Integer > map = this . subscriptions . get ( Long . valueOf ( longValue ) ) ;
if ( map ! = null ) {
m . checkNotNullExpressionValue ( map , " subscriptions[guildId] ?: continue " ) ;
boolean z2 = false ;
for ( Map . Entry < Long , Integer > entry2 : value . entrySet ( ) ) {
long longValue2 = entry2 . getKey ( ) . longValue ( ) ;
int intValue = entry2 . getValue ( ) . intValue ( ) ;
Integer num = map . get ( Long . valueOf ( longValue2 ) ) ;
if ( num ! = null ) {
int intValue2 = num . intValue ( ) - intValue ;
if ( intValue2 > 0 ) {
map . put ( Long . valueOf ( longValue2 ) , Integer . valueOf ( intValue2 ) ) ;
} else {
map . remove ( Long . valueOf ( longValue2 ) ) ;
z2 = true ;
}
}
}
if ( z2 ) {
if ( map . isEmpty ( ) ) {
this . subscriptions . remove ( Long . valueOf ( longValue ) ) ;
}
this . onChange . invoke ( Long . valueOf ( longValue ) , get ( longValue ) , Boolean . TRUE ) ;
}
}
}
this . delayedFlushSubscription = null ;
this . pendingUnsubscriptions . clear ( ) ;
}
}
private final void triggerUnsubscribe ( ) {
if ( this . delayedFlushSubscription = = null ) {
2021-07-03 23:36:06 +00:00
Observable < Long > e02 = Observable . e0 ( 20 , TimeUnit . SECONDS , this . delayScheduler ) ;
m . checkNotNullExpressionValue ( e02 , " Observable \ n .ti….SECONDS, delayScheduler) " ) ;
ObservableExtensionsKt . appSubscribe$default ( e02 , GuildMemberSubscriptionsManager . class , ( Context ) null , new GuildMemberSubscriptionsManager$triggerUnsubscribe$1 ( this ) , ( Function1 ) null , ( Function0 ) null , ( Function0 ) null , new GuildMemberSubscriptionsManager$triggerUnsubscribe$2 ( this ) , 58 , ( Object ) null ) ;
2021-06-27 20:44:35 +00:00
}
}
public final Set < Long > get ( long j ) {
Set < Long > keySet ;
Map < Long , Integer > map = this . subscriptions . get ( Long . valueOf ( j ) ) ;
return ( map = = null | | ( keySet = map . keySet ( ) ) = = null ) ? n0 . emptySet ( ) : keySet ;
}
public final void remove ( long j ) {
this . subscriptions . remove ( Long . valueOf ( j ) ) ;
this . pendingUnsubscriptions . remove ( Long . valueOf ( j ) ) ;
}
public final void reset ( ) {
this . subscriptions . clear ( ) ;
this . pendingUnsubscriptions . clear ( ) ;
Subscription subscription = this . delayedFlushSubscription ;
if ( subscription ! = null ) {
subscription . unsubscribe ( ) ;
}
this . delayedFlushSubscription = null ;
}
public final void retainAll ( List < Long > list ) {
m . checkNotNullParameter ( list , " guildIds " ) ;
Iterator < Map . Entry < Long , Map < Long , Integer > > > it = this . subscriptions . entrySet ( ) . iterator ( ) ;
while ( it . hasNext ( ) ) {
if ( ! list . contains ( Long . valueOf ( it . next ( ) . getKey ( ) . longValue ( ) ) ) ) {
it . remove ( ) ;
}
}
}
public final void subscribe ( long j , long j2 ) {
Map < Long , Integer > map = this . subscriptions . get ( Long . valueOf ( j ) ) ;
if ( map = = null ) {
map = new HashMap < > ( ) ;
}
Long valueOf = Long . valueOf ( j2 ) ;
Integer num = map . get ( Long . valueOf ( j2 ) ) ;
map . put ( valueOf , Integer . valueOf ( num ! = null ? num . intValue ( ) + 1 : 1 ) ) ;
this . subscriptions . put ( Long . valueOf ( j ) , map ) ;
Integer num2 = map . get ( Long . valueOf ( j2 ) ) ;
if ( num2 ! = null & & num2 . intValue ( ) = = 1 ) {
this . onChange . invoke ( Long . valueOf ( j ) , map . keySet ( ) , Boolean . FALSE ) ;
}
}
public final void unsubscribe ( long j , long j2 ) {
Map < Long , Integer > map = this . subscriptions . get ( Long . valueOf ( j ) ) ;
if ( ( map ! = null ? map . get ( Long . valueOf ( j2 ) ) : null ) ! = null ) {
Map < Long , Integer > map2 = this . pendingUnsubscriptions . get ( Long . valueOf ( j ) ) ;
if ( map2 = = null ) {
map2 = new HashMap < > ( ) ;
}
Long valueOf = Long . valueOf ( j2 ) ;
Integer num = map2 . get ( Long . valueOf ( j2 ) ) ;
map2 . put ( valueOf , Integer . valueOf ( num ! = null ? num . intValue ( ) + 1 : 1 ) ) ;
this . pendingUnsubscriptions . put ( Long . valueOf ( j ) , map2 ) ;
Integer num2 = map2 . get ( Long . valueOf ( j2 ) ) ;
if ( num2 ! = null & & num2 . intValue ( ) = = 1 ) {
triggerUnsubscribe ( ) ;
}
}
}
}