Skip to content

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

@DmitryEfimenko

Description

@DmitryEfimenko

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions