-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
125 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,89 @@ | ||
import localforage from 'localforage'; | ||
import React from 'react'; | ||
import useAsyncEffect from 'use-async-effect'; | ||
|
||
import storage from 'lib/storage'; | ||
|
||
/** | ||
* @private | ||
* | ||
* Value we compare to to determine if a storage value is pending sync. | ||
*/ | ||
const PENDING = Symbol('PENDING'); | ||
type LocalForage = ReturnType<typeof localforage['createInstance']>; | ||
type UseStorageItem<T = any> = [T, React.Dispatch<React.SetStateAction<T>>]; | ||
|
||
|
||
/** | ||
* Returns true if the provided value is pending. | ||
*/ | ||
export function isPending(value: any) { | ||
return value === PENDING; | ||
} | ||
|
||
const instances = new Map<string, LocalForage>(); | ||
|
||
type HookReturnValue<T = any> = [ | ||
T, | ||
React.Dispatch<React.SetStateAction<T>> | ||
]; | ||
|
||
/** | ||
* Provided a key, returns a tuple value and setter function that will sync the | ||
* provided value to Local Storage. | ||
* | ||
* TODO: Make own package. | ||
*/ | ||
function useStorageItem<T = any>(key: string): HookReturnValue<T | typeof PENDING | undefined>; | ||
function useStorageItem<T = any>(key: string, initialValue: T): HookReturnValue<T | typeof PENDING>; | ||
function useStorageItem<T = any>(key: string, initialValue?: T) { | ||
const [localValue, setLocalValue] = React.useState<T | typeof PENDING | undefined>(PENDING); | ||
function useStorageItem<T = any>(namespace: string, key: string): UseStorageItem<T | undefined>; | ||
function useStorageItem<T = any>(namespace: string, key: string, initialValue: T): UseStorageItem<T>; | ||
function useStorageItem<T = any>(namespace: string, key: string, initialValue?: T) { | ||
const [localValue, setLocalValue] = React.useState<T | undefined>(); | ||
const [storage, setStorage] = React.useState<LocalForage>(); | ||
|
||
/** | ||
* Updates the tracked value locally and in storage. | ||
*/ | ||
const setValue: React.Dispatch<React.SetStateAction<T>> = React.useCallback(value => { | ||
if (!storage) return; | ||
|
||
const setValue: React.Dispatch<React.SetStateAction<T>> = value => { | ||
void storage.setItem(key, value); | ||
setLocalValue(value as T); | ||
}; | ||
}, [storage]); | ||
|
||
useAsyncEffect(async () => { | ||
const valueFromStorage = await storage.getItem<T>(key); | ||
|
||
if (valueFromStorage !== null) { | ||
setLocalValue(valueFromStorage); | ||
} else if (initialValue !== undefined) { | ||
setValue(initialValue); | ||
/** | ||
* Initialize storage instance. | ||
*/ | ||
React.useEffect(() => { | ||
if (!instances.has(namespace)) { | ||
instances.set(namespace, localforage.createInstance({ | ||
driver: localforage.LOCALSTORAGE, | ||
name: namespace, | ||
version: 1 | ||
})); | ||
} | ||
|
||
}, [setLocalValue]); | ||
setStorage(instances.get(namespace)); | ||
}, [setStorage]); | ||
|
||
|
||
/** | ||
* Sync value from storage to local state. | ||
*/ | ||
React.useEffect(() => { | ||
if (!storage) return; | ||
|
||
void storage?.getItem<T>(key).then(valueFromStorage => { | ||
// If we got a value back from storage, set the local value accordingly. | ||
if (valueFromStorage !== null) { | ||
setLocalValue(valueFromStorage); | ||
return; | ||
} | ||
|
||
// If storage was empty and an initial value was provided, set it locally | ||
// and in storage. | ||
if (initialValue !== undefined) { | ||
setValue(initialValue); | ||
return; | ||
} | ||
}); | ||
}, [storage, setValue]); | ||
|
||
|
||
return [localValue, setValue]; | ||
} | ||
|
||
|
||
export default useStorageItem; | ||
export function withNamespace(namespace: string) { | ||
function boundUseStorageItem<T = any>(key: string): UseStorageItem<T | undefined>; | ||
function boundUseStorageItem<T = any>(key: string, initialValue: T): UseStorageItem<T>; | ||
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions | ||
function boundUseStorageItem<T = any>(key: string, initialValue?: T) { | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
return useStorageItem(namespace, key, initialValue); | ||
} | ||
|
||
return boundUseStorageItem; | ||
} | ||
|
||
|
||
export default withNamespace; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters