mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 22:17:03 +05:30
feat: separate db reads and writes to separate dispatchers
This commit is contained in:
parent
f818da6f64
commit
976c9dd064
5 changed files with 38 additions and 20 deletions
|
@ -6,7 +6,8 @@
|
|||
*/
|
||||
package dev.msfjarvis.claw.android.viewmodel
|
||||
|
||||
import dev.msfjarvis.claw.core.injection.DatabaseDispatcher
|
||||
import dev.msfjarvis.claw.core.injection.DatabaseReadDispatcher
|
||||
import dev.msfjarvis.claw.core.injection.DatabaseWriteDispatcher
|
||||
import dev.msfjarvis.claw.database.local.PostComments
|
||||
import dev.msfjarvis.claw.database.local.PostCommentsQueries
|
||||
import dev.msfjarvis.claw.model.Comment
|
||||
|
@ -18,14 +19,15 @@ class CommentsRepository
|
|||
@Inject
|
||||
constructor(
|
||||
private val postCommentsQueries: PostCommentsQueries,
|
||||
@DatabaseDispatcher private val dbDispatcher: CoroutineDispatcher,
|
||||
@DatabaseReadDispatcher private val readDispatcher: CoroutineDispatcher,
|
||||
@DatabaseWriteDispatcher private val writeDispatcher: CoroutineDispatcher,
|
||||
) {
|
||||
|
||||
suspend fun getSeenComments(postId: String) =
|
||||
withContext(dbDispatcher) { postCommentsQueries.getCommentIds(postId).executeAsOneOrNull() }
|
||||
withContext(readDispatcher) { postCommentsQueries.getCommentIds(postId).executeAsOneOrNull() }
|
||||
|
||||
suspend fun markSeenComments(postId: String, comments: List<Comment>) {
|
||||
withContext(dbDispatcher) {
|
||||
withContext(writeDispatcher) {
|
||||
postCommentsQueries.rememberComments(PostComments(postId, comments.map { it.shortId }))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,8 @@ package dev.msfjarvis.claw.android.viewmodel
|
|||
|
||||
import app.cash.sqldelight.coroutines.asFlow
|
||||
import app.cash.sqldelight.coroutines.mapToList
|
||||
import dev.msfjarvis.claw.core.injection.DatabaseDispatcher
|
||||
import dev.msfjarvis.claw.core.injection.DatabaseReadDispatcher
|
||||
import dev.msfjarvis.claw.core.injection.DatabaseWriteDispatcher
|
||||
import dev.msfjarvis.claw.database.local.ReadPostsQueries
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
|
@ -18,11 +19,12 @@ class ReadPostsRepository
|
|||
@Inject
|
||||
constructor(
|
||||
private val readPostsQueries: ReadPostsQueries,
|
||||
@DatabaseDispatcher private val dbDispatcher: CoroutineDispatcher,
|
||||
@DatabaseReadDispatcher private val readDispatcher: CoroutineDispatcher,
|
||||
@DatabaseWriteDispatcher private val writeDispatcher: CoroutineDispatcher,
|
||||
) {
|
||||
val readPosts = readPostsQueries.selectAllPosts().asFlow().mapToList(dbDispatcher)
|
||||
val readPosts = readPostsQueries.selectAllPosts().asFlow().mapToList(readDispatcher)
|
||||
|
||||
suspend fun markRead(postId: String) {
|
||||
withContext(dbDispatcher) { readPostsQueries.markRead(postId) }
|
||||
withContext(writeDispatcher) { readPostsQueries.markRead(postId) }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,8 @@ package dev.msfjarvis.claw.android.viewmodel
|
|||
|
||||
import app.cash.sqldelight.coroutines.asFlow
|
||||
import app.cash.sqldelight.coroutines.mapToList
|
||||
import dev.msfjarvis.claw.core.injection.DatabaseDispatcher
|
||||
import dev.msfjarvis.claw.core.injection.DatabaseReadDispatcher
|
||||
import dev.msfjarvis.claw.core.injection.DatabaseWriteDispatcher
|
||||
import dev.msfjarvis.claw.database.local.SavedPost
|
||||
import dev.msfjarvis.claw.database.local.SavedPostQueries
|
||||
import dev.msfjarvis.claw.model.UIPost
|
||||
|
@ -23,23 +24,24 @@ class SavedPostsRepository
|
|||
@Inject
|
||||
constructor(
|
||||
private val savedPostQueries: SavedPostQueries,
|
||||
@DatabaseDispatcher private val dbDispatcher: CoroutineDispatcher,
|
||||
@DatabaseReadDispatcher private val readDispatcher: CoroutineDispatcher,
|
||||
@DatabaseWriteDispatcher private val writeDispatcher: CoroutineDispatcher,
|
||||
) {
|
||||
val savedPosts = savedPostQueries.selectAllPosts().asFlow().mapToList(dbDispatcher)
|
||||
val savedPosts = savedPostQueries.selectAllPosts().asFlow().mapToList(readDispatcher)
|
||||
|
||||
suspend fun toggleSave(post: UIPost) {
|
||||
if (savedPosts.firstOrNull().orEmpty().any { it.shortId == post.shortId }) {
|
||||
Napier.d(tag = TAG) { "Removing post: ${post.shortId}" }
|
||||
withContext(dbDispatcher) { savedPostQueries.deletePost(post.shortId) }
|
||||
withContext(writeDispatcher) { savedPostQueries.deletePost(post.shortId) }
|
||||
} else {
|
||||
Napier.d(tag = TAG) { "Saving post: ${post.shortId}" }
|
||||
withContext(dbDispatcher) { savedPostQueries.insertOrReplacePost(post.toSavedPost()) }
|
||||
withContext(writeDispatcher) { savedPostQueries.insertOrReplacePost(post.toSavedPost()) }
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun savePosts(posts: List<SavedPost>) {
|
||||
Napier.d(tag = TAG) { "Saving posts: ${posts.joinToString(",") { it.shortId }}" }
|
||||
withContext(dbDispatcher) {
|
||||
withContext(writeDispatcher) {
|
||||
savedPostQueries.transaction {
|
||||
posts.forEach { post -> savedPostQueries.insertOrReplacePost(post) }
|
||||
}
|
||||
|
|
|
@ -21,8 +21,11 @@ interface DispatcherProvider {
|
|||
|
||||
fun io(): CoroutineDispatcher = Dispatchers.IO
|
||||
|
||||
fun database(): CoroutineDispatcher =
|
||||
Dispatchers.IO.limitedParallelism(1, name = "DatabaseDispatcher")
|
||||
fun databaseRead(): CoroutineDispatcher =
|
||||
Dispatchers.IO.limitedParallelism(4, name = "DatabaseRead")
|
||||
|
||||
fun databaseWrite(): CoroutineDispatcher =
|
||||
Dispatchers.IO.limitedParallelism(1, name = "DatabaseWrite")
|
||||
}
|
||||
|
||||
/** Concrete type for [DispatcherProvider] with all the defaults from the class. */
|
||||
|
|
|
@ -16,7 +16,9 @@ import dev.msfjarvis.claw.core.coroutines.DispatcherProvider
|
|||
import javax.inject.Qualifier
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
|
||||
@Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class DatabaseDispatcher
|
||||
@Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class DatabaseReadDispatcher
|
||||
|
||||
@Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class DatabaseWriteDispatcher
|
||||
|
||||
@Qualifier @Retention(AnnotationRetention.RUNTIME) annotation class MainDispatcher
|
||||
|
||||
|
@ -36,9 +38,16 @@ interface CoroutineDispatcherModule {
|
|||
return dispatcherProvider.io()
|
||||
}
|
||||
|
||||
@[Provides DatabaseDispatcher]
|
||||
fun provideDatabaseDispatcher(dispatcherProvider: DispatcherProvider): CoroutineDispatcher {
|
||||
return dispatcherProvider.database()
|
||||
@[Provides DatabaseReadDispatcher]
|
||||
fun provideDatabaseReadDispatcher(dispatcherProvider: DispatcherProvider): CoroutineDispatcher {
|
||||
return dispatcherProvider.databaseRead()
|
||||
}
|
||||
|
||||
@[Provides DatabaseWriteDispatcher]
|
||||
fun provideDatabaseWriteDispatcher(
|
||||
dispatcherProvider: DispatcherProvider
|
||||
): CoroutineDispatcher {
|
||||
return dispatcherProvider.databaseWrite()
|
||||
}
|
||||
|
||||
@[Provides MainDispatcher]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue