Skip to content

Commit

Permalink
fix(sdk): replace GlobalScope with correct coroutine scope
Browse files Browse the repository at this point in the history
  • Loading branch information
hamada147 authored May 15, 2023
1 parent 3ca39ee commit e44ac86
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.http.HttpMethod
import io.ktor.http.Url
import io.ktor.serialization.kotlinx.json.json
import kotlinx.coroutines.CoroutineScope
import java.time.Duration
import kotlin.jvm.Throws
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -68,15 +69,11 @@ class PrismAgent {
val mercury: Mercury
var fetchingMessagesJob: Job? = null

private val prismAgentScope: CoroutineScope = CoroutineScope(Dispatchers.Default)
private val api: Api
private val connectionManager: ConnectionManager
private val flowState = MutableSharedFlow<State>()

fun getFlowState(): Flow<State> {
return flowState
}

@OptIn(DelicateCoroutinesApi::class)
constructor(
apollo: Apollo,
castor: Castor,
Expand All @@ -86,7 +83,7 @@ class PrismAgent {
seed: Seed?,
api: Api?
) {
GlobalScope.launch {
prismAgentScope.launch {
flowState.emit(State.STOPPED)
}
this.apollo = apollo
Expand All @@ -110,7 +107,6 @@ class PrismAgent {
)
}

@OptIn(DelicateCoroutinesApi::class)
@JvmOverloads
constructor(
apollo: Apollo,
Expand All @@ -121,7 +117,7 @@ class PrismAgent {
api: Api? = null,
mediatorHandler: MediationHandler
) {
GlobalScope.launch {
prismAgentScope.launch {
flowState.emit(State.STOPPED)
}
this.apollo = apollo
Expand All @@ -146,6 +142,10 @@ class PrismAgent {
this.connectionManager = ConnectionManager(mercury, castor, pluto, mediatorHandler, mutableListOf())
}

fun getFlowState(): Flow<State> {
return flowState
}

@Throws(PrismAgentError.MediationRequestFailedError::class)
suspend fun start() {
if (state != State.STOPPED) {
Expand Down Expand Up @@ -272,7 +272,7 @@ class PrismAgent {
@JvmOverloads
fun startFetchingMessages(requestInterval: Int = 5) {
if (fetchingMessagesJob == null) {
fetchingMessagesJob = GlobalScope.launch {
fetchingMessagesJob = prismAgentScope.launch {
while (true) {
connectionManager.awaitMessages().collect { array ->
val messagesIds = mutableListOf<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import io.iohk.atala.prism.walletsdk.apollo.ApolloImpl
import io.iohk.atala.prism.walletsdk.castor.CastorImpl
import io.iohk.atala.prism.walletsdk.domain.buildingblocks.Apollo
Expand All @@ -26,8 +27,6 @@ import io.iohk.atala.prism.walletsdk.prismagent.mediation.BasicMediatorHandler
import io.iohk.atala.prism.walletsdk.prismagent.mediation.MediationHandler
import io.iohk.atala.prism.walletsdk.prismagent.protocols.outOfBand.OutOfBandInvitation
import io.iohk.atala.prism.walletsdk.prismagent.protocols.outOfBand.PrismOnboardingInvitation
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.lang.Exception
import kotlin.jvm.Throws
Expand All @@ -45,9 +44,8 @@ class FirstViewModel : ViewModel() {
private val notification: MutableLiveData<String> = MutableLiveData("")
private val agentState: MutableLiveData<String> = MutableLiveData("")

@OptIn(DelicateCoroutinesApi::class)
fun startAgent(context: Context) {
GlobalScope.launch {
viewModelScope.launch {
initializeApollo()
initializePluto(context)
initializeCastor()
Expand All @@ -64,11 +62,9 @@ class FirstViewModel : ViewModel() {
}
}

@OptIn(DelicateCoroutinesApi::class)
fun stopAgent() {
// TODO: late init property not initialized error
agent.let {
GlobalScope.launch {
if (this::agent.isInitialized) {
viewModelScope.launch {
agent.stopFetchingMessages()
agent.stop()
}
Expand All @@ -87,13 +83,12 @@ class FirstViewModel : ViewModel() {
return agentState
}

@OptIn(DelicateCoroutinesApi::class)
@Throws(Exception::class)
@Throws(Exception::class, PrismAgentError.UnknownInvitationTypeError::class)
fun parseAndAcceptOOB(oobUrl: String) {
if (this::agent.isInitialized.not()) {
throw Exception("Agent has not been started")
}
GlobalScope.launch {
viewModelScope.launch {
when (val invitation = agent.parseInvitation(oobUrl)) {
is OutOfBandInvitation -> {
agent.acceptOutOfBandInvitation(invitation)
Expand All @@ -108,9 +103,8 @@ class FirstViewModel : ViewModel() {
}
}

@OptIn(DelicateCoroutinesApi::class)
fun sendTestMessage(did: DID) {
GlobalScope.launch {
viewModelScope.launch {
// val senderPeerDid = agent.createNewPeerDID(
// emptyArray(),
// true
Expand All @@ -127,9 +121,8 @@ class FirstViewModel : ViewModel() {
}
}

@OptIn(DelicateCoroutinesApi::class)
fun createPeerDid() {
GlobalScope.launch {
viewModelScope.launch {
val did = agent.createNewPeerDID(
arrayOf(
DIDDocument.Service(
Expand Down Expand Up @@ -206,7 +199,6 @@ class FirstViewModel : ViewModel() {
)
}

@OptIn(DelicateCoroutinesApi::class)
private fun initializeAgent() {
agent = PrismAgent(
apollo = apollo,
Expand All @@ -216,7 +208,7 @@ class FirstViewModel : ViewModel() {
seed = seed,
mediatorHandler = handler
)
GlobalScope.launch() {
viewModelScope.launch {
agent.getFlowState().collect {
agentState.postValue(it.name)
}
Expand Down

0 comments on commit e44ac86

Please sign in to comment.