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

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

Open
DmitryEfimenko opened this issue Feb 4, 2022 · 1 comment

Comments

@DmitryEfimenko
Copy link

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

@jacksteamdev
Copy link
Contributor

This is a cool idea! PRs are welcome!

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

No branches or pull requests

2 participants