-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
119 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 59 additions & 7 deletions
66
src/lib/state/persistance/adapters/createLocalStorageAdapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,65 @@ | ||
export const createLocalStorageAdapter = () => ({ | ||
getItem: async (key: string) => { | ||
const serializedValue = localStorage.getItem(key) | ||
import { type Adapter } from "../types" | ||
|
||
if (!serializedValue) return undefined | ||
const normalizeStore = (store: unknown) => { | ||
if (!store || typeof store !== "object") { | ||
return undefined | ||
} | ||
|
||
return store | ||
} | ||
|
||
/** | ||
* Create an adapter which use one unique store entry to store all | ||
* state. When using many signals it can help with maintenance to keep things | ||
* tidy. | ||
*/ | ||
const createSharedStoreAdapter = ({ | ||
adapter, | ||
key | ||
}: { | ||
adapter: Adapter | ||
key: string | ||
}): Adapter => ({ | ||
getItem: async (itemKey: string) => { | ||
const unsafeStore = await adapter.getItem(key) | ||
const store = normalizeStore(unsafeStore) ?? {} | ||
|
||
return JSON.parse(serializedValue) | ||
if (itemKey in store) { | ||
return store[itemKey as keyof typeof store] | ||
} | ||
|
||
return undefined | ||
}, | ||
|
||
setItem: async (key: string, value: unknown) => { | ||
localStorage.setItem(key, JSON.stringify(value)) | ||
setItem: async (itemKey: string, value: unknown) => { | ||
const unsafeStore = await adapter.getItem(key) | ||
const store = normalizeStore(unsafeStore) ?? {} | ||
|
||
await adapter.setItem(key, { ...store, [itemKey]: value }) | ||
} | ||
}) | ||
|
||
export const createLocalStorageAdapter = ({ | ||
key | ||
}: { key?: string } = {}): Adapter => { | ||
if (key) { | ||
return createSharedStoreAdapter({ | ||
adapter: createLocalStorageAdapter(), | ||
key | ||
}) | ||
} | ||
|
||
return { | ||
getItem: async (key: string) => { | ||
const serializedValue = localStorage.getItem(key) | ||
|
||
if (!serializedValue) return undefined | ||
|
||
return JSON.parse(serializedValue) | ||
}, | ||
|
||
setItem: async (key: string, value: unknown) => { | ||
localStorage.setItem(key, JSON.stringify(value)) | ||
} | ||
} | ||
} |
40 changes: 0 additions & 40 deletions
40
src/lib/state/persistance/adapters/createSharedStoreAdapter.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters