Subscribe to var changes before invoke observer

This commit is contained in:
alex-greben
2023-04-10 17:03:14 +03:00
parent 76c9f6f0de
commit ecb2eebf38
2 changed files with 16 additions and 1 deletions
@@ -97,8 +97,8 @@ internal class VariableController {
}
private fun onVariableDeclared(variable: Variable) {
notifyVariableChanged(variable)
variable.addObserver(notifyVariableChangedCallback)
notifyVariableChanged(variable)
}
fun getMutableVariable(name: String): Variable? {
@@ -74,6 +74,21 @@ class VariableControllerTest {
Assert.assertEquals("non-initialized", lastValue)
}
@Test
fun `variable changed inside declaration callback`() {
val variable = Variable.StringVariable("late_init_variable", "declared_value")
var counter = 0
subscribe(variable.name) {
if (counter < 5) {
counter++
variable.set("new_value_$counter")
}
}
declareVariable(variable)
Assert.assertEquals(5, counter)
}
@Test
fun `null returned when no variable specified`() {
Assert.assertNull(variableController.getMutableVariable("unknown_variable"))