Skip to content

Commit

Permalink
fix init
Browse files Browse the repository at this point in the history
  • Loading branch information
Not-Jayden committed Jul 15, 2024
1 parent 312f252 commit 2d54d08
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ type StorageType = "local" | "session";
type PersistedStateOptions<T> = {
/** The storage type to use. Defaults to `local`. */
storage?: StorageType | StorageAdapter<T>;
/** Whether to sync with the state changes from other tabs. Defaults to `true`. */
syncTabs?: boolean;
/**
* Whether to synchronize the state with changes detected via the `subscribe` callback. This allows the state to be updated in response to changes originating from various sources, including other browser tabs or sessions when using web storage like localStorage or sessionStorage. Defaults to `true`.
*/
syncChanges?: boolean;
};

/**
Expand All @@ -135,7 +137,7 @@ export class Persisted<T> {
#storageAdapter: StorageAdapter<T> | null;

constructor(key: string, initialValue: T, options: PersistedStateOptions<T> = {}) {
const { storage = "local", syncTabs = true } = options;
const { storage = "local", syncChanges = true } = options;

this.#key = key;
this.#initialValue = initialValue;
Expand All @@ -154,7 +156,7 @@ export class Persisted<T> {
});
});

if (syncTabs) {
if (syncChanges) {
$effect(() => {
return untrack(() => {
if (!this.#storageAdapter?.subscribe) {
Expand Down Expand Up @@ -185,11 +187,8 @@ export class Persisted<T> {
}

const valueFromStorage = await this.#storageAdapter.getItem(this.#key);
if (!valueFromStorage.found) {
return;
}

this.#current = valueFromStorage.value;
this.#current = valueFromStorage.found ? valueFromStorage.value : this.#initialValue;
this.#isInitialized = true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect } from "vitest";

import { Persisted } from "./index.js";
import { Persisted, WebStorageAdapter } from "./index.js";
import { testWithEffect } from "$lib/test/util.svelte.js";

const key = "test-key";
Expand Down Expand Up @@ -65,10 +65,21 @@ describe("PersistedState", () => {
const date = new Date(isoDate);

const serializer = {
serialize: (value: Date) => value.toISOString(),
deserialize: (value: string) => new Date(value),
serialize: (value: Date) => {
return value.toISOString();
},
deserialize: (value: string) => {
return new Date(value);
},
};
const persistedState = new Persisted(key, date, { serializer });
const persistedState = new Persisted(key, date, {
storage: new WebStorageAdapter({
storage: localStorage,
serializer,
}),
});
// persistedState.current = date;
// await new Promise((resolve) => setTimeout(resolve, 50));
expect(persistedState.current).toBe(date);
await new Promise((resolve) => setTimeout(resolve, 0));

Expand All @@ -91,7 +102,7 @@ describe("PersistedState", () => {
testWithEffect(
"does not update persisted value when local storage changes independently if syncTabs is false",
async () => {
const persistedState = new Persisted(key, initialValue, { syncTabs: false });
const persistedState = new Persisted(key, initialValue, { syncChanges: false });
localStorage.setItem(key, JSON.stringify("new-value"));
await new Promise((resolve) => setTimeout(resolve, 0));
expect(persistedState.current).toBe(initialValue);
Expand Down

0 comments on commit 2d54d08

Please sign in to comment.