import { KernelMessage , Session } from '@jupyterlab/services' ; import { PromiseDelegate } from '@lumino/coreutils' ; import { ISignal , Signal } from '@lumino/signaling' ; import { IDebugger } from './tokens' ; /**
 * A concrete implementation of IDebugger.ISession.
 */ export class DebuggerSession implements IDebugger . ISession { param options constructor ( options ) { this . connection = options . connection ; } /**
     * Whether the debug session is disposed.
     */ get isDisposed ( ) : boolean { return this . _isDisposed ; } /**
     * A signal emitted when the debug session is disposed.
     */ get disposed ( ) : ISignal < this , void > { return this . _disposed ; } /**
     * Returns the API session connection to connect to a debugger.
     */ get connection ( ) : Session . ISessionConnection { return this . _connection ; } param connection set connection ( connection ) { if ( this . _connection ) { this . _connection . iopubMessage . disconnect ( this . _handleEvent , this ) ; } this . _connection = connection ; if ( ! this . _connection ) { this . _isStarted = false ; return ; } this . _connection . iopubMessage . connect ( this . _handleEvent , this ) ; this . _ready = new PromiseDelegate < void > ( ) ; const future = this . connection . kernel ?. requestDebug ( { type : 'request' , seq : 0 , command : 'debugInfo' } ) ; future . onReply = ( msg ) : void => { this . _ready . resolve ( ) ; future . dispose ( ) ; } ; } /**
     * Whether the debug session is started
     */ get isStarted ( ) : boolean { return this . _isStarted ; } /**
     * Signal emitted for debug event messages.
     */ get eventMessage ( ) : ISignal < IDebugger . ISession , IDebugger . ISession . Event > { return this . _eventMessage ; } /**
     * Dispose the debug session.
     */ dispose ( ) { if ( this . _isDisposed ) { return ; } this . _isDisposed = true ; this . _disposed . emit ( ) ; Signal . clearData ( this ) ; } /**
     * Start a new debug session
     */ async start ( ) { await this . sendRequest ( 'initialize' , { clientID : 'jupyterlab' , clientName : 'JupyterLab' , adapterID : this . connection . kernel ?. name ?? '' , pathFormat : 'path' , linesStartAt1 : true , columnsStartAt1 : true , supportsVariableType : true , supportsVariablePaging : true , supportsRunInTerminalRequest : true , locale : 'en-us' } ) ; this . _isStarted = true ; await this . sendRequest ( 'attach' , { } ) ; } /**
     * Stop the running debug session.
     */ async stop ( ) { await this . sendRequest ( 'disconnect' , { restart : false , terminateDebuggee : false } ) ; this . _isStarted = false ; } /**
     * Restore the state of a debug session.
     */ async restoreState ( ) { const message = await this . sendRequest ( 'debugInfo' , { } ) ; this . _isStarted = message . body . isStarted ; return message ; } param command param args async sendRequest < K extends keyof IDebugger . ISession . Request > ( command , args ) { await this . _ready . promise ; const message = await this . _sendDebugMessage ( { type : 'request' , seq : this . _seq ++ , command , arguments : args } ) ; return message . content as IDebugger . ISession . Response [ K ] ; } param sender param message private _handleEvent ( sender , message ) { const msgType = message . header . msg_type ; if ( msgType !== 'debug_event' ) { return ; } const event = message . content as IDebugger . ISession . Event ; this . _eventMessage . emit ( event ) ; } param msg private async _sendDebugMessage ( msg ) { const kernel = this . connection . kernel ; if ( ! kernel ) { return Promise . reject ( new Error ( 'A kernel is required to send debug messages.' ) ) ; } const reply = new PromiseDelegate < KernelMessage . IDebugReplyMsg > ( ) ; const future = kernel . requestDebug ( msg ) ; future . onReply = ( msg ) : void => { reply . resolve ( msg ) ; } ; await future . done ; return reply . promise ; } private _seq = 0 ; private _ready = new PromiseDelegate < void > ( ) ; private _connection : Session . ISessionConnection ; private _isDisposed = false ; private _isStarted = false ; private _disposed = new Signal < this , void > ( this ) ; private _eventMessage = new Signal < IDebugger . ISession , IDebugger . ISession . Event > ( this ) ; } /**
 * A namespace for `DebuggerSession` statics.
 */ export namespace DebuggerSession { /**
     * Instantiation options for a `DebuggerSession`.
     */ export interface IOptions { /**
         * The session connection used by the debug session.
         */ connection : Session . ISessionConnection ; } }