Skip to content

Commit

Permalink
Shared bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownJoe796 committed Dec 4, 2024
1 parent 066bb41 commit 5fbcdb7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ abstract class DependencyTracker{
open fun cancel() {
dependencies.forEach { it.second() }
dependencies.clear()
log?.log("Cleared dependencies")
}

protected fun dependencyBlockStart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package com.lightningkite.kiteui.reactive

import com.lightningkite.kiteui.Console
import com.lightningkite.kiteui.InternalKiteUi
import com.lightningkite.kiteui.printStackTrace2
import com.lightningkite.kiteui.report
Expand All @@ -18,6 +19,7 @@ fun <T> shared(coroutineContext: CoroutineContext = Dispatchers.Unconfined, useL

class SharedReadable<T>(
coroutineContext: CoroutineContext = Dispatchers.Unconfined,
val log: Console? = null,
useLastWhileLoading: Boolean = false,
private val action: ReactiveContext.() -> T
) : Readable<T>, CalculationContext {
Expand All @@ -29,11 +31,13 @@ class SharedReadable<T>(
throwable.report("SharedReadable")
}
}
override val coroutineContext = job + restOfContext
// override val coroutineContext = job + restOfContext
override val coroutineContext get() = job + restOfContext

private fun cancel() {
job.cancel()
job = Job()
scope.cancel()
}

private val scope = TypedReactiveContext(this, action = action)
Expand All @@ -45,16 +49,21 @@ class SharedReadable<T>(
}
private var lcount = 0
override fun addListener(listener: () -> Unit): () -> Unit {
log?.log("addListener $lcount $listener")
if (lcount++ == 0) {
log?.log("startCalculation")
scope.startCalculation()
}
val r = scope.addListener(listener)
var removed = false
return label@{
log?.log("remover called ($removed) for $listener")
if(removed) return@label
removed = true
log?.log("remover activated ($removed, $lcount) for $listener")
r()
if (--lcount == 0) {
log?.log("cancelling")
cancel()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,43 @@ class SharedTest {
assertEquals(1, onRemoveCalled)
}

@Test fun sharedTerminatesWhenNoOneIsListeningCancelDeps() {
var onRemoveCalled = 0
var scopeCalled = 0
var dependencyListeners = 0
val listener = object: Listenable {
override fun addListener(listener: () -> Unit): () -> Unit {
dependencyListeners++
return { dependencyListeners-- }
}
}
val shared = shared {
rerunOn(listener)
scopeCalled++
onRemove { onRemoveCalled++ }
42
}
assertEquals(0, scopeCalled)
assertEquals(0, onRemoveCalled)
assertEquals(0, dependencyListeners)
var removeListener = shared.addListener { }
assertEquals(1, dependencyListeners)
assertEquals(1, scopeCalled)
assertEquals(0, onRemoveCalled)
removeListener()
assertEquals(0, dependencyListeners)
assertEquals(1, scopeCalled)
assertEquals(1, onRemoveCalled)
removeListener = shared.addListener { }
assertEquals(1, dependencyListeners)
assertEquals(2, scopeCalled)
assertEquals(1, onRemoveCalled)
removeListener()
assertEquals(0, dependencyListeners)
assertEquals(2, scopeCalled)
assertEquals(2, onRemoveCalled)
}

@Test fun sharedSharesCalculations() {
var hits = 0
val property = Property(1)
Expand Down

0 comments on commit 5fbcdb7

Please sign in to comment.