Aurelia - Computed property is re-calculated even when nested computed property has not changed - aurelia

I have computed property A which is dependent on X (#computedFrom('X')) and computed property B which is dependent on A (#computedFrom('A')).
My question is:
if x changes, but the result of A does not should B be re-calculated? Is this by design?

Related

How to handle changing props inside a component

I'm making a Form wrapper and I'm wondering how to handle data object prop.
From what I have seen out there, there are some (like FormKit) that mutate the prop object that holds the data. This is not a recommended way according to the official docs. Then there are those (like VueFormulate) that create a shallow copy of the data object before emiting the changed object. To support nested data object you need to deep copy the data object on every change. This seems wasteful with large forms.
Is there an alternative where you don't deep copy an object and you don't mutate object prop?
You could possibly use v-model. as the docs will say, it's shorthand for the child component having a property bound from the parent and when a change event occurs you emit an update event from the child to the parent to have the parent update the property, and the child will subsequently sync to the new value.
ie:
childComponent *has value change*
onChange (value) => this.$emit("updateValue", value)
parentComponent *recieved "updateValue" event*
onUpdateValue(value) => this.parentValue = value

Why mobx computed getter doesn't call after change value

I have TestStore and observable property fields.
When I click to any name, i call action changeOneName and change some object inside fields. Computed getter hasError called again and i see console.log("hasError computed");
Why don't I see console.log("valueFields computed"); second time after changing name to 'ErrorName'?
https://codesandbox.io/s/vibrant-lumiere-cv2tp?file=/src/TestStore.js
You only changed name property of the object, not the object itself. And Object.values only dereferences direct values (objects) of your fields object, not the inner things, like name. So computed don't need to rerun because things that were referenced in that computed didn't change.
hasError did rerun because you actually dereference name property inside of it, so when name changes it reruns.
Hope it makes sense.

Computed property is called multiple times in Aurelia

I have a computed property which is dependent on A and B:
#computedFrom(A, B)
get property() {
}
The property is used in element which has repeat.for (5 elements in total):
<element repeat.for 1 to 5 elementProperty=$"{property}">
</element>
When value A changes, I would expect property() to be called just once, and all elements updated with the new value.
But property() is called 6 times.
Why?
Edit:
When I do this...
<template repeat.for 1 to 5>
<element elementProperty=$"{property}"></element>
</template>
...property() is called 6 times too.
Is this how it should work?
If you have a repeat.for, then the source property of any bindings in it is called for each child element that is rendered. This is normal behavior as they are all separate binding instances.
If your binding source changes, all binding targets (in this case 6) need to be updated and that happens by calling the source property. Computed bindings tend to be such simple computations that this is generally not an issue.
If your computed bindings are computationally expensive, you might want to consider using a property observer for the dependent properties and simply setting the computed property when any of them change, rather than letting the binding engine call it on binding.
EDIT
To clarify: this doesn't really have anything to do with computedFrom. A non-computed property would also be read 6 times but there is no getter to hook the debugger on, so you can't see that directly.

Swift: Computed type properties in classes

I'm trying to understand something about type properties in swift.
The Swift Programming Language says
For classes, you can define computed type properties only
So a computed property does not store a value itself, but it is calculated. That I understand. But I don't get how such a thing can apply to type properties. Such properties belong to the class itself and not to an instance of it.
So if you use a getter for such a computed type property, what could you possibly use to calculate it? It can't be any other type properties, as they too can only be computed properties. You would get a sort of loop of computed properties because there aren't any stored type properties.
In the same way, I also don't get what a setter would do. If you call the setter of a computed type property, what can it set? There are no stored type properties to be set.
Bear in mind that stored class properties are only unsupported at the moment. The compiler error you get when you try to use them—"Class variables not yet supported"— suggests that they're on their way. Computed class properties don't necessarily have to make sense on their own.
However, computed properties don't always have to be based on the values of stored data. As it stands, you could use them for "static" read-only values associated with the class, say:
class var ThisIsAClassConstant: String { return "Woo" }
And people have already come up with ways to store associated values, for example, in the first two singleton patterns in this answer, the class property stores its state in either a global (but private) variable, or in a static variable inside a nested structure.
These are obviously a bit "workaroundy", but they're a way of achieving class-like storage while it's not officially implemented.

How are to-many attributes specified in a fetched property?

I have two entities, Parent and Child. The Parent entity has a to-many relationship to Child named "children." Child has a String attribute named "childName."
I want to make a fetched property on Parent, let's call it "specialChild" that returns a Child with a particular name, let's say "Special". The following predicates return an empty set when I access the fetched property:
children.childName == "Special"
SUBQUERY(children, $eachChild, $eachChild.childName =
"Special").#count > 0
SUBQUERY(children, $eachChild, ANY $eachChild.childName =
"Special").#count > 0
I believe I'm messing up the predicate somehow, because I'm still pretty inexperienced with them. (and I can find zero documentation from Apple on "SUBQUERY") How am I supposed to specify "the child whose childName is Special" in the Parent's fetched property predicate?
Yes, I am calling -refreshObject:mergeChanges: but I still receive an empty result. Yes, the destination entity is Child.
What you want is parent==$FETCH_SOURCE AND childName=="Special". This gets any Child whose childName is "Special" and whose parent is the object looking up its special children.
Attributes in a fetched property predicate must exist on the destination entity. Here the destination is Child, so you can't use children since that only exists on Parent.
The $FETCH_SOURCE part corresponds to where you'd use self if you wrote the predicate in code. Without that you get every special child, not just the ones attached to the originating Parent. It says, the parent attribute of the child must be the specific instance looking up the fetched property value.