Umbraco.CMS.Backoffice icon indicating copy to clipboard operation
Umbraco.CMS.Backoffice copied to clipboard

[ISSUE] UmbArrayState or UmbObjectState with nested state throws when observing

Open enkelmedia opened this issue 1 year ago • 0 comments

Describe the isses First of all, this might be intentional from the core team but I wanted to raise this to understand why, how to get around the issue and maybe also as a placeholder for something to write documentation about.

I'm working on a list of "things" where each thing have a couple of properties that I would like to make observable. Because of the deepFreeze going on in UmbDeepState observables stored in UmbArrayState and UmbObjectState can accept new observers and this exception is thrown:

VM12842 chunk-PUA7J4RB.js:638 Uncaught TypeError: Cannot assign to read only property 'currentObservers' of object '#'

Here is a made up example:

export class CarList {
  id = new UmbStringState(undefined);
  cars = new UmbArrayState<Car>([],(c)=>c.id);
  
  constructor(id : string) {
    this.id.setValue(id);
  }

}

export class Car {

  id = new UmbStringState(undefined);
  owners = new UmbArrayState<CarOwner>([],(o)=>o.name);

  constructor(id : string) {
    this.id.setValue(id);
  }
}

export class CarOwner {
  name:string;
  city :string;

  constructor(name:string,city:string) {
    this.name = name;
    this.city = city;
  }
}

const cars : Car[] = [];
cars.push(new Car("car1"));
cars.push(new Car("car2"));

const carList = new CarList("list1");

carList.cars.setValue(cars);

const car1 = carList.cars.getValue()[0];

// This will throw because #currentObservers is frozen.
car1.owners.asObservable().subscribe((owner)=> {
  console.log('ownwer');
});

Implementing the same thing as above using BehaviorSubject and RxJs directly works fine. I'm wondering, is this intentional and why? Is the approach that I'm trying to take "bad"?

enkelmedia avatar May 01 '24 14:05 enkelmedia