Add NodeObserver

This commit is contained in:
Brahim Hadriche committed 2026-10-12 19:21:19 -04:00
1 parent c581a4767a
commit df4e16b846
6 files changed
+251

No files matched your search

@@ -0,0 +1,37 @@
import "pkg:/source/utils/AssertUtils.bs"
import "pkg:/source/utils/Types.bs"
' Observes a field from this component's scope, so stopping drops only this observation: UnobserveFieldScoped
' drops every observation of the field made from the calling component.
function Init()
m.top.ObserveField("target", FuncName(OnTargetChange))
m.top.ObserveField("nodeDestroy", FuncName(OnNodeDestroy))
end function
function OnTargetChange(event as object) as void
StopObserving()
target = event.GetData()
#if DEBUG
Assert.NodeHasField(target.node, target.fieldName)
#end if
m.node = target.node
m.fieldName = target.fieldName
m.node.ObserveFieldScoped(m.fieldName, FuncName(OnFieldChange))
end function
function OnFieldChange() as void
StopObserving()
m.top.changed = true
end function
function OnNodeDestroy() as void
StopObserving()
end function
function StopObserving() as void
if m.node <> invalid
m.node.UnobserveFieldScoped(m.fieldName)
m.node = invalid
end if
end function
@@ -0,0 +1,11 @@
<component name="NodeObserver" extends="Node">
<interface>
<!-- { node, fieldName }: the field to observe. -->
<field id="target" type="assocarray" />
<!-- Set to true on the field's first change; the field is no longer observed after that. -->
<field id="changed" type="boolean" />
<!-- Set by the creator when done: stops observing. Dropped while observing on a task thread, the observer stays
registered on the field even after the task ends. -->
<field id="nodeDestroy" type="boolean" />
</interface>
</component>
@@ -0,0 +1,17 @@
' Observes source.value through a NodeObserver made on this task's thread, and blocks in wait until it changes.
function Init()
m.top.functionName = "WaitForChange"
end function
function WaitForChange() as void
port = CreateObject("roMessagePort")
observer = CreateObject("roSGNode", "NodeObserver")
observer.target = { node: m.top.source, fieldName: "value" }
observer.ObserveField("changed", port)
clock = CreateObject("roTimespan")
m.top.ready = true
if wait(2000, port) <> invalid
m.top.changedAtMs = clock.TotalMilliseconds()
end if
observer.nodeDestroy = true
end function
@@ -0,0 +1,7 @@
<component name="NodeObserverTestTask" extends="Task">
<interface>
<field id="source" type="node" />
<field id="ready" type="boolean" />
<field id="changedAtMs" type="integer" value="-1" />
</interface>
</component>
@@ -0,0 +1,64 @@
import "pkg:/components/NodeObserver/NodeObserver.bs"
namespace Tests
' Runs inside a NodeObserver, so a spec can call its handlers: a throw in a field's handler crashes the app
' instead of reaching the code that set the field.
@SGNode("NodeObserver")
@suite
class NodeObserverOnRenderThreadTests extends Rooibos.BaseTestSuite
protected override function beforeEach()
m.source = CreateObject("roSGNode", "Node")
m.source.addField("value", "integer", false)
m.port = CreateObject("roMessagePort")
end function
function CreateObserver() as object
observer = CreateObject("roSGNode", "NodeObserver")
observer.target = { node: m.source, fieldName: "value" }
observer.ObserveField("changed", m.port)
return observer
end function
@describe("NodeObserver made on the render thread")
@it("sets changed on the field's first change only")
function _()
observer = m.CreateObserver()
m.source.value = 1
m.assertNotInvalid(wait(500, m.port))
m.source.value = 2
m.assertInvalid(wait(100, m.port))
m.assertTrue(observer.changed)
end function
@it("stops observing on nodeDestroy")
function _()
observer = m.CreateObserver()
observer.nodeDestroy = true
m.source.value = 1
m.assertInvalid(wait(100, m.port))
m.assertFalse(observer.changed)
end function
@it("throws in DEBUG on a field the node doesn't have")
function _()
event = {
target: { node: m.source, fieldName: "missing" }
GetData: function() as object
return m.target
end function
}
message = invalid
try
OnTargetChange(event)
catch error
message = error.message
end try
m.assertEqual(message, "Assertion failed: Node does not have field 'missing'. <invalid>")
end function
end class
end namespace
@@ -0,0 +1,115 @@
namespace Tests
@suite
class NodeObserverTests extends Rooibos.BaseTestSuite
protected override function beforeEach()
m.source = CreateObject("roSGNode", "Node")
m.source.addField("value", "integer", false)
m.port = CreateObject("roMessagePort")
end function
function CreateObserver() as object
observer = CreateObject("roSGNode", "NodeObserver")
observer.target = { node: m.source, fieldName: "value" }
return observer
end function
' Every message on the port within waitMs of the previous one, as "<field>=<data>".
function Messages(waitMs as integer) as object
messages = []
message = wait(waitMs, m.port)
while message <> invalid
messages.Push(`${message.GetField()}=${message.GetData()}`)
message = wait(waitMs, m.port)
end while
return messages
end function
@describe("NodeObserver")
@it("sets changed on the field's first change only")
function _()
observer = m.CreateObserver()
observer.ObserveField("changed", m.port)
m.source.value = 1
m.assertEqual(m.Messages(500), ["changed=true"])
m.source.value = 2
m.assertEqual(m.Messages(100), [])
end function
@it("stops observing on nodeDestroy")
function _()
observer = m.CreateObserver()
observer.ObserveField("changed", m.port)
observer.nodeDestroy = true
m.source.value = 1
m.assertEqual(m.Messages(100), [])
m.assertFalse(observer.changed)
end function
@it("moves to a new target, leaving the previous one")
function _()
other = CreateObject("roSGNode", "Node")
other.addField("value", "integer", false)
observer = m.CreateObserver()
observer.ObserveField("changed", m.port)
observer.target = { node: other, fieldName: "value" }
m.source.value = 1
m.assertEqual(m.Messages(100), [])
other.value = 1
m.assertEqual(m.Messages(500), ["changed=true"])
end function
@it("stops without dropping the field's other observers")
function _()
m.source.ObserveFieldScoped("value", m.port)
stopped = m.CreateObserver()
kept = m.CreateObserver()
kept.ObserveField("changed", m.port)
stopped.nodeDestroy = true
m.source.value = 1
m.assertEqual(m.Messages(500), ["value=1", "changed=true"])
m.source.value = 2
m.assertEqual(m.Messages(100), ["value=2"])
end function
@it("wakes a task blocked in wait when another thread sets the field")
function _()
task = CreateObject("roSGNode", "NodeObserverTestTask")
task.source = m.source
task.control = "run"
clock = CreateObject("roTimespan")
while task.ready <> true and clock.TotalMilliseconds() < 3000
sleep(10)
end while
sleep(100)
m.source.value = 1
while task.changedAtMs = -1 and clock.TotalMilliseconds() < 5000
sleep(10)
end while
m.assertTrue(task.changedAtMs >= 100 and task.changedAtMs < 500, `changed at ${task.changedAtMs} ms`)
end function
@it("leaves nothing observing the field once it changed or stopped")
function _()
' Held, so only the observer's own unobserve can end its observation.
held = []
for i = 1 to 1000
held.Push(m.CreateObserver())
m.source.value = i
stopped = m.CreateObserver()
stopped.nodeDestroy = true
held.Push(stopped)
end for
' A field set costs about 1µs per observation the field holds, or has held at once.
clock = CreateObject("roTimespan")
for i = 1 to 200
m.source.value = -i
end for
m.assertTrue(clock.TotalMilliseconds() < 50, `200 sets took ${clock.TotalMilliseconds()} ms`)
end function
end class
end namespace