-
-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not refresh transaction when it is not being affected. (#246)
* Do not refresh transaction when it is not being affected. * Use correct null-aware comparison for HttpTransaction.
- Loading branch information
Showing
8 changed files
with
197 additions
and
2 deletions.
There are no files selected for viewing
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
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
34 changes: 34 additions & 0 deletions
34
library/src/main/java/com/chuckerteam/chucker/internal/support/LiveDataUtils.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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.chuckerteam.chucker.internal.support | ||
|
||
import android.annotation.SuppressLint | ||
import androidx.arch.core.executor.ArchTaskExecutor | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MediatorLiveData | ||
import java.util.concurrent.Executor | ||
|
||
// Unlike built-in extension operation is performed on a provided thread pool. | ||
// This is needed in our case since we compare requests and responses which can be big | ||
// and result in frame drops. | ||
internal fun <T> LiveData<T>.distinctUntilChanged( | ||
executor: Executor = ioExecutor(), | ||
areEqual: (old: T, new: T) -> Boolean = { old, new -> old == new } | ||
): LiveData<T> { | ||
val distinctMediator = MediatorLiveData<T>() | ||
var old = uninitializedToken | ||
distinctMediator.addSource(this) { new -> | ||
executor.execute { | ||
@Suppress("UNCHECKED_CAST") | ||
if (old === uninitializedToken || !areEqual(old as T, new)) { | ||
old = new | ||
distinctMediator.postValue(new) | ||
} | ||
} | ||
} | ||
return distinctMediator | ||
} | ||
|
||
private val uninitializedToken: Any? = Any() | ||
|
||
// It is lesser evil than providing a custom executor. | ||
@SuppressLint("RestrictedApi") | ||
private fun ioExecutor() = ArchTaskExecutor.getIOThreadExecutor() |
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
88 changes: 88 additions & 0 deletions
88
...rc/test/java/com/chuckerteam/chucker/internal/support/LiveDataDistinctUntilChangedTest.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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.chuckerteam.chucker.internal.support | ||
|
||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.distinctUntilChanged | ||
import com.chuckerteam.chucker.test | ||
import junit.framework.TestCase.assertEquals | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class LiveDataDistinctUntilChangedTest { | ||
@get:Rule val instantExecutorRule = InstantTaskExecutorRule() | ||
|
||
@Test | ||
fun initialUpstreamData_isEmittedDownstream() { | ||
val upstream = MutableLiveData<Any?>(null) | ||
|
||
upstream.distinctUntilChanged().test { | ||
assertEquals(null, expectData()) | ||
} | ||
} | ||
|
||
@Test | ||
fun emptyUpstream_isNotEmittedDownstream() { | ||
val upstream = MutableLiveData<Any?>() | ||
|
||
upstream.distinctUntilChanged().test { | ||
expectNoData() | ||
} | ||
} | ||
|
||
@Test | ||
fun newDistinctData_isEmittedDownstream() { | ||
val upstream = MutableLiveData<Int?>() | ||
|
||
upstream.distinctUntilChanged().test { | ||
upstream.value = 1 | ||
assertEquals(1, expectData()) | ||
|
||
upstream.value = 2 | ||
assertEquals(2, expectData()) | ||
|
||
upstream.value = null | ||
assertEquals(null, expectData()) | ||
|
||
upstream.value = 2 | ||
assertEquals(2, expectData()) | ||
} | ||
} | ||
|
||
@Test | ||
fun newIndistinctData_isNotEmittedDownstream() { | ||
val upstream = MutableLiveData<String?>() | ||
|
||
upstream.distinctUntilChanged().test { | ||
upstream.value = null | ||
assertEquals(null, expectData()) | ||
|
||
upstream.value = null | ||
expectNoData() | ||
|
||
upstream.value = "" | ||
assertEquals("", expectData()) | ||
|
||
upstream.value = "" | ||
expectNoData() | ||
} | ||
} | ||
|
||
@Test | ||
fun customFunction_canBeUsedToDistinguishData() { | ||
val upstream = MutableLiveData<Pair<Int, String>>() | ||
|
||
upstream.distinctUntilChanged { old, new -> old.first == new.first }.test { | ||
upstream.value = 1 to "" | ||
assertEquals(1 to "", expectData()) | ||
|
||
upstream.value = 1 to "a" | ||
expectNoData() | ||
|
||
upstream.value = 2 to "b" | ||
assertEquals(2 to "b", expectData()) | ||
|
||
upstream.value = 3 to "b" | ||
assertEquals(3 to "b", expectData()) | ||
} | ||
} | ||
} |