Skip to content

Commit

Permalink
Synchronize the book updating.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWoitaschek committed Mar 10, 2023
1 parent a02cffe commit dbbaee9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions data/src/main/kotlin/voice/data/repo/BookRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class BookRepository
) {

private var warmedUp = false
private val warmupMutex = Mutex()
private val mutex = Mutex()

private suspend fun warmUp() {
if (warmedUp) return
warmupMutex.withLock {
mutex.withLock {
if (warmedUp) return@withLock
val chapters = contentRepo.all()
.filter { it.isActive }
Expand Down Expand Up @@ -59,9 +59,11 @@ class BookRepository
}

suspend fun updateBook(id: BookId, update: (BookContent) -> BookContent) {
val content = contentRepo.get(id) ?: return
val updated = update(content)
contentRepo.put(updated)
mutex.withLock {
val content = contentRepo.get(id) ?: return
val updated = update(content)
contentRepo.put(updated)
}
}

private suspend fun BookContent.book(): Book? {
Expand Down

0 comments on commit dbbaee9

Please sign in to comment.