Skip to content

RxState should be readonly? #1449

@jon9090

Description

@jon9090

Description

When I change the company object, it changes the store, also.

Which causes unexpected behavior in the state.

Why does the store not readonly? (using object freeze like ngrx)

vm$ = this.state.select(selectSlice(["company"])).pipe(
    map(({ company }) => ({ company })),
    tap(({ company }) => {
      company.id = 2; // <-------------- I change the id here, and it change the store because it's ref object.
      console.log({ company });
    })
  );

https://codesandbox.io/s/condescending-thompson-e51sh1?file=/src/app/app.component.ts:334-670

   template: `
    in app! vm = {{ vm$ | async | json }}<br />
    state = {{ state$ | async | json }}                          <---- state changed.
  `,
  providers: [RxState]
})
export class AppComponent {
  title = "CodeSandbox";

  state$ = this.state.select();

  vm$ = this.state.select(selectSlice(["company"])).pipe(
    map(({ company }) => ({ company })),
    tap(({ company }) => {
      company.id = 2; // <-- should be readonly??
      console.log({ company });
    })
  );

  constructor(private state: RxState<{ company: { id: number } }>) {
    this.state.set({ company: { id: 1 } });
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    { } State@rx-angular/state related

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions