This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
session.ts
168 lines (147 loc) · 4.02 KB
/
session.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { IClientSession } from '@jupyterlab/apputils';
import { CodeEditor } from '@jupyterlab/codeeditor';
import { KernelMessage } from '@jupyterlab/services';
import { PromiseDelegate } from '@phosphor/coreutils';
import { ISignal, Signal } from '@phosphor/signaling';
import { IDebugger } from './tokens';
export class DebugSession implements IDebugger.ISession {
/**
* Instantiate a new debug session
*
* @param options - The debug session instantiation options.
*/
constructor(options: DebugSession.IOptions) {
this.client = options.client;
this.client.iopubMessage.connect(this._handleEvent, this);
}
/**
* The client session to connect to a debugger.
*/
client: IClientSession;
/**
* The code editors in a debugger session.
*/
editors: CodeEditor.IEditor[];
/**
* Dispose the debug session.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._isDisposed = true;
this._disposed.emit();
Signal.clearData(this);
}
/**
* A signal emitted when the debug session is disposed.
*/
get disposed(): ISignal<this, void> {
return this._disposed;
}
/**
* Whether the debug session is disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* Start a new debug session
*/
async start(): Promise<void> {
await this.sendRequest('initialize', {
clientID: 'jupyterlab',
clientName: 'JupyterLab',
adapterID: 'python',
pathFormat: 'path',
linesStartAt1: true,
columnsStartAt1: true,
supportsVariableType: true,
supportsVariablePaging: true,
supportsRunInTerminalRequest: true,
locale: 'en-us'
});
await this.sendRequest('attach', {});
}
/**
* Stop the running debug session.
*/
async stop(): Promise<void> {
await this.sendRequest('disconnect', {
restart: false,
terminateDebuggee: true
});
}
/**
* Send a custom debug request to the kernel.
* @param command debug command.
* @param args arguments for the debug command.
*/
async sendRequest<K extends keyof IDebugger.ISession.Request>(
command: K,
args: IDebugger.ISession.Request[K]
): Promise<IDebugger.ISession.Response[K]> {
const message = await this._sendDebugMessage({
type: 'request',
seq: this._seq++,
command,
arguments: args
});
return message.content as IDebugger.ISession.Response[K];
}
/**
* Signal emitted for debug event messages.
*/
get eventMessage(): ISignal<DebugSession, IDebugger.ISession.Event> {
return this._eventMessage;
}
/**
* Handle debug events sent on the 'iopub' channel.
*/
private _handleEvent(
sender: IClientSession,
message: KernelMessage.IIOPubMessage
): void {
const msgType = message.header.msg_type;
if (msgType !== 'debug_event') {
return;
}
const event = message.content as IDebugger.ISession.Event;
this._eventMessage.emit(event);
}
/**
* Send a debug request message to the kernel.
* @param msg debug request message to send to the kernel.
*/
private async _sendDebugMessage(
msg: KernelMessage.IDebugRequestMsg['content']
): Promise<KernelMessage.IDebugReplyMsg> {
const reply = new PromiseDelegate<KernelMessage.IDebugReplyMsg>();
const kernel = this.client.kernel;
const future = kernel.requestDebug(msg);
future.onReply = (msg: KernelMessage.IDebugReplyMsg) => {
return reply.resolve(msg);
};
await future.done;
return reply.promise;
}
private _disposed = new Signal<this, void>(this);
private _isDisposed: boolean = false;
private _eventMessage = new Signal<DebugSession, IDebugger.ISession.Event>(
this
);
private _seq: number = 0;
}
/**
* A namespace for `DebugSession` statics.
*/
export namespace DebugSession {
export interface IOptions {
/**
* The client session used by the debug session.
*/
client: IClientSession;
}
}