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

Setter does not works. #22

Closed
ja3abuser opened this issue Sep 1, 2022 · 1 comment
Closed

Setter does not works. #22

ja3abuser opened this issue Sep 1, 2022 · 1 comment

Comments

@ja3abuser
Copy link

ja3abuser commented Sep 1, 2022

Hey! Looks like setter does not works for me.

export const userStore = asyncable<User>(
  fetchUserData,
  async () => {
    return {...await fetchUserData(), changed: true}
  },
);

userStore.set(await userStore.get());
console.log(await userStore.get()) // returns original object, without "changed" property.
@PaulMaly
Copy link
Member

PaulMaly commented Sep 1, 2022

Hi! I'm not sure what exactly you need to do, but there is an assumption that you are using setter not for its intended purpose.

Firstly, setter designed to perform some outgoing side-effect when asyncable store value has changed.
Secondery, you try to set the same value. asyncable store is smart enough to don't do anything if nothing is changed.
Lastly, setter callback is not set the store value and returning something from this function is does not perform any action.

The basic example with setter:

export const userStore = asyncable(fetchUserData, saveUserData);
<!-- this subscription to the store will perform `getter` callback to fetch user data from the server -->
{#await $userStore} 
   <p>Loading user data...</p>
{:then user}
  <form on:submit|preventDefault={save}>
     <input name="firstName" value={user.firstName}>
     <input name="lastName" value={user.lastName}>
     <input name="email" value={user.email}>
     <button>Save</button>
  </form>
{/await}

<script>
  import { userStore } from './stores/userStore';
     
  function save(e) {
    const formData = new FormData(e.target);
    const userData = Object.fromEntries(formData.entries());

    // this store change will perform `setter` callback and send user data updates to the server
    userStore.set(userData); 
  }
</script>

Hope it would help you! Feel free to ask additional questions!

p/s I know that this package docs are awful. Still can't find enough time to make them better. )))

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