@@ -35,6 +35,9 @@ import { setLocalStorage } from "../util/localStorage";
3535import { migrateLocalStorage } from "../util/migrateLocalStorage" ;
3636import { useWebviewListener } from "./useWebviewListener" ;
3737
38+ const INITIAL_CONFIG_POLLING_INTERVAL = 2_000 ;
39+ const INITIAL_CONFIG_POLLING_MAX_ATTEMPTS = 100 ;
40+
3841function ParallelListeners ( ) {
3942 const dispatch = useAppDispatch ( ) ;
4043 const ideMessenger = useContext ( IdeMessengerContext ) ;
@@ -43,25 +46,71 @@ function ParallelListeners() {
4346 const selectedProfileId = useAppSelector (
4447 ( store ) => store . profiles . selectedProfileId ,
4548 ) ;
46- const hasDoneInitialConfigLoad = useRef ( false ) ;
49+
50+ const hasReceivedOrRetrievedConfig = useRef ( false ) ;
4751
4852 // Load symbols for chat on any session change
4953 const sessionId = useAppSelector ( ( state ) => state . session . id ) ;
5054 const lastSessionId = useAppSelector ( ( store ) => store . session . lastSessionId ) ;
5155 const [ initialSessionId ] = useState ( sessionId || lastSessionId ) ;
5256
57+ // Once we know core is up
58+ const onInitialConfigLoad = useCallback ( async ( ) => {
59+ debugger ;
60+ dispatch ( setConfigLoading ( false ) ) ;
61+ void dispatch ( refreshSessionMetadata ( { } ) ) ;
62+
63+ const jetbrains = isJetBrains ( ) ;
64+ if ( jetbrains ) {
65+ // Save theme colors to local storage for immediate loading in JetBrains
66+ void ideMessenger
67+ . request ( "jetbrains/getColors" , undefined )
68+ . then ( ( result ) => {
69+ if ( result . status === "success" ) {
70+ setDocumentStylesFromTheme ( result . content ) ;
71+ }
72+ } ) ;
73+
74+ // Tell JetBrains the webview is ready
75+ void ideMessenger
76+ . request ( "jetbrains/onLoad" , undefined )
77+ . then ( ( result ) => {
78+ if ( result . status === "success" ) {
79+ const msg = result . content ;
80+ ( window as any ) . windowId = msg . windowId ;
81+ ( window as any ) . serverUrl = msg . serverUrl ;
82+ ( window as any ) . workspacePaths = msg . workspacePaths ;
83+ ( window as any ) . vscMachineId = msg . vscMachineId ;
84+ ( window as any ) . vscMediaUrl = msg . vscMediaUrl ;
85+ }
86+ } ) ;
87+ }
88+
89+ ideMessenger . post ( "docs/initStatuses" , undefined ) ;
90+ void dispatch ( updateFileSymbolsFromHistory ( ) ) ;
91+
92+ if ( initialSessionId ) {
93+ await dispatch (
94+ loadSession ( {
95+ sessionId : initialSessionId ,
96+ saveCurrentSession : false ,
97+ } ) ,
98+ ) ;
99+ }
100+ } , [ initialSessionId , ideMessenger ] ) ;
101+
53102 const handleConfigUpdate = useCallback (
54- async ( isInitial : boolean , result : FromCoreProtocol [ "configUpdate" ] [ 0 ] ) => {
103+ async ( result : FromCoreProtocol [ "configUpdate" ] [ 0 ] ) => {
55104 const {
56105 result : configResult ,
57106 profileId,
58107 organizations,
59108 selectedOrgId,
60109 } = result ;
61- if ( isInitial && hasDoneInitialConfigLoad . current ) {
62- return ;
110+ if ( hasReceivedOrRetrievedConfig . current === false ) {
111+ await onInitialConfigLoad ( ) ;
63112 }
64- hasDoneInitialConfigLoad . current = true ;
113+ hasReceivedOrRetrievedConfig . current = true ;
65114 dispatch ( setOrganizations ( organizations ) ) ;
66115 dispatch ( setSelectedOrgId ( selectedOrgId ) ) ;
67116 dispatch ( setSelectedProfile ( profileId ) ) ;
@@ -92,56 +141,65 @@ function ParallelListeners() {
92141 setHasReasoningEnabled ( supportsReasoning && ! isReasoningDisabled ) ,
93142 ) ;
94143 } ,
95- [ dispatch , hasDoneInitialConfigLoad ] ,
144+ [ dispatch , hasReceivedOrRetrievedConfig , onInitialConfigLoad ] ,
96145 ) ;
97146
98- // Load config from the IDE
147+ // Startup activity
99148 useEffect ( ( ) => {
100- async function initialLoadConfig ( ) {
101- dispatch ( setIsSessionMetadataLoading ( true ) ) ;
102- dispatch ( setConfigLoading ( true ) ) ;
149+ void dispatch ( cancelStream ( ) ) ;
150+ dispatch ( setIsSessionMetadataLoading ( true ) ) ;
151+ dispatch ( setConfigLoading ( true ) ) ;
152+
153+ // Local storage migration/jetbrains styles loading
154+ const jetbrains = isJetBrains ( ) ;
155+ setDocumentStylesFromLocalStorage ( jetbrains ) ;
156+
157+ migrateLocalStorage ( dispatch ) ;
158+
159+ // Poll config just to be safe
160+ async function pollInitialConfigLoad ( ) {
103161 const result = await ideMessenger . request (
104162 "config/getSerializedProfileInfo" ,
105163 undefined ,
106164 ) ;
107165 if ( result . status === "success" ) {
108- await handleConfigUpdate ( true , result . content ) ;
109- }
110- dispatch ( setConfigLoading ( false ) ) ;
111- if ( initialSessionId ) {
112- await dispatch (
113- loadSession ( {
114- sessionId : initialSessionId ,
115- saveCurrentSession : false ,
116- } ) ,
117- ) ;
166+ console . log ( "succeeded" , result . content ) ;
167+ if ( hasReceivedOrRetrievedConfig . current === false ) {
168+ await handleConfigUpdate ( result . content ) ;
169+ }
170+ } else {
171+ console . log ( "Failed" ) ;
172+ console . error ( result . error ) ;
118173 }
119174 }
120- void initialLoadConfig ( ) ;
175+ void pollInitialConfigLoad ( ) ;
176+ let pollAttempts = 0 ;
121177 const interval = setInterval ( ( ) => {
122- if ( hasDoneInitialConfigLoad . current ) {
123- // Init to run on initial config load
124- ideMessenger . post ( "docs/initStatuses" , undefined ) ;
125- void dispatch ( updateFileSymbolsFromHistory ( ) ) ;
126- void dispatch ( refreshSessionMetadata ( { } ) ) ;
127-
128- // This triggers sending pending status to the GUI for relevant docs indexes
178+ if ( hasReceivedOrRetrievedConfig . current ) {
129179 clearInterval ( interval ) ;
180+ } else if ( pollAttempts >= INITIAL_CONFIG_POLLING_MAX_ATTEMPTS ) {
181+ clearInterval ( interval ) ;
182+ console . warn (
183+ `Config polling stopped after ${ INITIAL_CONFIG_POLLING_MAX_ATTEMPTS } attempts` ,
184+ ) ;
130185 } else {
131- void initialLoadConfig ( ) ;
186+ console . log ( "Config load attempt #" + pollAttempts ) ;
187+ pollAttempts ++ ;
188+ void pollInitialConfigLoad ( ) ;
132189 }
133- } , 2_000 ) ;
190+ } , INITIAL_CONFIG_POLLING_INTERVAL ) ;
134191
135192 return ( ) => clearInterval ( interval ) ;
136- } , [ hasDoneInitialConfigLoad , ideMessenger , initialSessionId ] ) ;
193+ } , [ hasReceivedOrRetrievedConfig , ideMessenger , handleConfigUpdate ] ) ;
137194
195+ // Handle config update events from core
138196 useWebviewListener (
139197 "configUpdate" ,
140198 async ( update ) => {
141199 if ( ! update ) {
142200 return ;
143201 }
144- await handleConfigUpdate ( false , update ) ;
202+ await handleConfigUpdate ( update ) ;
145203 } ,
146204 [ handleConfigUpdate ] ,
147205 ) ;
@@ -152,42 +210,6 @@ function ParallelListeners() {
152210 }
153211 } , [ sessionId ] ) ;
154212
155- // ON LOAD
156- useEffect ( ( ) => {
157- // Override persisted state
158- void dispatch ( cancelStream ( ) ) ;
159-
160- const jetbrains = isJetBrains ( ) ;
161- setDocumentStylesFromLocalStorage ( jetbrains ) ;
162-
163- if ( jetbrains ) {
164- // Save theme colors to local storage for immediate loading in JetBrains
165- void ideMessenger
166- . request ( "jetbrains/getColors" , undefined )
167- . then ( ( result ) => {
168- if ( result . status === "success" ) {
169- setDocumentStylesFromTheme ( result . content ) ;
170- }
171- } ) ;
172-
173- // Tell JetBrains the webview is ready
174- void ideMessenger
175- . request ( "jetbrains/onLoad" , undefined )
176- . then ( ( result ) => {
177- if ( result . status === "error" ) {
178- return ;
179- }
180-
181- const msg = result . content ;
182- ( window as any ) . windowId = msg . windowId ;
183- ( window as any ) . serverUrl = msg . serverUrl ;
184- ( window as any ) . workspacePaths = msg . workspacePaths ;
185- ( window as any ) . vscMachineId = msg . vscMachineId ;
186- ( window as any ) . vscMediaUrl = msg . vscMediaUrl ;
187- } ) ;
188- }
189- } , [ ] ) ;
190-
191213 useWebviewListener (
192214 "jetbrains/setColors" ,
193215 async ( data ) => {
@@ -253,10 +275,6 @@ function ParallelListeners() {
253275 }
254276 } , [ isInEdit , history ] ) ;
255277
256- useEffect ( ( ) => {
257- migrateLocalStorage ( dispatch ) ;
258- } , [ ] ) ;
259-
260278 return < > </ > ;
261279}
262280
0 commit comments