|
1 | 1 | import { writable as internal, type Writable } from 'svelte/store' |
2 | 2 |
|
3 | 3 | declare type Updater<T> = (value: T) => T; |
4 | | -declare type StoreDict<T> = { [key: string]: Writable<T> } |
| 4 | +declare type StoreDict<T> = { [key: string]: Persisted<T> } |
| 5 | + |
| 6 | +interface Persisted<T> extends Writable<T> { |
| 7 | + reset: () => void |
| 8 | +} |
5 | 9 |
|
6 | 10 | /* eslint-disable @typescript-eslint/no-explicit-any */ |
7 | 11 | interface Stores { |
@@ -37,11 +41,11 @@ function getStorage(type: StorageType) { |
37 | 41 | } |
38 | 42 |
|
39 | 43 | /** @deprecated `writable()` has been renamed to `persisted()` */ |
40 | | -export function writable<StoreType, SerializerType = StoreType>(key: string, initialValue: StoreType, options?: Options<StoreType, SerializerType>): Writable<StoreType> { |
| 44 | +export function writable<StoreType, SerializerType = StoreType>(key: string, initialValue: StoreType, options?: Options<StoreType, SerializerType>): Persisted<StoreType> { |
41 | 45 | console.warn("writable() has been deprecated. Please use persisted() instead.\n\nchange:\n\nimport { writable } from 'svelte-persisted-store'\n\nto:\n\nimport { persisted } from 'svelte-persisted-store'") |
42 | 46 | return persisted<StoreType, SerializerType>(key, initialValue, options) |
43 | 47 | } |
44 | | -export function persisted<StoreType, SerializerType = StoreType>(key: string, initialValue: StoreType, options?: Options<StoreType, SerializerType>): Writable<StoreType> { |
| 48 | +export function persisted<StoreType, SerializerType = StoreType>(key: string, initialValue: StoreType, options?: Options<StoreType, SerializerType>): Persisted<StoreType> { |
45 | 49 | if (options?.onError) console.warn("onError has been deprecated. Please use onWriteError instead") |
46 | 50 |
|
47 | 51 | const serializer = options?.serializer ?? JSON |
@@ -125,9 +129,11 @@ export function persisted<StoreType, SerializerType = StoreType>(key: string, in |
125 | 129 | return value |
126 | 130 | }) |
127 | 131 | }, |
| 132 | + reset() { |
| 133 | + this.set(initialValue) |
| 134 | + }, |
128 | 135 | subscribe |
129 | 136 | } |
130 | 137 | } |
131 | | - |
132 | 138 | return stores[storageType][key] |
133 | 139 | } |
0 commit comments