Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Compatibility with localForage #55

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
66 changes: 25 additions & 41 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { AtomEffect } from 'recoil'

type PersistItemValue = string | undefined | null

export interface PersistStorage {
setItem(key: string, value: string): void | Promise<void>
mergeItem?(key: string, value: string): Promise<void>
getItem(key: string): null | string | Promise<string>
setItem(key: string, value: string): any
mergeItem?(key: string, value: string): any
getItem(key: string): PersistItemValue | Promise<PersistItemValue>
}

export interface PersistState {}

export interface PersistConfiguration {
key?: string
storage?: PersistStorage
key: string
storage: PersistStorage
}

/**
Expand All @@ -19,38 +23,30 @@ export interface PersistConfiguration {
* @param config.storage Local storage to use, defaults to `localStorage`
*/
export const recoilPersist = (
config: PersistConfiguration = {},
config: Partial<PersistConfiguration> = {},
): { persistAtom: AtomEffect<any> } => {
if (typeof window === 'undefined') {
return {
persistAtom: () => {},
}
}

const { key = 'recoil-persist', storage = localStorage } = config
const {
key = 'recoil-persist' as PersistConfiguration['key'],
storage = localStorage as PersistConfiguration['storage'],
} = 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)) {
setSelf(s[node.key])
}
})
}
if (state.hasOwnProperty(node.key)) {
setSelf(state[node.key])
}
getState().then((s) => {
if (s.hasOwnProperty(node.key)) {
setSelf(s[node.key])
}
})
}

onSet(async (newValue, _, isReset) => {
const state = getState()
if (typeof state.then === 'function') {
state.then((s: any) => updateState(newValue, s, node.key, isReset))
} else {
updateState(newValue, state, node.key, isReset)
}
getState().then((s) => updateState(newValue, s, node.key, isReset))
})
}

Expand All @@ -69,22 +65,7 @@ export const recoilPersist = (
setState(state)
}

const getState = (): any => {
const toParse = storage.getItem(key)
if (toParse === null || toParse === undefined) {
return {}
}
if (typeof toParse === 'string') {
return parseState(toParse)
}
if (typeof toParse.then === 'function') {
return toParse.then(parseState)
}

return {}
}

const parseState = (toParse: string) => {
const parseState = (toParse: string | null | undefined): PersistState => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should change this to PersistItemValue

if (toParse === null || toParse === undefined) {
return {}
}
Expand All @@ -96,7 +77,10 @@ export const recoilPersist = (
}
}

const setState = (state: any): void => {
const getState = (): Promise<PersistState> =>
Promise.resolve(storage.getItem(key)).then(parseState)

const setState = (state: PersistState): void => {
try {
if (typeof storage.mergeItem === 'function') {
storage.mergeItem(key, JSON.stringify(state))
Expand Down