Comments (6)
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);
});
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.
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); });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.
Hi! I'm having the same problem. Is there any way to fix it?
from svelte-store.
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.
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.
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)
- Confused about asyncDerived store not updating on new store values HOT 3
- How to delay the asyncDerived store kicking off until previous writeable has completed? HOT 3
- Typescript support for initial `undefined` state HOT 11
- Overhaul typing to use strict mode
- Passing arguments to load() and reactively reloading on argument update HOT 1
- IndexedDB? HOT 3
- enableStoreTestingMode always throws error, regardless of where it is located HOT 2
- Svelte 4 support HOT 5
- asyncDerived stores doesn't update when dependencies are Set or Map HOT 11
- Error at demo code HOT 3
- AsyncDerived returns undefined HOT 1
- What's the name of the npm package? HOT 1
- Update npm to latest commit to add svelte 4 support HOT 3
- Thenable store
- `trackState` typing
- Svelte 5 Support HOT 3
- How can I know the number of subscribers on a Loadable store at any point in time?
- Publish the latest version HOT 4
- Fix typing in AsyncStoreOptions HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from svelte-store.