Skip to content

Commit 4cce219

Browse files
committed
fix tests handing because of suspendLazy
1 parent 9bf35b4 commit 4cce219

File tree

1 file changed

+12
-4
lines changed
  • domain/src/main/java/com/example/util/simpletimetracker/domain/base

1 file changed

+12
-4
lines changed

domain/src/main/java/com/example/util/simpletimetracker/domain/base/LazySuspend.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
package com.example.util.simpletimetracker.domain.base
22

33
import kotlinx.coroutines.CoroutineScope
4-
import kotlinx.coroutines.CoroutineStart
5-
import kotlinx.coroutines.async
4+
import kotlinx.coroutines.sync.Mutex
5+
import kotlinx.coroutines.sync.withLock
66

77
fun <T> CoroutineScope.suspendLazy(
88
initializer: suspend CoroutineScope.() -> T,
99
) = object : SuspendLazy<T> {
10-
private val deferred = async(start = CoroutineStart.LAZY, block = initializer)
11-
override suspend operator fun invoke(): T = deferred.await()
10+
private val mutex = Mutex()
11+
private var value: T? = null
12+
13+
override suspend operator fun invoke(): T {
14+
value?.let { return it }
15+
return mutex.withLock {
16+
value?.let { return it }
17+
initializer().also { value = it }
18+
}
19+
}
1220
}
1321

1422
interface SuspendLazy<T> {

0 commit comments

Comments
 (0)