Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@diamondlightsource/cs-web-lib",
"version": "0.8.0",
"version": "0.9.0",
"description": "Control system web library",
"main": "dist/cjs/index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/redux/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { store } from "./store";
export type { CsWebLibConfig } from "./store";
100 changes: 69 additions & 31 deletions src/redux/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,77 @@ import { SimulatorPlugin } from "../connection/sim";
import { PvwsPlugin } from "../connection/pvws";
import { ConnectionForwarder } from "../connection/forwarder";

const PVWS_SOCKET =
process.env.VITE_PVWS_SOCKET ?? import.meta.env.VITE_PVWS_SOCKET;
const PVWS_SSL =
(process.env.VITE_PVWS_SSL ?? import.meta.env.VITE_PVWS_SSL) === "true";
const THROTTLE_PERIOD = parseFloat(
process.env.VITE_THROTTLE_PERIOD ??
import.meta.env.VITE_THROTTLE_PERIOD ??
"100"
);

const simulator = new SimulatorPlugin();
const plugins: [string, Connection][] = [["sim://", simulator]];
if (PVWS_SOCKET !== undefined) {
const pvws = new PvwsPlugin(PVWS_SOCKET, PVWS_SSL);
plugins.unshift(["pva://", pvws]);
plugins.unshift(["ca://", pvws]);
plugins.unshift(["loc://", pvws]);
plugins.unshift(["sim://", pvws]);
plugins.unshift(["ssim://", pvws]);
plugins.unshift(["dev://", pvws]);
plugins.unshift(["eq://", pvws]);
}
const connection = new ConnectionForwarder(plugins);
export type CsWebLibConfig = {
PVWS_SOCKET: string | undefined;
PVWS_SSL: boolean | undefined;
THROTTLE_PERIOD: number | undefined;
};

// Store singleton
let storeInstance: ReturnType<typeof createStore> | null = null;
let connectionInstance: ConnectionForwarder | null = null;

const buildConnection = (config?: CsWebLibConfig) => {
const PVWS_SOCKET =
config?.PVWS_SOCKET ??
process.env.VITE_PVWS_SOCKET ??
import.meta.env.VITE_PVWS_SOCKET;
const PVWS_SSL =
(config?.PVWS_SSL ??
process.env.VITE_PVWS_SSL ??
import.meta.env.VITE_PVWS_SSL) === "true";

const simulator = new SimulatorPlugin();
const plugins: [string, Connection][] = [["sim://", simulator]];

if (PVWS_SOCKET !== undefined) {
const pvws = new PvwsPlugin(PVWS_SOCKET, PVWS_SSL);
plugins.unshift(["pva://", pvws]);
plugins.unshift(["ca://", pvws]);
plugins.unshift(["loc://", pvws]);
plugins.unshift(["sim://", pvws]);
plugins.unshift(["ssim://", pvws]);
plugins.unshift(["dev://", pvws]);
plugins.unshift(["eq://", pvws]);
}

return new ConnectionForwarder(plugins);
};

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

export const store = createStore(
csReducer,
/* preloadedState, */ composeEnhancers(
applyMiddleware(
connectionMiddleware(connection),
throttleMiddleware(new UpdateThrottle(THROTTLE_PERIOD))
export const store = (config?: CsWebLibConfig) => {
if (storeInstance) {
return storeInstance;
}

if (!connectionInstance) {
connectionInstance = buildConnection(config);
}

const THROTTLE_PERIOD: number = parseFloat(
config?.THROTTLE_PERIOD ??
process.env.VITE_THROTTLE_PERIOD ??
import.meta.env.VITE_THROTTLE_PERIOD ??
"100"
);

storeInstance = createStore(
csReducer,
/* preloadedState, */ composeEnhancers(
applyMiddleware(
connectionMiddleware(connectionInstance),
throttleMiddleware(new UpdateThrottle(THROTTLE_PERIOD))
)
)
)
);
);

return storeInstance;
};

// Reset store (for testing)
export const resetStore = () => {
storeInstance = null;
connectionInstance = null;
};
2 changes: 1 addition & 1 deletion src/ui/hooks/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function useSubscription(
}

export function writePv(pvName: string, value: DType): void {
store.dispatch({
store().dispatch({
type: WRITE_PV,
payload: { pvName: pvName, value: value }
});
Expand Down
6 changes: 3 additions & 3 deletions src/ui/widgets/Tabs/tabContainer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("<TabContainer>", (): void => {
};
const { findByText } = await act(() => {
return render(
<Provider store={store}>
<Provider store={store()}>
<TabContainerComponent tabs={{ one: child }} />
</Provider>
);
Expand All @@ -32,7 +32,7 @@ describe("<TabContainer>", (): void => {
log.setLevel("error");
const { findByText } = await act(() => {
return render(
<Provider store={store}>
<Provider store={store()}>
<TabContainerComponent tabs={{ one: child }} />
</Provider>
);
Expand All @@ -54,7 +54,7 @@ describe("<TabContainer>", (): void => {

const { findByText } = await act(() => {
return render(
<Provider store={store}>
<Provider store={store()}>
<TabContainerComponent tabs={{ one: child1, two: child2 }} />
</Provider>
);
Expand Down
Loading