Skip to content

Commit 01c2e29

Browse files
authored
Merge pull request #265 from intercepted16/master
feat: Add reset method to persisted store
2 parents 1a83f84 + 8187dd1 commit 01c2e29

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { writable as internal, type Writable } from 'svelte/store'
22

33
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+
}
59

610
/* eslint-disable @typescript-eslint/no-explicit-any */
711
interface Stores {
@@ -37,11 +41,11 @@ function getStorage(type: StorageType) {
3741
}
3842

3943
/** @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> {
4145
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'")
4246
return persisted<StoreType, SerializerType>(key, initialValue, options)
4347
}
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> {
4549
if (options?.onError) console.warn("onError has been deprecated. Please use onWriteError instead")
4650

4751
const serializer = options?.serializer ?? JSON
@@ -125,9 +129,11 @@ export function persisted<StoreType, SerializerType = StoreType>(key: string, in
125129
return value
126130
})
127131
},
132+
reset() {
133+
this.set(initialValue)
134+
},
128135
subscribe
129136
}
130137
}
131-
132138
return stores[storageType][key]
133139
}

test/localStorageStore.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ describe('persisted()', () => {
8686
})
8787
})
8888

89+
describe('reset', () => {
90+
it('resets to initial value', () => {
91+
const store = persisted('myKey14', 123);
92+
store.set(456);
93+
store.reset();
94+
const value = get(store);
95+
96+
expect(value).toEqual(123);
97+
expect(localStorage.myKey14).toEqual('123');
98+
});
99+
});
100+
89101
describe('subscribe()', () => {
90102
it('publishes updates', () => {
91103
const store = persisted('myKey7', 123)

0 commit comments

Comments
 (0)