Git Product home page Git Product logo

Comments (6)

rafadess avatar rafadess commented on August 27, 2024

I have the same issue with persistent, it starts with undefined even though it has initial value.

Maybe, one way to fix it is passing the initial value to the thisStore writable. This way subscribe function will return the correct initial value.

let initialValue

if (isLoadable(initial)) {
 initialValue = await initial.load();
} else {
 initialValue = initial
}

const thisStore = writable<T>(initialValue, (set) => {
  initialSync = synchronize(set);
});

https://github.com/square/svelte-store/blob/44341f5166826982d947c9dd8bc63193911c8a74/src/persisted/index.ts#L140C3-L140C3

The if conditional could go into a dedicate function, since it is the same code inside of syncronize. Any way, this is just a possible solution. 😉

from svelte-store.

based64-eth avatar based64-eth commented on August 27, 2024

I have the same issue with persistent, it starts with undefined even though it has initial value.

Maybe, one way to fix it is passing the initial value to the thisStore writable. This way subscribe function will return the correct initial value.

let initialValue

if (isLoadable(initial)) {
 initialValue = await initial.load();
} else {
 initialValue = initial
}

const thisStore = writable<T>(initialValue, (set) => {
  initialSync = synchronize(set);
});

https://github.com/square/svelte-store/blob/44341f5166826982d947c9dd8bc63193911c8a74/src/persisted/index.ts#L140C3-L140C3

The if conditional could go into a dedicate function, since it is the same code inside of syncronize. Any way, this is just a possible solution. 😉

This is not an acceptable solution in the case that the initial value NEEDS to be equal to the value that the user has stored in localStorage.

from svelte-store.

thanhnguyen2187 avatar thanhnguyen2187 commented on August 27, 2024

Hi! I'm having the same problem. Is there any way to fix it?

from svelte-store.

nikicat avatar nikicat commented on August 27, 2024

I have the same issue with persistent, it starts with undefined even though it has initial value.
Maybe, one way to fix it is passing the initial value to the thisStore writable. This way subscribe function will return the correct initial value.

let initialValue

if (isLoadable(initial)) {
 initialValue = await initial.load();
} else {
 initialValue = initial
}

const thisStore = writable<T>(initialValue, (set) => {
  initialSync = synchronize(set);
});

https://github.com/square/svelte-store/blob/44341f5166826982d947c9dd8bc63193911c8a74/src/persisted/index.ts#L140C3-L140C3
The if conditional could go into a dedicate function, since it is the same code inside of syncronize. Any way, this is just a possible solution. 😉

This is not an acceptable solution in the case that the initial value NEEDS to be equal to the value that the user has stored in localStorage.

But it's better than the current state.

from svelte-store.

rafadess avatar rafadess commented on August 27, 2024

I have the same issue with persistent, it starts with undefined even though it has initial value.
Maybe, one way to fix it is passing the initial value to the thisStore writable. This way subscribe function will return the correct initial value.

let initialValue

if (isLoadable(initial)) {
 initialValue = await initial.load();
} else {
 initialValue = initial
}

const thisStore = writable<T>(initialValue, (set) => {
  initialSync = synchronize(set);
});

https://github.com/square/svelte-store/blob/44341f5166826982d947c9dd8bc63193911c8a74/src/persisted/index.ts#L140C3-L140C3
The if conditional could go into a dedicate function, since it is the same code inside of syncronize. Any way, this is just a possible solution. 😉

This is not an acceptable solution in the case that the initial value NEEDS to be equal to the value that the user has stored in localStorage.

Hunn, how about this then:

let initialValue
const storageKey = await getKey();
const stored = getStorageItem(storageKey);

if (stored) {
  initialValue = stored
} else {
  if (isLoadable(initial)) {
   initialValue = await initial.load();
  } else {
   initialValue = initial
  }
}


const thisStore = writable<T>(initialValue, (set) => {
  initialSync = synchronize(set);
});

I can make a PR with this, if the maintainers agree with.

from svelte-store.

nikicat avatar nikicat commented on August 27, 2024

I've solved my case with such a wrapper

function fixPersisted<T>(initial: T, store: Writable<T>): Writable<T> {
  return {
    subscribe: (run: (value: any) => void, invalidate?) => {
      return store.subscribe((value: any) => {
	if (value !== undefined) {
	  run(value)
	} else {
	  run(initial)
	}
      }, invalidate)
    },
    set: store.set,
    update: store.update,
  }
}

It doesn't wait to load the persisted value and always returns the initial value on start.

from svelte-store.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.