1 // Copyright 2015-present 650 Industries. All rights reserved. 2 package host.exp.exponent.utils 3 4 import android.util.Log 5 import host.exp.exponent.analytics.EXL 6 7 object AsyncCondition { 8 private val TAG = AsyncCondition::class.java.simpleName 9 10 private val listenerMap = mutableMapOf<String, AsyncConditionListener>() 11 waitnull12 @JvmStatic fun wait(key: String, listener: AsyncConditionListener) { 13 if (listener.isReady()) { 14 listener.execute() 15 } else { 16 synchronized(listenerMap) { 17 if (listenerMap.containsKey(key)) { 18 EXL.e(TAG, "Map already contains entry for key $key. Ignoring.") 19 return 20 } 21 listenerMap.put(key, listener) 22 } 23 } 24 } 25 notifynull26 @JvmStatic fun notify(key: String) { 27 synchronized(listenerMap) { 28 if (!listenerMap.containsKey(key)) { 29 Log.w(TAG, "Could not find listener for key: $key") 30 return 31 } 32 val listener = listenerMap.remove(key) 33 if (listener!!.isReady()) { 34 listener.execute() 35 } 36 } 37 } 38 removenull39 @JvmStatic fun remove(key: String) { 40 synchronized(listenerMap) { listenerMap.remove(key) } 41 } 42 43 interface AsyncConditionListener { isReadynull44 fun isReady(): Boolean 45 fun execute() 46 } 47 } 48