|
| 1 | +package com.onesignal.common.consistency |
| 2 | + |
| 3 | +import com.onesignal.common.consistency.enums.IamFetchRywTokenKey |
| 4 | +import com.onesignal.common.consistency.models.ICondition |
| 5 | +import com.onesignal.common.consistency.models.IConsistencyKeyEnum |
| 6 | + |
| 7 | +/** |
| 8 | + * Used for read your write consistency when fetching In-App Messages. |
| 9 | + * |
| 10 | + * Params: |
| 11 | + * key : String - the index of the RYW token map |
| 12 | + */ |
| 13 | +class IamFetchReadyCondition( |
| 14 | + private val key: String, |
| 15 | +) : ICondition { |
| 16 | + companion object { |
| 17 | + const val ID = "IamFetchReadyCondition" |
| 18 | + } |
| 19 | + |
| 20 | + override val id: String |
| 21 | + get() = ID |
| 22 | + |
| 23 | + override fun isMet(indexedTokens: Map<String, Map<IConsistencyKeyEnum, String>>): Boolean { |
| 24 | + val tokenMap = indexedTokens[key] ?: return false |
| 25 | + val userUpdateTokenSet = tokenMap[IamFetchRywTokenKey.USER] != null |
| 26 | + val subscriptionUpdateTokenSet = tokenMap[IamFetchRywTokenKey.SUBSCRIPTION] != null |
| 27 | + |
| 28 | + /** |
| 29 | + * We always update the session count so we know we will have a userUpdateToken. We don't |
| 30 | + * necessarily make a subscriptionUpdate call on every session. The following logic |
| 31 | + * is written in a way so that if somehow the subscriptionUpdateToken is set *before* the |
| 32 | + * userUpdateToken, we will wait for the userUpdateToken to also be set. This is because |
| 33 | + * we know that a userUpdate call was made and both user & subscription properties are |
| 34 | + * considered during segment calculations. |
| 35 | + */ |
| 36 | + return (userUpdateTokenSet && subscriptionUpdateTokenSet) || userUpdateTokenSet |
| 37 | + } |
| 38 | + |
| 39 | + override fun getNewestToken(indexedTokens: Map<String, Map<IConsistencyKeyEnum, String?>>): String? { |
| 40 | + val tokenMap = indexedTokens[key] ?: return null |
| 41 | + // maxOrNull compares lexicographically |
| 42 | + return listOfNotNull(tokenMap[IamFetchRywTokenKey.USER], tokenMap[IamFetchRywTokenKey.SUBSCRIPTION]).maxOrNull() |
| 43 | + } |
| 44 | +} |
0 commit comments