storage icon indicating copy to clipboard operation
storage copied to clipboard

introduce convenience methods to select/set particular property of the state

Open DmitryEfimenko opened this issue 4 years ago • 1 comments

Is your feature request related to a problem? Please describe.

Since retrieving distinct changes for a particular state property is a very common scenario, I'd suggest introducing a method that would do that for you. Similar, it'd be great if there was a method for updating a particular property of the state:

Describe the solution you'd like

pseudo code: observe value changes to a particular state prop:

selectKey<K extends keyof StateType>(key: K): Observable<StateType[K]> {
  return this.valueStream.pipe(
    pluck(key),
    distinctUntilChanged()
  );
}

update a particular state prop:

private updateKey<K extends keyof StateType>(
  key: K,
  updaterOrNewVal: StateType[K] | ((currentValue: StateType[K]) => StateType[K])
) {
  this.bucket.set(state => {
    const prev = state[key];
    const updated = typeof updaterOrNewVal === 'function'
      ? updaterOrNewVal(prev)
      : updaterOrNewVal;
    return { ...state, [key]: updated };
  });
}

Describe alternatives you've considered

Additional context

DmitryEfimenko avatar Feb 04 '22 23:02 DmitryEfimenko

This is a cool idea! PRs are welcome!

jacksteamdev avatar Feb 06 '22 17:02 jacksteamdev