memory key should be string
This commit is contained in:
parent
c41d7e0437
commit
e3ec395b35
@ -49,10 +49,10 @@ interface OfficerReady {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const OFFICER_KEY = buildStorageKey("officer", codecForOfficer());
|
const OFFICER_KEY = buildStorageKey("officer", codecForOfficer());
|
||||||
const ACCOUNT_KEY = buildStorageKey<Account>("account");
|
const ACCOUNT_KEY = "account";
|
||||||
|
|
||||||
export function useOfficer(): OfficerState {
|
export function useOfficer(): OfficerState {
|
||||||
const accountStorage = useMemoryStorage(ACCOUNT_KEY);
|
const accountStorage = useMemoryStorage<Account>(ACCOUNT_KEY);
|
||||||
const officerStorage = useLocalStorage(OFFICER_KEY);
|
const officerStorage = useLocalStorage(OFFICER_KEY);
|
||||||
|
|
||||||
const officer = officerStorage.value;
|
const officer = officerStorage.value;
|
||||||
|
@ -27,37 +27,37 @@ const storage: ObservableMap<string, any> = memoryMap<any>();
|
|||||||
|
|
||||||
//with initial value
|
//with initial value
|
||||||
export function useMemoryStorage<Type = string>(
|
export function useMemoryStorage<Type = string>(
|
||||||
key: StorageKey<Type>,
|
key: string,
|
||||||
defaultValue: Type,
|
defaultValue: Type,
|
||||||
): Required<StorageState<Type>>;
|
): Required<StorageState<Type>>;
|
||||||
//with initial value
|
//with initial value
|
||||||
export function useMemoryStorage<Type = string>(
|
export function useMemoryStorage<Type = string>(
|
||||||
key: StorageKey<Type>,
|
key: string,
|
||||||
): StorageState<Type>;
|
): StorageState<Type>;
|
||||||
// impl
|
// impl
|
||||||
export function useMemoryStorage<Type = string>(
|
export function useMemoryStorage<Type = string>(
|
||||||
key: StorageKey<Type>,
|
key: string,
|
||||||
defaultValue?: Type,
|
defaultValue?: Type,
|
||||||
): StorageState<Type> {
|
): StorageState<Type> {
|
||||||
const [storedValue, setStoredValue] = useState<Type | undefined>(
|
const [storedValue, setStoredValue] = useState<Type | undefined>(
|
||||||
(): Type | undefined => {
|
(): Type | undefined => {
|
||||||
const prev = storage.get(key.id);
|
const prev = storage.get(key);
|
||||||
return prev;
|
return prev === undefined ? defaultValue : prev;
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return storage.onUpdate(key.id, () => {
|
return storage.onUpdate(key, () => {
|
||||||
const newValue = storage.get(key.id);
|
const newValue = storage.get(key);
|
||||||
setStoredValue(newValue);
|
setStoredValue(newValue === undefined ? defaultValue : newValue);
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const setValue = (value?: Type): void => {
|
const setValue = (value?: Type): void => {
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
storage.delete(key.id);
|
storage.delete(key);
|
||||||
} else {
|
} else {
|
||||||
storage.set(key.id, value);
|
storage.set(key, value);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user