@@ -8,39 +8,77 @@ import { SimulatorPlugin } from "../connection/sim";
88import { PvwsPlugin } from "../connection/pvws" ;
99import { 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
3548const 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+ } ;
0 commit comments