Best way to expose a readonly signal array in an angular service

Discussão em 'Angular' iniciado por Gabriel, Dezembro 24, 2024.

  1. Gabriel

    Gabriel Guest

    I am using Angular signals in a service

    @Injectable()
    export class MyService {
    private _myObjects = signal<MyObject[]>([]);
    }


    I want to expose this array to components in a read-only way. I see two approaches:

    myObjects = this._myObjects.asReadonly();


    and

    myObjects = computed(() => myDeepCopy(this._myObjects()));
    // where myDeepCopy is whatever deep copy function you can imagine


    The first approach does not actually protect against mutation of the underlying array in the component but it is faster as I don't need to create a new deep copy each time _myObjects changes. Moreover, because it is marked as readOnly, I feel like it implies that a consumer should not try to mutate the underlying array (even if it does not enforce this rule).

    So my question is: What is the best practice here? Should I just mark it as readOnly and trust the consumer not to mutate the underlying array or should I expose a deep copy so that there is absolutely no risk even if it comes at a small performance penalty and a bit less readable code?

    Continue reading...

Compartilhe esta Página