Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ 문자열 상수로 변경, 취소 가능한 서스펜드 변경 #89

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ object CollectionId {

const val USERS_COLLECTION = "Users"
const val RUNNERS_COLLECTION = "Runners"
const val RUNNERS_ID = "runnersId"
const val GROUPS_COLLECTION = "Groups"
const val GROUP_NOTIFICATIONS_COLLECTION = "GroupNotifications"
const val START_NOTIFICATION = "start"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import com.whyranoid.data.constant.CollectionId
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

class RunnerDataSourceImpl(private val db: FirebaseFirestore) : RunnerDataSource {

override fun getCurrentRunnerCount(): Flow<Int> = callbackFlow {
db.collection(CollectionId.RUNNERS_COLLECTION)
.document("runnersId")
.document(CollectionId.RUNNERS_ID)
.addSnapshotListener { snapshot, _ ->
snapshot?.let {
val count = it.data?.size ?: -1
Expand All @@ -26,9 +26,9 @@ class RunnerDataSourceImpl(private val db: FirebaseFirestore) : RunnerDataSource

override suspend fun startRunning(uid: String): Boolean {
if (uid.isBlank()) return false
return suspendCoroutine { continuation ->
return suspendCancellableCoroutine { continuation ->
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시.....cancellable일 때 뭐가 더 좋은건지 다시 설명해주실 수 있을까요.............불량학생이라 죄송합니다 교수님...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

재수강하러 왔습니다

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

공식 문서에서 prompt cancellation guarantee 라고 표현하는데요.
일시 중단된 동안 현재 코루틴의 작업이 취소된 경우 작업 재개하지 않고 중단하게 됩니다!

db.collection(CollectionId.RUNNERS_COLLECTION)
.document("runnersId")
.document(CollectionId.RUNNERS_ID)
.update(uid, uid)
.addOnSuccessListener {
continuation.resume(true)
Expand All @@ -41,9 +41,9 @@ class RunnerDataSourceImpl(private val db: FirebaseFirestore) : RunnerDataSource

override suspend fun finishRunning(uid: String): Boolean {
if (uid.isBlank()) return false
return suspendCoroutine { continuation ->
return suspendCancellableCoroutine { continuation ->
db.collection(CollectionId.RUNNERS_COLLECTION)
.document("runnersId")
.document(CollectionId.RUNNERS_ID)
.update(uid, FieldValue.delete())
.addOnSuccessListener {
continuation.resume(true)
Expand Down