2022-04-29 12:50:28 +03:00

38 lines
911 B
TypeScript

import { pick } from 'lodash';
import { makeAutoObservable } from 'mobx';
import { RootStore } from '../root';
import { Values } from './types';
export type CalculationValues = Partial<Record<Values, any>>;
export class ValuesStore {
root: RootStore;
#values: CalculationValues = {};
constructor(rootStore: RootStore) {
makeAutoObservable(this);
this.root = rootStore;
}
hydrate = (initialValues: CalculationValues) => {
this.#values = initialValues;
};
getValue(valueName: Values) {
return this.#values[valueName];
}
getValues(valuesNames: Values[]) {
return pick(this.#values, valuesNames);
}
setValue(valueName: Values, value: any) {
this.#values[valueName] = value;
}
setValues(values: CalculationValues, options: { replace?: boolean }) {
if (options.replace) this.#values = values;
this.#values = Object.assign(this.#values, values);
}
}