Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,36 +1,65 @@
package org.polyfrost.compose.runtime

import androidx.compose.runtime.BroadcastFrameClock
import androidx.compose.runtime.CompositionContext
import androidx.compose.runtime.Recomposer
import androidx.compose.runtime.snapshots.Snapshot
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import org.polyfrost.compose.host.HostErrors
import java.util.concurrent.atomic.AtomicBoolean

public class PolyComposeClock {
private val clock = BroadcastFrameClock()
private val scope = CoroutineScope(Dispatchers.Unconfined + clock)
private val effectErrors = CoroutineExceptionHandler { _, e ->
if (e is VirtualMachineError) {
val t = Thread.currentThread()
t.uncaughtExceptionHandler.uncaughtException(t, e)
} else {
HostErrors.error("Compose effect failed; its composition stopped updating, other compositions are unaffected", e)
}
}
private val scope = CoroutineScope(Dispatchers.Unconfined + clock + SupervisorJob() + effectErrors)

private val recomposerImpl = Recomposer(scope.coroutineContext).also { r ->
private val live = mutableListOf<Recomposer>()
private var retiredChanges = 0L
private val lock = Any()

internal fun newRecomposer(): Recomposer {
val r = Recomposer(scope.coroutineContext)
synchronized(lock) { live.add(r) }
scope.launch(start = CoroutineStart.UNDISPATCHED) {
r.runRecomposeAndApplyChanges()
try {
r.runRecomposeAndApplyChanges()
} catch (e: CancellationException) {
throw e
} catch (@Suppress("TooGenericExceptionCaught") e: Throwable) {
if (e is VirtualMachineError) throw e
HostErrors.error("Compose content failed and stopped updating; other compositions are unaffected", e)
} finally {
r.cancel()
synchronized(lock) {
retiredChanges += r.changeCount
live.remove(r)
}
}
}
return r
}

internal val recomposer: CompositionContext get() = recomposerImpl

private val inFrame = AtomicBoolean(false)

public fun frame(nanos: Long = System.nanoTime(), notify: Boolean = true): Boolean {
if (!inFrame.compareAndSet(false, true)) return false
try {
if (notify) Snapshot.sendApplyNotifications()
val appliedBefore = recomposerImpl.changeCount
val appliedBefore = changeCount
clock.sendFrame(nanos)
appliedChange = recomposerImpl.changeCount != appliedBefore
appliedChange = changeCount != appliedBefore
return appliedChange
} finally {
inFrame.set(false)
Expand All @@ -40,7 +69,8 @@ public class PolyComposeClock {
public var appliedChange: Boolean = false
private set

public val changeCount: Long get() = recomposerImpl.changeCount
public val changeCount: Long
get() = synchronized(lock) { retiredChanges + live.sumOf { it.changeCount } }
}

public object PolyComposeHost {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class PolyComposeRuntime(private val host: PolyComposeClock = PolyCompose
public val root: RootNode = RootNode()

private val applier = PolyApplier(root)
private val composition = Composition(applier, host.recomposer)
private val recomposer = host.newRecomposer()
private val composition = Composition(applier, recomposer)

public var revision: Long = 0L
private set
Expand Down Expand Up @@ -131,7 +132,11 @@ public class PolyComposeRuntime(private val host: PolyComposeClock = PolyCompose
}

public fun dispose() {
composition.dispose()
try {
composition.dispose()
} finally {
recomposer.cancel()
}
}

private companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.polyfrost.compose.runtime

import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.snapshots.Snapshot
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class PolyComposeClockTest {
@Test
fun failingCompositionDoesNotStopOthers() {
val clock = PolyComposeClock()
val tick = mutableIntStateOf(0)
var healthySeen = -1
val healthy = PolyComposeRuntime(clock)
val broken = PolyComposeRuntime(clock)
healthy.setContent {
val v = tick.intValue
SideEffect { healthySeen = v }
}
broken.setContent {
check(tick.intValue != 1) { "boom" }
}

for (i in 1..3) {
Snapshot.withMutableSnapshot { tick.intValue = i }
clock.frame()
assertEquals(i, healthySeen)
}

healthy.dispose()
broken.dispose()
}

@Test
fun failingEffectIsReportedNotUncaught() {
val thread = Thread.currentThread()
val original = thread.uncaughtExceptionHandler
val uncaught = mutableListOf<Throwable>()
thread.uncaughtExceptionHandler = Thread.UncaughtExceptionHandler { _, e -> uncaught.add(e) }
try {
val clock = PolyComposeClock()
val tick = mutableIntStateOf(0)
var healthySeen = -1
val healthy = PolyComposeRuntime(clock)
val broken = PolyComposeRuntime(clock)
healthy.setContent {
val v = tick.intValue
SideEffect { healthySeen = v }
}
broken.setContent {
val v = tick.intValue
LaunchedEffect(v) { check(v != 1) { "effect boom" } }
}

for (i in 1..3) {
Snapshot.withMutableSnapshot { tick.intValue = i }
clock.frame()
assertEquals(i, healthySeen)
}
assertTrue(uncaught.isEmpty(), "uncaught: $uncaught")

healthy.dispose()
broken.dispose()
} finally {
thread.uncaughtExceptionHandler = original
}
}

@Test
fun fatalErrorReachesUncaughtHandler() {
val thread = Thread.currentThread()
val original = thread.uncaughtExceptionHandler
val uncaught = mutableListOf<Throwable>()
thread.uncaughtExceptionHandler = Thread.UncaughtExceptionHandler { _, e -> uncaught.add(e) }
try {
val clock = PolyComposeClock()
val tick = mutableIntStateOf(0)
val broken = PolyComposeRuntime(clock)
broken.setContent {
if (tick.intValue == 1) throw StackOverflowError("fatal")
}

Snapshot.withMutableSnapshot { tick.intValue = 1 }
clock.frame()
assertTrue(uncaught.any { it is StackOverflowError }, "uncaught: $uncaught")

broken.dispose()
} finally {
thread.uncaughtExceptionHandler = original
}
}
}