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

Create a Set/Map signal wrappers #489

Open
Harpush opened this issue Aug 26, 2024 · 5 comments
Open

Create a Set/Map signal wrappers #489

Harpush opened this issue Aug 26, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@Harpush
Copy link

Harpush commented Aug 26, 2024

When working with Set and Map you can't wrap it in a signal as they are mutable. You can however create an abstraction around it to emulate a signal Set/Map.

I will be happy to create a PR

@eneajaho
Copy link
Collaborator

It reminds me of this one https://x.com/Enea_Jahollari/status/1826278131675586608 😅

@eneajaho eneajaho added the enhancement New feature or request label Aug 27, 2024
@Harpush
Copy link
Author

Harpush commented Aug 27, 2024

@eneajaho so what do you think? Should I work on that? Does it sound like a good addition?

@eneajaho
Copy link
Collaborator

eneajaho commented Sep 5, 2024

what about reactive arrays?
This is a wide topic which comes to the part of -> should we do this? or should angular itself provide these?

@Harpush
Copy link
Author

Harpush commented Sep 5, 2024

what about reactive arrays? This is a wide topic which comes to the part of -> should we do this? or should angular itself provide these?

I think arrays are different. There are known pattern how to use them immutably as they are serializable and state managements support them usually.
Set and Map are mutable by design and unlike arrays have no immutable patterns.

@Harpush
Copy link
Author

Harpush commented Sep 14, 2024

@eneajaho The idea I have is:

export interface SetSignal<T> {
  (): ReadonlySet<T>
  readonly array: Signal<readonly T[]>
  readonly size: Signal<number>
  has(value: T): boolean
  add(values: T | T[]): void
  delete(values: T | T[]): void
  clear(): void
  replace(values: T[]): void
}

const valuesSet = createSetSignal([2, 3, 4, 4]); // SetSignal<number>

array, size and has are reactive while the others are not

I also think a good addition is allowing for the mirror technique here:

const values = signal([1,3,3]);
const valuesSet = createSetSignal(values); // SetSignal<number>
console.log(valuesSet.array()); // [1,3]
values.set([1,2,1]);
console.log(valuesSet.array()); // [1,2]

The biggest benefit here is that Set is much better than Record<string, true> in terms of performance

What do you think?

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

No branches or pull requests

2 participants