Skip to content

Commit 72f5813

Browse files
committed
Rewriting store to handle a runtime configuration
1 parent 8d969bc commit 72f5813

File tree

4 files changed

+74
-35
lines changed

4 files changed

+74
-35
lines changed

src/redux/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { store } from "./store";
2+
export type { CsWebLibConfig } from "./store";

src/redux/store.ts

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,77 @@ import { SimulatorPlugin } from "../connection/sim";
88
import { PvwsPlugin } from "../connection/pvws";
99
import { ConnectionForwarder } from "../connection/forwarder";
1010

11-
const PVWS_SOCKET =
12-
process.env.VITE_PVWS_SOCKET ?? import.meta.env.VITE_PVWS_SOCKET;
13-
const PVWS_SSL =
14-
(process.env.VITE_PVWS_SSL ?? import.meta.env.VITE_PVWS_SSL) === "true";
15-
const THROTTLE_PERIOD = parseFloat(
16-
process.env.VITE_THROTTLE_PERIOD ??
17-
import.meta.env.VITE_THROTTLE_PERIOD ??
18-
"100"
19-
);
20-
21-
const simulator = new SimulatorPlugin();
22-
const plugins: [string, Connection][] = [["sim://", simulator]];
23-
if (PVWS_SOCKET !== undefined) {
24-
const pvws = new PvwsPlugin(PVWS_SOCKET, PVWS_SSL);
25-
plugins.unshift(["pva://", pvws]);
26-
plugins.unshift(["ca://", pvws]);
27-
plugins.unshift(["loc://", pvws]);
28-
plugins.unshift(["sim://", pvws]);
29-
plugins.unshift(["ssim://", pvws]);
30-
plugins.unshift(["dev://", pvws]);
31-
plugins.unshift(["eq://", pvws]);
32-
}
33-
const connection = new ConnectionForwarder(plugins);
11+
export type CsWebLibConfig = {
12+
PVWS_SOCKET: string | undefined;
13+
PVWS_SSL: boolean | undefined;
14+
THROTTLE_PERIOD: number | undefined;
15+
};
16+
17+
// Store singleton
18+
let storeInstance: ReturnType<typeof createStore> | null = null;
19+
let connectionInstance: ConnectionForwarder | null = null;
20+
21+
const buildConnection = (config?: CsWebLibConfig) => {
22+
const PVWS_SOCKET =
23+
config?.PVWS_SOCKET ??
24+
process.env.VITE_PVWS_SOCKET ??
25+
import.meta.env.VITE_PVWS_SOCKET;
26+
const PVWS_SSL =
27+
(config?.PVWS_SSL ??
28+
process.env.VITE_PVWS_SSL ??
29+
import.meta.env.VITE_PVWS_SSL) === "true";
30+
31+
const simulator = new SimulatorPlugin();
32+
const plugins: [string, Connection][] = [["sim://", simulator]];
33+
34+
if (PVWS_SOCKET !== undefined) {
35+
const pvws = new PvwsPlugin(PVWS_SOCKET, PVWS_SSL);
36+
plugins.unshift(["pva://", pvws]);
37+
plugins.unshift(["ca://", pvws]);
38+
plugins.unshift(["loc://", pvws]);
39+
plugins.unshift(["sim://", pvws]);
40+
plugins.unshift(["ssim://", pvws]);
41+
plugins.unshift(["dev://", pvws]);
42+
plugins.unshift(["eq://", pvws]);
43+
}
44+
45+
return new ConnectionForwarder(plugins);
46+
};
3447

3548
const composeEnhancers =
3649
(window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
3750

38-
export const store = createStore(
39-
csReducer,
40-
/* preloadedState, */ composeEnhancers(
41-
applyMiddleware(
42-
connectionMiddleware(connection),
43-
throttleMiddleware(new UpdateThrottle(THROTTLE_PERIOD))
51+
export const store = (config?: CsWebLibConfig) => {
52+
if (storeInstance) {
53+
return storeInstance;
54+
}
55+
56+
if (!connectionInstance) {
57+
connectionInstance = buildConnection(config);
58+
}
59+
60+
const THROTTLE_PERIOD: number = parseFloat(
61+
config?.THROTTLE_PERIOD ??
62+
process.env.VITE_THROTTLE_PERIOD ??
63+
import.meta.env.VITE_THROTTLE_PERIOD ??
64+
"100"
65+
);
66+
67+
storeInstance = createStore(
68+
csReducer,
69+
/* preloadedState, */ composeEnhancers(
70+
applyMiddleware(
71+
connectionMiddleware(connectionInstance),
72+
throttleMiddleware(new UpdateThrottle(THROTTLE_PERIOD))
73+
)
4474
)
45-
)
46-
);
75+
);
76+
77+
return storeInstance;
78+
};
79+
80+
// Reset store (for testing)
81+
export const resetStore = () => {
82+
storeInstance = null;
83+
connectionInstance = null;
84+
};

src/ui/hooks/useSubscription.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function useSubscription(
4444
}
4545

4646
export function writePv(pvName: string, value: DType): void {
47-
store.dispatch({
47+
store().dispatch({
4848
type: WRITE_PV,
4949
payload: { pvName: pvName, value: value }
5050
});

src/ui/widgets/Tabs/tabContainer.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe("<TabContainer>", (): void => {
1717
};
1818
const { findByText } = await act(() => {
1919
return render(
20-
<Provider store={store}>
20+
<Provider store={store()}>
2121
<TabContainerComponent tabs={{ one: child }} />
2222
</Provider>
2323
);
@@ -32,7 +32,7 @@ describe("<TabContainer>", (): void => {
3232
log.setLevel("error");
3333
const { findByText } = await act(() => {
3434
return render(
35-
<Provider store={store}>
35+
<Provider store={store()}>
3636
<TabContainerComponent tabs={{ one: child }} />
3737
</Provider>
3838
);
@@ -54,7 +54,7 @@ describe("<TabContainer>", (): void => {
5454

5555
const { findByText } = await act(() => {
5656
return render(
57-
<Provider store={store}>
57+
<Provider store={store()}>
5858
<TabContainerComponent tabs={{ one: child1, two: child2 }} />
5959
</Provider>
6060
);

0 commit comments

Comments
 (0)