nank1ro / solidart

Signals in Dart and Flutter, inspired by SolidJS
https://docs.page/nank1ro/solidart
MIT License
163 stars 7 forks source link

Make interaction with collections (List, Set, Map) easier #66

Closed nank1ro closed 1 year ago

nank1ro commented 1 year ago

Problem

Currently dealing with signals with collections as values is a bit painful. Let us take a very simple example:

final numbers = Signal([1, 2]);
// you may expect this to work
numbers.value.add(3);
// but the `numbers` signal will not update and won't notify listeners because you're modifying the value of its instance directly

// instead you have to provide a new instance
numbers.value = [...numbers.value, 3];

This is the same behaviour present in ValueNotifier by Flutter.

Proposal

In SolidJS there is an object called Store, a store is a collection of Signals. What this mean is that each value of the Store is a Signal itself, and you can listen to each one individually.

In solidart I don't think this is needed because it may add an extra complexity and the goal of solidart is to stay simple.

Instead I'd propose to create 3 new kind of signals:

  1. ListSignal, to interact with a list of objects
  2. SetSignal, to interact with a set of objects
  3. MapSignal, to interact with a map of objects

The previous example will become as simple as:

final numbers = ListSignal([1, 2]);
numbers.add(3);

This will make solidart even easier and will give a better developer experience for its users.