Skip to content

Commit

Permalink
Adding onCurrentMainThreadMessageFinished and make MainThreadMessageS…
Browse files Browse the repository at this point in the history
…copedLazy work all the time
  • Loading branch information
pyricau committed Sep 11, 2024
1 parent e455c89 commit 8f4a330
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
1 change: 0 additions & 1 deletion papa-main-trace/api/papa-main-trace.api
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public final class papa/MainThreadMessageSpy {
public final fun addTracer (Lpapa/MainThreadMessageSpy$Tracer;)V
public final fun getCurrentMessageAsString ()Ljava/lang/String;
public final fun getEnabled ()Z
public final fun onCurrentMessageFinished (Lkotlin/jvm/functions/Function0;)V
public final fun removeTracer (Lpapa/MainThreadMessageSpy$Tracer;)V
public final fun startSpyingMainThreadDispatching ()V
public final fun stopSpyingMainThreadDispatching ()V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ class MainThreadMessageScopedLazy<T>(val provider: () -> T) : ReadOnlyProperty<A
valueOrNull?.let {
return it
}
check(MainThreadMessageSpy.enabled) {
"Can't use a MainThreadMessageScopedLazy when MainThreadMessageSpy is not enabled."
}
val value = provider()
valueOrNull = value
MainThreadMessageSpy.onCurrentMessageFinished {
Handlers.onCurrentMainThreadMessageFinished {
valueOrNull = null
}
return value
Expand Down
32 changes: 16 additions & 16 deletions papa-main-trace/src/main/java/papa/MainThreadMessageSpy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,6 @@ object MainThreadMessageSpy {
tracers.removeAll { it === tracer }
}

fun onCurrentMessageFinished(block: () -> Unit) {
if (!enabled) {
return
}
Handlers.checkOnMainThread()
tracers.add(object : Tracer {
override fun onMessageDispatch(
messageAsString: String,
before: Boolean
) {
tracers.remove(this)
block()
}
})
}

fun startSpyingMainThreadDispatching() {
Handlers.checkOnMainThread()
if (VERSION.SDK_INT == 28) {
Expand Down Expand Up @@ -96,4 +80,20 @@ object MainThreadMessageSpy {
enabled = false
Looper.getMainLooper().setMessageLogging(null)
}

/**
* Internal, we expect callers to have checked that we're on the
* main thread and that enabled is true.
*/
internal fun onCurrentMessageFinished(block: () -> Unit) {
tracers.add(object : Tracer {
override fun onMessageDispatch(
messageAsString: String,
before: Boolean
) {
tracers.remove(this)
block()
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class MainThreadMessageSpyTest {
runOrder += "second post"
runnablesRan.countDown()
}
MainThreadMessageSpy.onCurrentMessageFinished {
Handlers.onCurrentMainThreadMessageFinished {
runOrder += "first post finished"
runnablesRan.countDown()
}
Expand All @@ -81,8 +81,8 @@ class MainThreadMessageSpyTest {
@Test
fun no_ConcurrentModificationException_when_iterating_after_onCurrentMessageFinished_removes_its_tracer() {
runOnMainSync {
MainThreadMessageSpy.onCurrentMessageFinished {}
MainThreadMessageSpy.onCurrentMessageFinished {}
Handlers.onCurrentMainThreadMessageFinished {}
Handlers.onCurrentMainThreadMessageFinished {}
}
}

Expand Down

0 comments on commit 8f4a330

Please sign in to comment.