immersive-home/app/addons/rdot/state.gd

51 lines
1.3 KiB
GDScript3
Raw Normal View History

2024-04-09 16:30:23 +03:00
extends RdotNode
2024-04-09 18:11:24 +03:00
class_name RdotStateInternal
2024-04-09 16:30:23 +03:00
var equal: Callable = func(this, a, b): a == b
var value: Variant = null
static func createSignal(initialValue: Variant):
2024-04-09 18:11:24 +03:00
var node = RdotStateInternal.new()
2024-04-09 16:30:23 +03:00
node.value = initialValue
var getter = func():
RdotGraph.getInstance().producerAccessed(node)
return node.value
return [getter, node]
static func setPostSignalSetFn(fn: Callable) -> Callable:
var graph := RdotGraph.getInstance()
var prev = graph.postSignalSetFn
graph.postSignalSetFn = fn
return prev
2024-04-09 18:11:24 +03:00
static func signalGetFn(this: RdotStateInternal):
2024-04-09 16:30:23 +03:00
RdotGraph.getInstance().producerAccessed(this)
return this.value
2024-04-09 18:11:24 +03:00
static func signalSetFn(node: RdotStateInternal, newValue: Variant):
2024-04-09 16:30:23 +03:00
var graph := RdotGraph.getInstance()
assert(graph.producerUpdatesAllowed())
if !node.equal.call(node.wrapper, node.value, newValue):
node.value = newValue
signalValueChanged(node)
2024-04-09 18:11:24 +03:00
static func signalUpdateFn(node: RdotStateInternal, updater: Callable):
2024-04-09 16:30:23 +03:00
var graph := RdotGraph.getInstance()
assert(graph.producerUpdatesAllowed())
signalSetFn(node, updater.call(node.value))
2024-04-09 18:11:24 +03:00
static func signalValueChanged(node: RdotStateInternal):
2024-04-09 16:30:23 +03:00
var graph := RdotGraph.getInstance()
node.version += 1
graph.producerIncrementEpoch()
graph.producerNotifyConsumers(node)
if !graph.postSignalSetFn.is_null():
graph.postSignalSetFn.call()