Why do you remove the lastValue?
AnnaFrom19 opened this issue · 2 comments
Hi!
Excuse me, can you explain why do you remove lastValue for setter and getter?
In 8.* version we have smth like this:
const OldComponentDefinition = {
tag: 'my-component',
param: {
get: (host, lastValue) => { lastValue ?? 9 },
set: (host, value, lastValue) => { lastValue > value ? lastValue : value },
}
}
But now only:
const NewComponentDefinition = {
tag: 'my-component',
param: {
value: (host, value) => {
// ???
}
}
}
Is there any way to get current value of param?
The value
is the lastValue
from the getter. The missing is only the third argument, which was removed for several reasons:
- It rarely helped, as there is an
observe
method, so you can watch and do action based on the change of the value - It would be very complicated to keep it with the merged
value()
method, as you can have different scenarios when that function is called - after the assertion, after the dependency update, etc.. in those situations thelastValue
would behave differently, so to avoid confusion, it was removed - It has been a stateful pattern - as you rely on the value set in runtime rather than on the passed arguments
Your example is a very good case for the above reasons. Your param
is a strange value, as it only allows you to increase the value, and silently skip if the new value is lower. What should be a callback in the first place, you put into the property calculation (it was just fine before v9):
I am not sure what is the structure, and how the value is passed to your component, but instead of applying this logic inside of the property definition, it should be applied outside of it.
define({
tag: 'my-component',
input: {
value: 0,
observe(host, value) {
if (value > host.param) host.param = value;
},
},
param: 9,
});
Thanks for the answer. Then I will change my application logic. I think it will be even clearer in the new version.