Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix Asynchronous Compactability #62

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ import { recoilPersist } from 'recoil-persist'
const { persistAtom } = recoilPersist({
key: 'recoil-persist', // this key is using to store data in local storage
storage: localStorage, // configurate which stroage will be used to store the data
stringify: JSON.stringify,
parser: JSON.parse,
})
```

Expand All @@ -133,6 +135,23 @@ type config.storage = Storage
Set `config.storage` with `sessionStorage` or other `Storage` implementation to
change storage target. Otherwise `localStorage` is used (default).


```js
type config.stringify = Function
```

Set `config.stringify` with any method implementation to
change the saved data. Otherwise `JSON.stringify` is used (default).


```js
type config.parser = Function
```

Set `config.parser` with any method implementation to
change/transfer return data. Otherwise `JSON.parse` is used (default).


## Migration from version 1.x.x to 2.x.x

The API changed from version 1.x.x.
Expand All @@ -154,7 +173,7 @@ const {
- ['count'], // no need for specifying atoms keys
{
key: 'recoil-persist', // configuration stay the same too
storage: localStorage
storage: localStorage,
}
)

Expand Down
27 changes: 18 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface PersistStorage {
export interface PersistConfiguration {
key?: string
storage?: PersistStorage
stringify?: (value: any) => any
parser?: (value: any) => any
}

/**
Expand All @@ -27,27 +29,34 @@ export const recoilPersist = (
}
}

const { key = 'recoil-persist', storage = localStorage } = config

const {
key = 'recoil-persist',
storage = localStorage,
stringify = JSON.stringify,
parser = JSON.parse,
} = config
const persistAtom: AtomEffect<any> = ({ onSet, node, trigger, setSelf }) => {
if (trigger === 'get') {
const state = getState()
if (typeof state.then === 'function') {
state.then((s) => {
if (s.hasOwnProperty(node.key)) {
state.then((s: any) => {
if (s && s.hasOwnProperty(node.key)) {
setSelf(s[node.key])
}
})
}

if (state.hasOwnProperty(node.key)) {
setSelf(state[node.key])
}
}

onSet(async (newValue, _, isReset) => {
onSet((newValue, _, isReset) => {
const state = getState()
if (typeof state.then === 'function') {
state.then((s: any) => updateState(newValue, s, node.key, isReset))
state.then((s: any) =>
updateState(newValue, s ?? {}, node.key, isReset),
)
} else {
updateState(newValue, state, node.key, isReset)
}
Expand Down Expand Up @@ -89,7 +98,7 @@ export const recoilPersist = (
return {}
}
try {
return JSON.parse(state)
return parser(state)
} catch (e) {
console.error(e)
return {}
Expand All @@ -99,9 +108,9 @@ export const recoilPersist = (
const setState = (state: any): void => {
try {
if (typeof storage.mergeItem === 'function') {
storage.mergeItem(key, JSON.stringify(state))
storage.mergeItem(key, stringify(state))
} else {
storage.setItem(key, JSON.stringify(state))
storage.setItem(key, stringify(state))
}
} catch (e) {
console.error(e)
Expand Down