Skip to content

Commit

Permalink
Synchronize registrations in ChartEntryModelProducer
Browse files Browse the repository at this point in the history
Co-authored-by: Patrick Michalik <120058021+patrickmichalik@users.noreply.github.com>
  • Loading branch information
Gowsky and patrickmichalik committed Aug 11, 2024
1 parent b931d96 commit fba3ea4
Showing 1 changed file with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import java.util.concurrent.ConcurrentHashMap

/**
* A [ChartModelProducer] implementation that generates [ChartEntryModel] instances.
Expand All @@ -51,8 +54,9 @@ public class ChartEntryModelProducer(
private var cachedInternalModel: InternalModel? = null
private val mutex = Mutex()
private val coroutineScope = CoroutineScope(dispatcher)
private val updateReceivers: HashMap<Any, UpdateReceiver> = HashMap()
private val updateReceivers = ConcurrentHashMap<Any, UpdateReceiver>()
private val extraStore = MutableExtraStore()
private val registrationLock = Mutex()

public constructor(
vararg entryCollections: List<ChartEntry>,
Expand All @@ -74,11 +78,14 @@ public class ChartEntryModelProducer(
series = entries.copy()
updateExtras(extraStore)
cachedInternalModel = null
val deferredUpdates = updateReceivers.values.map { updateReceiver ->
coroutineScope.async { updateReceiver.handleUpdate() }
}
coroutineScope.launch {
registrationLock.lock()
val deferredUpdates = updateReceivers.values.map { updateReceiver ->
async { updateReceiver.handleUpdate() }
}

deferredUpdates.awaitAll()
registrationLock.unlock()
mutex.unlock()
}
return true
Expand Down Expand Up @@ -200,8 +207,12 @@ public class ChartEntryModelProducer(
getOldModel,
updateChartValues,
).run {
updateReceivers[key] = this
handleUpdate()
runBlocking {
registrationLock.withLock {
updateReceivers[key] = this@run
handleUpdate()
}
}
}
}

Expand Down

0 comments on commit fba3ea4

Please sign in to comment.