diff --git a/src/storage/createStore.ts b/src/storage/createStore.ts index 29d0076..a11c15e 100644 --- a/src/storage/createStore.ts +++ b/src/storage/createStore.ts @@ -1,6 +1,7 @@ +/* eslint-disable no-else-return */ /* eslint-disable no-await-in-loop */ import { Security } from 'src/storage/Security'; -import { useEffect, useState } from 'react'; +import { useCallback, useEffect, useState } from 'react'; import { Serializable } from '..'; /** A utility type that forces you to declare all the values specified in the type interface for a module. */ @@ -305,30 +306,29 @@ function createStore( arguments.length === 2 ? defaultValue : key === null ? defaults : defaults[key] ); - useEffect(() => { - if (key !== null) { - store.get(key).then(setValue as any); + const onChange = useCallback(({ key: k, newValue }: DataChange) => { + if (key === null) { + setValue(prev => ({ ...prev, [k]: newValue } as any)); + } else { + setValue(newValue as any); + } + }, []); - const onChanged = ({ newValue }: DataChange) => { - setValue(newValue as any); + useEffect(() => { + if (key === null) { + store.all().then(setValue as any); + store.keys().forEach(k => store.subscribe(k, onChange as any)); + return () => { + store.keys().forEach(k => store.unsubscribe(onChange as any)); }; - store.subscribe(key, onChanged); + } else { + store.get(key).then(setValue as any); + store.subscribe(key, onChange as any); return () => { - store.unsubscribe(onChanged); + store.unsubscribe(onChange as any); }; } - - store.all().then(setValue as any); - - const onChanged = (change: DataChange) => { - setValue(prev => ({ ...prev, [change.key]: change.newValue } as any)); - }; - // @ts-ignore - Object.keys(defaults).forEach(k => store.subscribe(k, onChanged)); - return () => { - store.unsubscribe(onChanged); - }; - }, [key]); + }, []); const set = async newValue => { if (key === null) {