Skip to content

Commit

Permalink
Merge pull request #42 from superwall-me/fix-dependency-late-init
Browse files Browse the repository at this point in the history
Should fix IdentityManager race
  • Loading branch information
anglinb authored Oct 19, 2023
2 parents 4ef1963 + 2b9ac40 commit e69f417
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 10 additions & 1 deletion superwall/src/main/java/com/superwall/sdk/Superwall.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.superwall.sdk.delegate.SuperwallDelegateJava
import com.superwall.sdk.delegate.subscription_controller.PurchaseController
import com.superwall.sdk.dependencies.DependencyContainer
import com.superwall.sdk.misc.SerialTaskManager
import com.superwall.sdk.models.config.Config
import com.superwall.sdk.paywall.presentation.PaywallCloseReason
import com.superwall.sdk.paywall.presentation.PresentationItems
import com.superwall.sdk.paywall.presentation.internal.dismiss
Expand All @@ -24,8 +25,10 @@ import com.superwall.sdk.paywall.vc.delegate.PaywallViewControllerEventDelegate
import com.superwall.sdk.paywall.vc.web_view.messaging.PaywallWebEvent
import com.superwall.sdk.paywall.vc.web_view.messaging.PaywallWebEvent.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.filterNotNull
import java.util.*

public class Superwall(context: Context, apiKey: String, purchaseController: PurchaseController?) :
Expand Down Expand Up @@ -114,6 +117,11 @@ public class Superwall(context: Context, apiKey: String, purchaseController: Pur
}
}


protected val _setup = MutableStateFlow<Boolean?>(null)
val isSetup: Flow<Boolean> get() = _setup.filterNotNull()


lateinit var dependencyContainer: DependencyContainer

/// Used to serially execute register calls.
Expand All @@ -122,8 +130,9 @@ public class Superwall(context: Context, apiKey: String, purchaseController: Pur
fun setup() {
this.dependencyContainer = DependencyContainer(context, purchaseController, options)


CoroutineScope(Dispatchers.IO).launch {
// We're all setup, this allows everything else to reference the dependency container
_setup.emit(true)
dependencyContainer.storage.configure(apiKey = apiKey)
dependencyContainer.storage.recordAppInstall {
track(event = it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import kotlinx.serialization.json.JsonObject
import java.util.*

suspend fun Superwall.track(event: Trackable): TrackingResult {

// Wait for _setup
Superwall.instance.isSetup.first()

// Get parameters to be sent to the delegate and stored in an event.
// now with Date
val eventCreatedAt = Date()
Expand Down

0 comments on commit e69f417

Please sign in to comment.