UmbObjectState, and "Deep" updates
# package-development
k
So, Before i go and "re-architect" something, quick question about update. If i have an UmbObjectState of an object and i want to update one value on it i can
Copy code
ts
this.#data.update({name: nameValue})
and that would update the name property. however if my object has nested stuff. eg.
Copy code
ts
export type myCustomThings = {
   id : string;
   name: string;
   options: myCustomOptions
}

export type myCustomOptions = {
    enabled: boolean,
    useColors: boolean
}
then is there a nice way to update these child options individually, without you know having to read them all in and work it out myself 🙂 e.g
Copy code
ts
this.#data.update({options : { useColors: false }});
doesn't work, because it replaces the whole 'options' method. So i obviously need to nest somehow ?
m
Not sure if spread operator would be something for this? But I guess you would still have to add spread operators in places you know need to change. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
k
at the moment i have this : not 100% happy with it...
Copy code
ts
setSyncOption(alias: string, option: SyncSendOption) {
       
    var settings = this.#data.getValue()?.sendSettings;
    if (!settings) return;

    const cloneSettings = structuredClone(settings);
    cloneSettings[alias as keyof SyncSendOptions] = option;
    this.#data.update({sendSettings : cloneSettings});
}
h think spread does also work @Markus Johansson
Copy code
ts
  var mergedSettings = { ...settings, ...{ [alias] : option}};
  this.#data.update({sendSettings: mergedSettings});
I just worry i will come back to this line of code in 12 months and not have a clue how it works 🙂
m
hahah 😄 I get the performance benefit of checking for object equality but it kind of sucks when you have nested structures like this. There is also things like "immerJS" but I think that it might be overkill
I have the same feeling about spread operator but it what the cool kids are using these days 😄
k
i suspect/hope performance isn't a big issue, this is a ui page with a few tick boxes on. so just making sure the base model gets updates property. it all works. yes cool kids => spread operators everywhere, but i still need to look up what
[...^1]
or something actually means everytime is see it !
m
I think that these kind of optimizations might be needed when you build the Azure backend - not a simple JS-view 😄 I feel you man!
2 Views