File tree Expand file tree Collapse file tree 1 file changed +12
-4
lines changed
domain/src/main/java/com/example/util/simpletimetracker/domain/base Expand file tree Collapse file tree 1 file changed +12
-4
lines changed Original file line number Diff line number Diff line change 11package com.example.util.simpletimetracker.domain.base
22
33import 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
77fun <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
1422interface SuspendLazy <T > {
You can’t perform that action at this time.
0 commit comments