@@ -94,24 +94,32 @@ public final class Service {
94
94
keyBindingManager. start ( )
95
95
96
96
Task {
97
- await XcodeInspector . shared. safe. $activeDocumentURL
98
- . removeDuplicates ( )
99
- . filter { $0 != . init( fileURLWithPath: " / " ) }
100
- . compactMap { $0 }
101
- . sink { [ weak self] fileURL in
102
- Task {
103
- do {
104
- let _ = try await self ? . workspacePool
105
- . fetchOrCreateWorkspaceAndFilespace ( fileURL: fileURL)
106
- } catch let error as Workspace . WorkspaceFileError {
107
- Logger . workspacePool
108
- . info ( error. localizedDescription)
109
- }
110
- catch {
111
- Logger . workspacePool. error ( error)
112
- }
97
+ await Publishers . CombineLatest (
98
+ XcodeInspector . shared. safe. $activeDocumentURL
99
+ . removeDuplicates ( ) ,
100
+ XcodeInspector . shared. safe. $latestActiveXcode
101
+ )
102
+ . receive ( on: DispatchQueue . main)
103
+ . sink { [ weak self] documentURL, latestXcode in
104
+ Task {
105
+ let fileURL = documentURL ?? latestXcode? . realtimeDocumentURL
106
+ guard fileURL != nil , fileURL != . init( fileURLWithPath: " / " ) else {
107
+ return
108
+ }
109
+ do {
110
+ let _ = try await self ? . workspacePool
111
+ . fetchOrCreateWorkspaceAndFilespace (
112
+ fileURL: fileURL!
113
+ )
114
+ } catch let error as Workspace . WorkspaceFileError {
115
+ Logger . workspacePool
116
+ . info ( error. localizedDescription)
117
+ }
118
+ catch {
119
+ Logger . workspacePool. error ( error)
113
120
}
114
- } . store ( in: & cancellable)
121
+ }
122
+ } . store ( in: & cancellable)
115
123
116
124
// Combine both workspace and auth status changes into a single stream
117
125
await Publishers . CombineLatest3 (
@@ -202,13 +210,11 @@ extension Service {
202
210
let name = self . getDisplayNameOfXcodeWorkspace ( url: workspaceURL)
203
211
let path = workspaceURL. path
204
212
205
- // switch workspace and username
206
- self . guiController. store. send ( . switchWorkspace( path: path, name: name, username: username) )
207
-
213
+ // switch workspace and username and wait for it to complete
214
+ await self . guiController. store. send ( . switchWorkspace( path: path, name: name, username: username) ) . finish ( )
208
215
// restore if needed
209
216
await self . guiController. restore ( path: path, name: name, username: username)
210
-
211
- // init chat tab if no history tab
212
- self . guiController. store. send ( . initWorkspaceChatTabIfNeeded( path: path, username: username) )
217
+ // init chat tab if no history tab (only after workspace is fully switched and restored)
218
+ await self . guiController. store. send ( . initWorkspaceChatTabIfNeeded( path: path, username: username) ) . finish ( )
213
219
}
214
220
}
0 commit comments