Skip to content

Commit

Permalink
updated use hook to now allow pulling in the entire object
Browse files Browse the repository at this point in the history
  • Loading branch information
sghsri committed Apr 25, 2024
1 parent b236110 commit eec1802
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/storage/createStore.ts
Original file line number Diff line number Diff line change
@@ -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. */
Expand Down Expand Up @@ -305,30 +306,29 @@ function createStore<T>(
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<T>) => {
if (key === null) {
setValue(prev => ({ ...prev, [k]: newValue } as any));
} else {
setValue(newValue as any);
}
}, []);

const onChanged = ({ newValue }: DataChange<T[typeof key]>) => {
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<T>) => {
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) {
Expand Down

0 comments on commit eec1802

Please sign in to comment.