32 lines
720 B
JavaScript

/* eslint-disable import/prefer-default-export */
import { computed } from 'mobx';
import { useMemo } from 'react';
import { useStore } from 'stores/hooks';
import { useDebouncedCallback } from 'use-debounce';
const DEBOUNCE_DELAY = 350;
export function useValue(valueName) {
const { $calculation } = useStore();
const storeValue = useMemo(
() => computed(() => $calculation.$values.getValue(valueName)),
[$calculation.$values, valueName]
);
const setStoreValue = useDebouncedCallback(
(value) => {
$calculation.$values.setValue(valueName, value);
},
DEBOUNCE_DELAY,
{
maxWait: 2000,
}
);
return {
value: storeValue.get(),
setValue: setStoreValue,
};
}