ngrx / platform

Reactive State for Angular
https://ngrx.io
Other
7.96k stars 1.95k forks source link

RFC: Tracking Signal Changes #4416

Open rainerhahnekamp opened 5 days ago

rainerhahnekamp commented 5 days ago

Information

Problem

Miscellaneous extensions such as DevTools, local storage sync, and undo/redo are constrained due to the glitch-free effect of Signals. This issue has already been raised in:

It appears that no official mid-term solution will be provided.

Proposed Solution

The Signal Store's patchState function offers the potential for a workaround.

patchState is the sole function capable of updating the state and is fully controlled by ngrx/signals. It could track its calls and provide necessary information to any SignalStore feature that requires it.

Ideally, this functionality should be incorporated into the core (see angular/angular#53703). Implementing patchState shouldn't be too complex and could be seen as low-hanging fruit.

If there is interest, I am willing to develop a prototype.


Example

type StateChangeCallback<State> = (prev: State, current: State, diff: Partial<State>) => void;

// Using it in a feature
const withDevTools = (name: string, stateChangeCallback: StateChangeCallback<Record<string, unknown>>) => {
  // ...
}

// Using it in SignalStore
signalStore(
  withMethods((store, stateChangeCallback) => {
    // ...
  }),
  withHooks((store, stateChangeCallback) => {
    // ...
  });
)

The signalStore factory, as described in issue #4340, would work well in combination with withDevTools from the example.

Documentation page

No response

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