55
66import * as positron from 'positron' ;
77import * as vscode from 'vscode' ;
8- import { RSession } from './session' ;
8+ import { RSession , getActiveRSessions } from './session' ;
99
1010/**
11- * Manages all the R sessions. We keep our own references to each session in a
12- * singleton instance of this class so that we can invoke methods/check status
13- * directly, without going through Positron's API.
11+ * Manages all the R sessions.
1412 */
1513export class RSessionManager implements vscode . Disposable {
1614 /// Singleton instance
@@ -21,9 +19,6 @@ export class RSessionManager implements vscode.Disposable {
2119 /// but we may improve on this in the future so it is good practice to track them.
2220 private readonly _disposables : vscode . Disposable [ ] = [ ] ;
2321
24- /// Map of session IDs to RSession instances
25- private _sessions : Map < string , RSession > = new Map ( ) ;
26-
2722 /// The most recent foreground R session (foreground implies it is a console session)
2823 private _lastForegroundSessionId : string | null = null ;
2924
@@ -50,24 +45,11 @@ export class RSessionManager implements vscode.Disposable {
5045 }
5146
5247 /**
53- * Registers a runtime with the manager. Throws an error if a runtime with
54- * the same ID is already registered.
48+ * Registers a runtime with the manager.
5549 *
56- * @param id The runtime's ID
57- * @param runtime The runtime.
50+ * @param session The session.
5851 */
59- setSession ( sessionId : string , session : RSession ) : void {
60- if ( this . _sessions . has ( sessionId ) ) {
61- throw new Error ( `Session ${ sessionId } already registered.` ) ;
62- }
63- this . _sessions . set ( sessionId , session ) ;
64-
65- session . register ( {
66- dispose : ( ) => {
67- this . _sessions . delete ( sessionId ) ;
68- }
69- } ) ;
70-
52+ setSession ( session : RSession ) : void {
7153 session . register (
7254 session . onDidChangeRuntimeState ( async ( state ) => {
7355 await this . didChangeSessionRuntimeState ( session , state ) ;
@@ -106,9 +88,8 @@ export class RSessionManager implements vscode.Disposable {
10688 return ;
10789 }
10890
109- // TODO: Switch to `getActiveRSessions()` built on `positron.runtime.getActiveSessions()`
110- // and remove `this._sessions` entirely.
111- const session = this . _sessions . get ( sessionId ) ;
91+ const sessions = await getActiveRSessions ( ) ;
92+ const session = sessions . find ( s => s . metadata . sessionId === sessionId ) ;
11293 if ( ! session ) {
11394 // The foreground session is for another language.
11495 return ;
@@ -127,7 +108,8 @@ export class RSessionManager implements vscode.Disposable {
127108 */
128109 private async activateConsoleSession ( session : RSession , reason : string ) : Promise < void > {
129110 // Deactivate other console session servers first
130- await Promise . all ( Array . from ( this . _sessions . values ( ) )
111+ const sessions = await getActiveRSessions ( ) ;
112+ await Promise . all ( sessions
131113 . filter ( s => {
132114 return s . metadata . sessionId !== session . metadata . sessionId &&
133115 s . metadata . sessionMode === positron . LanguageRuntimeSessionMode . Console ;
@@ -159,9 +141,10 @@ export class RSessionManager implements vscode.Disposable {
159141 *
160142 * @returns The R console session, or undefined if there isn't one.
161143 */
162- getConsoleSession ( ) : RSession | undefined {
144+ async getConsoleSession ( ) : Promise < RSession | undefined > {
145+ const sessions = await getActiveRSessions ( ) ;
146+
163147 // Sort the sessions by creation time (descending)
164- const sessions = Array . from ( this . _sessions . values ( ) ) ;
165148 sessions . sort ( ( a , b ) => b . created - a . created ) ;
166149
167150 // Remove any sessions that aren't console sessions and have either
@@ -193,8 +176,9 @@ export class RSessionManager implements vscode.Disposable {
193176 * @param sessionId The session identifier
194177 * @returns The R session, or undefined if not found
195178 */
196- getSessionById ( sessionId : string ) : RSession | undefined {
197- return this . _sessions . get ( sessionId ) ;
179+ async getSessionById ( sessionId : string ) : Promise < RSession | undefined > {
180+ const sessions = await getActiveRSessions ( ) ;
181+ return sessions . find ( s => s . metadata . sessionId === sessionId ) ;
198182 }
199183
200184 /**
0 commit comments