@@ -9,7 +9,9 @@ import * as vsls from 'vsls';
99import * as fs from 'fs-extra' ;
1010
1111import { enableSessionWatcher , extensionContext } from '../extension' ;
12- import { activateRSessionGuest , browserDisposables , initGuest } from './shareSession' ;
12+ import { docScheme , docProvider } from './virtualDocs' ;
13+ import * as path from 'path' ;
14+ import { browserDisposables } from './shareSession' ;
1315import { initTreeView , rLiveShareProvider , shareWorkspace , ToggleNode } from './shareTree' ;
1416import { Commands , Callback , liveShareOnRequest , liveShareRequest } from './shareCommands' ;
1517
@@ -79,6 +81,10 @@ export async function initLiveShare(context: vscode.ExtensionContext): Promise<v
7981 void vscode . commands . executeCommand ( 'setContext' , 'r.liveShare:isGuest' , isGuestSession ) ;
8082
8183 // push commands
84+ context . subscriptions . push (
85+ vscode . commands . registerCommand ( 'r.activateRSessionGuest' , ( ) => activateRSessionGuest ( ) )
86+ ) ;
87+
8288 if ( ! isGuestSession ) {
8389 context . subscriptions . push (
8490 vscode . commands . registerCommand (
@@ -91,10 +97,6 @@ export async function initLiveShare(context: vscode.ExtensionContext): Promise<v
9197 }
9298 )
9399 ) ;
94- } else {
95- context . subscriptions . push (
96- vscode . commands . registerCommand ( 'r.activateRSessionGuest' , ( ) => activateRSessionGuest ( ) )
97- ) ;
98100 }
99101 }
100102}
@@ -144,6 +146,7 @@ export async function LiveSessionListener(): Promise<void> {
144146 break ;
145147 case vsls . Role . Guest :
146148 console . log ( '[LiveSessionListener] guest event' ) ;
149+ initGuest ( extensionContext ) ;
147150 await rGuestService ?. startService ( ) ;
148151 break ;
149152 case vsls . Role . Host :
@@ -165,6 +168,7 @@ export async function LiveSessionListener(): Promise<void> {
165168 break ;
166169 case vsls . Role . Guest :
167170 console . log ( '[LiveSessionListener] guest event' ) ;
171+ initGuest ( extensionContext ) ;
168172 await rGuestService . startService ( ) ;
169173 break ;
170174 default :
@@ -200,7 +204,19 @@ export class HostService {
200204 // Provides core liveshare functionality
201205 // The shared service is used as a RPC service
202206 // to pass messages between the host and guests
203- service = await liveSession . shareService ( ShareProviderName ) ;
207+ console . info ( `[HostService] Attempting to share service: ${ ShareProviderName } ` ) ;
208+ try {
209+ await liveSession . unshareService ( ShareProviderName ) ;
210+ } catch ( e ) {
211+ // ignore error if it wasn't shared
212+ }
213+ try {
214+ service = await liveSession . shareService ( ShareProviderName ) ;
215+ console . info ( `[HostService] shareService returned: ${ String ( ! ! service ) } ` ) ;
216+ } catch ( e ) {
217+ console . error ( `[HostService] shareService threw error: ${ String ( e ) } ` ) ;
218+ service = null ;
219+ }
204220 if ( service ) {
205221 this . _isStarted = true ;
206222 for ( const command in Commands . host ) {
@@ -233,9 +249,9 @@ export class HostService {
233249 void this . notifyWorkspace ( workspaceData ) ;
234250 }
235251 }
236- public notifyPlot ( file : string ) : void {
252+ public notifyPlot ( data : string , format : string ) : void {
237253 if ( this . _isStarted && shareWorkspace ) {
238- void liveShareRequest ( Callback . NotifyPlotUpdate , file ) ;
254+ void liveShareRequest ( Callback . NotifyPlotUpdate , data , format ) ;
239255 }
240256 }
241257 public notifyGuestPlotManager ( url : string ) : void {
@@ -256,7 +272,9 @@ export class GuestService {
256272 return this . _isStarted ;
257273 }
258274 public async startService ( ) : Promise < void > {
275+ console . info ( `[GuestService] Attempting to get shared service: ${ ShareProviderName } ` ) ;
259276 service = await liveSession . getSharedService ( ShareProviderName ) ;
277+ console . info ( `[GuestService] getSharedService returned: ${ String ( ! ! service ) } ` ) ;
260278 if ( service ) {
261279 this . _isStarted = true ;
262280 this . requestAttach ( ) ;
@@ -352,3 +370,45 @@ async function sessionCleanup(): Promise<void> {
352370 rLiveShareProvider . refresh ( ) ;
353371 }
354372}
373+
374+ export let guestResDir : string ;
375+
376+ export function initGuest ( context : vscode . ExtensionContext ) : void {
377+ if ( _sessionStatusBarItem ) {
378+ console . info ( 'initGuest: sessionStatusBarItem already exists' ) ;
379+ return ;
380+ }
381+ console . info ( 'Create guestSessionStatusBarItem' ) ;
382+ const sessionStatusBarItem = vscode . window . createStatusBarItem ( vscode . StatusBarAlignment . Right , 1000 ) ;
383+ sessionStatusBarItem . command = 'r.activateRSessionGuest' ;
384+ sessionStatusBarItem . text = 'Guest R: (not attached)' ;
385+ sessionStatusBarItem . tooltip = 'Click to activate host R session' ;
386+ sessionStatusBarItem . show ( ) ;
387+ context . subscriptions . push (
388+ sessionStatusBarItem ,
389+ vscode . workspace . registerTextDocumentContentProvider ( docScheme , docProvider )
390+ ) ;
391+ _sessionStatusBarItem = sessionStatusBarItem ;
392+ rGuestService ?. setStatusBarItem ( sessionStatusBarItem ) ;
393+ guestResDir = path . join ( context . extensionPath , 'dist' , 'resources' ) ;
394+ }
395+
396+ export async function activateRSessionGuest ( ) : Promise < void > {
397+ if ( ! isGuest ( ) ) {
398+ void vscode . window . showInformationMessage ( 'This command is only available for guests.' ) ;
399+ return ;
400+ }
401+ if ( config ( ) . get < boolean > ( 'sessionWatcher' , true ) ) {
402+ console . info ( '[activateRSessionGuest]' ) ;
403+ if ( ! rGuestService ?. isStarted ( ) ) {
404+ await rGuestService ?. startService ( ) ;
405+ }
406+ if ( rGuestService ?. isStarted ( ) ) {
407+ void rGuestService . requestAttach ( ) ;
408+ } else {
409+ void vscode . window . showWarningMessage ( 'Failed to connect to host R service. Please ensure the host has enabled sharing.' ) ;
410+ }
411+ } else {
412+ void vscode . window . showInformationMessage ( 'This command requires that r.sessionWatcher be enabled.' ) ;
413+ }
414+ }
0 commit comments