2022-05-08 13:30:07 +03:00

26 lines
694 B
JavaScript

import { createContext } from 'react';
import { RootStore } from './root';
/** @type{RootStore} */
let store;
export const StoreContext = createContext(store);
export function initializeStore(initialData) {
const _store = store ?? new RootStore();
if (initialData) {
const { user = null, calculation = {} } = initialData;
const { values = {}, statuses = {}, options = {} } = calculation;
_store.$user.hydrate(user);
_store.$calculation.$values.hydrate(values);
_store.$calculation.$status.hydrate(statuses);
_store.$calculation.$options.hydrate(options);
}
if (typeof window === 'undefined') return _store;
if (!store) store = _store;
return _store;
}