I'm trying to subscribe to property changes in my component model delegate, and usage of PropertyTreeChangeEvent
is tripping me up.
My goal: subscribe to an array of objects at path this.props.tags
.
1: Why do I have to call resolveReference()
? How is that different than startup?
override fun onStartup() {
log.infof("Starting up delegate for '%s'!", component.componentAddressPath)
tags = component.createPropertyReference("this.props.tags", onValueChange, Origin.ANY)
tags.resolveReference()
tags.startup()
}
2: What is the distinction between these two?
val onValueChange: (changeEvent: PropertyTreeChangeEvent) -> Unit = { changeEvent ->
changeEvent.readValue() // Maybe an array of a QualifiedValues? Why?
changeEvent.readCausalValue() // This one seems to make more sense.
}
Answered my own question 2:
changeEvent.readValue() // Returns the value of the subscribed property. i.e. always "this.props.tags"
changeEvent.readCasualValue() // Returns the value of the property at the path that changed. i.e. possibly "this.props.tags[0].tagPath"
2 Likes
You don't want to use createPropertyReference
; that's more "internal"/"under the hood" machinery. If you want to subscribe, you should be...subscribing
First, obtain the backend PropertyTree
for your property type:
props = component.getPropertyTreeOf(PropertyType.props);
Then you can issue a one-time read:
props.read("path").orElse(null)
Or create a subscription:
props.subscribe("path", Origin.ANY, someLambda)
You should maintain that list of Subscription
s returned by subscribe
, and in your model's onShutdown
make sure each one unsubscribes.
2 Likes
Awesome, thanks Paul.
I thought the whole thing seemed more convoluted than necessary