Skip to content

Commit ba39e77

Browse files
committed
mobx version
1 parent bebfe7e commit ba39e77

File tree

2 files changed

+31
-53
lines changed

2 files changed

+31
-53
lines changed

lib/Views/root.jsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { observer } from "mobx-react";
12
import PropTypes from "prop-types";
2-
import React, { Suspense, useSyncExternalStore } from "react";
3+
import React, { Suspense } from "react";
4+
import { createRoot } from "react-dom/client";
5+
import Variables from "../Styles/variables.scss";
36
import "./global.scss";
47
import { Loader } from "./Loader";
58
import { terriaStore } from "./terriaStore";
6-
import Variables from "../Styles/variables.scss";
7-
import { createRoot } from "react-dom/client";
89

910
// Lazy load the entire TerriaUserInterface component
1011
const LazyTerriaUserInterface = React.lazy(() =>
@@ -13,11 +14,8 @@ const LazyTerriaUserInterface = React.lazy(() =>
1314
}))
1415
);
1516

16-
const Root = ({ themeOverrides }) => {
17-
const { terria, viewState, status } = useSyncExternalStore(
18-
terriaStore.subscribe,
19-
terriaStore.getSnapshot
20-
);
17+
const Root = observer(({ themeOverrides }) => {
18+
const { terria, viewState, status } = terriaStore;
2119

2220
if (status === "loading") {
2321
return <Loader />;
@@ -32,7 +30,7 @@ const Root = ({ themeOverrides }) => {
3230
/>
3331
</Suspense>
3432
);
35-
};
33+
});
3634

3735
Root.propTypes = {
3836
themeOverrides: PropTypes.object

lib/Views/terriaStore.ts

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,39 @@
1+
import { makeObservable, observable, action } from "mobx";
12
import type Terria from "terriajs/lib/Models/Terria";
23
import type ViewState from "terriajs/lib/ReactViewModels/ViewState";
34

4-
type State =
5-
| {
6-
terria: Terria;
7-
viewState: ViewState;
8-
status: "ready";
9-
}
10-
| {
11-
terria: undefined;
12-
viewState: undefined;
13-
status: "loading";
14-
};
15-
16-
let state: State = {
17-
terria: undefined,
18-
viewState: undefined,
19-
status: "loading"
20-
};
21-
type Listener = () => void;
22-
let listeners: Listener[] = [];
23-
24-
const emitChange = () => {
25-
for (const listener of listeners) {
26-
listener();
5+
class TerriaStore {
6+
terria: Terria | undefined = undefined;
7+
viewState: ViewState | undefined = undefined;
8+
status: "loading" | "ready" = "loading";
9+
10+
constructor() {
11+
makeObservable(this, {
12+
terria: observable,
13+
viewState: observable,
14+
status: observable,
15+
setReady: action
16+
});
17+
18+
this.init();
2719
}
28-
};
2920

30-
export const terriaStore = {
3121
async init() {
22+
//@ts-expect-error: need to convert to TS
3223
await import("terriajs/lib/Core/prerequisites");
3324

3425
const { terria, viewState } = await import("../../index.js").then(
3526
(module) => module.default
3627
);
3728

38-
state = {
39-
terria,
40-
viewState,
41-
status: "ready"
42-
};
43-
44-
emitChange();
45-
},
46-
subscribe(listener: Listener) {
47-
listeners = [...listeners, listener];
48-
49-
return () => {
50-
listeners = listeners.filter((l) => l !== listener);
51-
};
52-
},
29+
this.setReady(terria, viewState);
30+
}
5331

54-
getSnapshot() {
55-
return state;
32+
setReady(terria: Terria, viewState: ViewState) {
33+
this.terria = terria;
34+
this.viewState = viewState;
35+
this.status = "ready";
5636
}
57-
};
37+
}
5838

59-
terriaStore.init();
39+
export const terriaStore = new TerriaStore();

0 commit comments

Comments
 (0)