Merge pull request #34 from mattremmel/main
Update sync implementation to ensure stored value is loaded into svelte
This commit is contained in:
@@ -42,7 +42,7 @@ describe("Store utilities", () => {
|
|||||||
|
|
||||||
describe("synced", () => {
|
describe("synced", () => {
|
||||||
it("should sync with localStorage", async () => {
|
it("should sync with localStorage", async () => {
|
||||||
const store = synced({
|
const store = await synced({
|
||||||
key: "testKey",
|
key: "testKey",
|
||||||
storage: localStorageProvider,
|
storage: localStorageProvider,
|
||||||
defaultValue: "default",
|
defaultValue: "default",
|
||||||
@@ -63,7 +63,7 @@ describe("Store utilities", () => {
|
|||||||
|
|
||||||
it("should load existing value from localStorage", async () => {
|
it("should load existing value from localStorage", async () => {
|
||||||
localStorage.setItem("testKey", JSON.stringify("existing"))
|
localStorage.setItem("testKey", JSON.stringify("existing"))
|
||||||
const store = synced({
|
const store = await synced({
|
||||||
key: "testKey",
|
key: "testKey",
|
||||||
storage: localStorageProvider,
|
storage: localStorageProvider,
|
||||||
defaultValue: "default",
|
defaultValue: "default",
|
||||||
@@ -78,7 +78,7 @@ describe("Store utilities", () => {
|
|||||||
|
|
||||||
describe("getter", () => {
|
describe("getter", () => {
|
||||||
it("should return current store value", async () => {
|
it("should return current store value", async () => {
|
||||||
const store = synced({
|
const store = await synced({
|
||||||
key: "test",
|
key: "test",
|
||||||
storage: localStorageProvider,
|
storage: localStorageProvider,
|
||||||
defaultValue: "initial",
|
defaultValue: "initial",
|
||||||
@@ -98,7 +98,7 @@ describe("Store utilities", () => {
|
|||||||
describe("withGetter", () => {
|
describe("withGetter", () => {
|
||||||
it("should add getter to writable store", async () => {
|
it("should add getter to writable store", async () => {
|
||||||
const store = withGetter(
|
const store = withGetter(
|
||||||
synced({
|
await synced({
|
||||||
key: "test",
|
key: "test",
|
||||||
storage: localStorageProvider,
|
storage: localStorageProvider,
|
||||||
defaultValue: "initial",
|
defaultValue: "initial",
|
||||||
@@ -117,7 +117,7 @@ describe("Store utilities", () => {
|
|||||||
describe("throttled", () => {
|
describe("throttled", () => {
|
||||||
it("should throttle updates", async () => {
|
it("should throttle updates", async () => {
|
||||||
const mockFn = vi.fn()
|
const mockFn = vi.fn()
|
||||||
const store = synced({
|
const store = await synced({
|
||||||
key: "test",
|
key: "test",
|
||||||
storage: localStorageProvider,
|
storage: localStorageProvider,
|
||||||
defaultValue: 0,
|
defaultValue: 0,
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ export interface SyncConfig<T> {
|
|||||||
storage: StorageProvider
|
storage: StorageProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
export const sync = <T>({key, store, storage}: SyncConfig<T>) => {
|
export type Synced<T> = Writable<T> & {ready: Promise<void>}
|
||||||
storage.get(key).then((value: T | undefined) => {
|
|
||||||
if (value !== undefined) {
|
export const sync = async <T>({key, store, storage}: SyncConfig<T>) => {
|
||||||
store.set(value)
|
const storedValue = await storage.get(key)
|
||||||
}
|
|
||||||
})
|
if (storedValue !== undefined) {
|
||||||
|
store.set(storedValue)
|
||||||
|
}
|
||||||
|
|
||||||
store.subscribe(async (value: T) => {
|
store.subscribe(async (value: T) => {
|
||||||
await storage.set(key, value)
|
await storage.set(key, value)
|
||||||
@@ -36,9 +38,10 @@ export interface SyncedConfig<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const synced = <T>({key, storage, defaultValue}: SyncedConfig<T>) => {
|
export const synced = <T>({key, storage, defaultValue}: SyncedConfig<T>) => {
|
||||||
const store = writable<T>(defaultValue)
|
const store = writable<T>(defaultValue) as Synced<T>
|
||||||
|
|
||||||
sync({key, store, storage})
|
const syncPromise = sync({key, store, storage})
|
||||||
|
store.ready = syncPromise
|
||||||
|
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user