Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance DeepSignal and toDeepSignal to support arrays #4556

Open
1 of 2 tasks
samuelfernandez opened this issue Oct 12, 2024 · 2 comments
Open
1 of 2 tasks

Enhance DeepSignal and toDeepSignal to support arrays #4556

samuelfernandez opened this issue Oct 12, 2024 · 2 comments

Comments

@samuelfernandez
Copy link
Contributor

Which @ngrx/* package(s) are relevant/related to the feature request?

signals

Information

Right now deep signals only support record-like signals. Ideally it would also support array-like structures, providing the same “deep” behavior.

It would be more tricky, since it’d need to proxy mutation operations, but should be technically possible.

Describe any alternatives/workarounds you're currently using

No response

I would be willing to submit a PR to fix this issue

  • Yes
  • No
@markostanimirovic
Copy link
Member

Can you provide an example of how the usage of arrays with deep signals within the SignalStore/State should look like? How the state update would look like?

@samuelfernandez
Copy link
Contributor Author

Can you provide an example of how the usage of arrays with deep signals within the SignalStore/State should look like? How the state update would look like?

@markostanimirovic Sure! Doing a quick comparison with records. When T extends a record:

type DeepSignal<T extends Record<any,any>> = Signal<T> & { [K in keyof T]: DeepSignal<T[K]> };

interface Person {
  name: string;
}

const deepPersonSignal: DeepSignal<Person> = /**/;

deepPersonSignal(); // Type: Person, emits reactively the person object.
deepPersonSignal.name(); // Type: string, signal that emits reactively the name.

My proposal, when the generic is an array, would be having this:

type DeepSignal<T extends R[]> = Signal<R[]> & { [K in keyof T]: DeepSignal<T[K]> } & {
  length: Signal<number>,
  entries: Signal<IterableIterator<[number, R]>>,
  keys: Signal<IterableIterator<number>>,
  values: Signal<IterableIterator<R>>
};

const deepPersonsSignal: DeepSignal<Person[]> = /**/;

deepPersonsSignal(); // Type: Person[], emits reactively the person array.
deepPersonsSignal[0](); // Type: Person, signal that emits reactively the first element of the array.
deepPersonsSignal[0].name(); // Type: string, signal that emits reactively the name of the first element.

Connecting to @alxhub's comment here: ducin/sygnalyze#1 (comment)

It'd be ideal that the deep signal of arrays didn't emit if only values are updated, but the array length is the same. As pointed out by @alxhub it would enhance the performance of @for control flow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants