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

Add optional callback argument to set() method for the repeating complex actions with state #1726

Closed
PaulMaly opened this issue Sep 11, 2018 · 6 comments

Comments

@PaulMaly
Copy link
Contributor

@Rich-Harris
Right now, if we need to make some multiple manipulations with the state we need doing something like:

const { foo, bar } = this.get();
++foo;
bar = 'baz';
this.set({ foo, bar });

I think it'll be nice if we'll able to create some function which will receive the current state as an argument and return a new state, and use this function as an argument in methods set():

export function changeFooBar({ foo, bar }) => {
   ++foo;
   bar = 'baz';
   return { foo, bar };
}
...
import { changeFooBar } from '...';
...
this.set(changeFooBar);

I think I can provide a PR for that change. Seems it's just a few lines of code.

@mrkishi
Copy link
Member

mrkishi commented Sep 11, 2018

Even though it'd be a small change, wouldn't it still complicate set unnecessarily? I'm generally against polymorphic functions whenever possible.

Other options include:

// explicitly passing state
this.set(changeFooBar(this.get()))

// abandoning functional helper
export function mutateFooBar(target) {
    let { foo, bar } = target.get()
    ++foo
    bar = 'baz'
    target.set({ foo, bar })
}

@Conduitry
Copy link
Member

Yeah I'm definitely leaning against including this in core of Svelte. It does sound like a reasonable enough contribution for svelte-extras though.

@PaulMaly
Copy link
Contributor Author

PaulMaly commented Sep 11, 2018

@Conduitry Yes and right now I use it like an external method which I need to include in every component in my app (!!!) because of 90% of my state changes are too complex to just using built-in set(). So, I suppose one line of code (!!!), without breaking changes, sound like a reasonable price to simplify my work. I'm sure it'll be useful for the others too.

@mrkishi
Copy link
Member

mrkishi commented Sep 12, 2018

I don't understand. Even with the change, you'd still be including the external utilities?

@PaulMaly
Copy link
Contributor Author

@mrkishi It’s not mandatory with this change.

But we’ll be able to import complex state manipulations just like we do it with events, actions and transitions.

@PaulMaly
Copy link
Contributor Author

Not relevant to v3.

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

3 participants