blob: e08dd750e470e7b8ad3f3ac494d95eaa62995baf (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
import { getLogger } from 'loglevel';
import { makeObservable, observable, runInAction } from 'mobx';
import React, { createContext, useContext } from 'react';
import type EditorStore from './editor/EditorStore';
import ThemeStore from './theme/ThemeStore';
const log = getLogger('RootStore');
export default class RootStore {
editorStore: EditorStore | undefined;
readonly themeStore: ThemeStore;
constructor(initialValue: string) {
this.themeStore = new ThemeStore();
makeObservable(this, {
editorStore: observable,
});
import('./editor/EditorStore')
.then(({ default: EditorStore }) => {
runInAction(() => {
this.editorStore = new EditorStore(initialValue);
});
})
.catch((error) => {
log.error('Failed to load EditorStore', error);
});
}
}
const StoreContext = createContext<RootStore | undefined>(undefined);
export interface RootStoreProviderProps {
children: JSX.Element;
rootStore: RootStore;
}
export function RootStoreProvider({
children,
rootStore,
}: RootStoreProviderProps): JSX.Element {
return (
<StoreContext.Provider value={rootStore}>{children}</StoreContext.Provider>
);
}
export const useRootStore = (): RootStore => {
const rootStore = useContext(StoreContext);
if (!rootStore) {
throw new Error('useRootStore must be used within RootStoreProvider');
}
return rootStore;
};
|