Add persistentJSON
Add `persistentJSON` function. I'm sure 99% developers create the function for there projects, so it will be nice to have in the repo.
```ts
export function persistentJSON<T> (name: string, initial: T, opts?: PersistentSimpleOptions) {
return persistentAtom<T>(name, initial, {
encode: JSON.stringify,
decode: JSON.parse,
...opts,
})
}
```
With optional initial value, but I'm not sure it's a good idea.
```ts
export function persistentJSON<T> (name: string, initial: T, opts?: PersistentSimpleOptions): WritableAtom<T>
export function persistentJSON<T> (name: string, initial?: undefined, opts?: PersistentSimpleOptions): WritableAtom<T | undefined>
export function persistentJSON<T> (name: string, initial?: T, opts?: PersistentSimpleOptions) {
return persistentAtom<T>(name, initial as T, {
encode: JSON.stringify,
decode: JSON.parse,
...opts,
})
}
```
4 条评论