-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ce2886
commit 7de859e
Showing
7 changed files
with
35 additions
and
98 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
...ata/src/main/java/dohun/kim/gatabuta/gatabuta_livedata/exception/NullLiveDataException.kt
This file was deleted.
Oops, something went wrong.
38 changes: 10 additions & 28 deletions
38
gatabuta-livedata/src/main/java/dohun/kim/gatabuta/gatabuta_livedata/getOrAwaitValue.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,22 @@ | ||
package dohun.kim.gatabuta.gatabuta_livedata | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.Observer | ||
import dohun.kim.gatabuta.gatabuta_livedata.exception.NullLiveDataException | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.TimeUnit | ||
import java.util.concurrent.TimeoutException | ||
import androidx.lifecycle.asFlow | ||
import kotlinx.coroutines.TimeoutCancellationException | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.withTimeout | ||
|
||
/** | ||
* If a LiveData is transformed, the result of the transformation must be observed. | ||
* Otherwise the transformation will never be evaluated. | ||
* | ||
* @link https://medium.com/androiddevelopers/unit-testing-livedata-and-other-common-observability-problems-bb477262eb04 | ||
*/ | ||
internal fun <T> LiveData<T>.getOrAwaitValue(): T { | ||
var data: T? = null | ||
val latch = CountDownLatch(1) | ||
val observer = object : Observer<T> { | ||
override fun onChanged(t: T) { | ||
data = t | ||
latch.countDown() | ||
this@getOrAwaitValue.removeObserver(this) | ||
internal suspend fun <T> LiveData<T>.getOrAwaitValue(): T? = | ||
try { | ||
withTimeout(100) { | ||
this@getOrAwaitValue.asFlow().first() | ||
} | ||
} catch (e: TimeoutCancellationException) { | ||
null | ||
} | ||
|
||
observeForever(observer) | ||
|
||
if (!latch.await(100, TimeUnit.MILLISECONDS)) { | ||
throw TimeoutException("LiveData value was never set. Consider using hasNoValue() instead.") | ||
} | ||
|
||
if (data == null) { | ||
throw NullLiveDataException( | ||
"Data is null, expected non-null" | ||
) | ||
} | ||
|
||
return data!! | ||
} |