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
231 lines (206 loc) · 5.65 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
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 DebugSession implements IDebugger.ISession {
/**
* Instantiate a new debug session
*
* @param options - The debug session instantiation options.
*/
constructor(options: DebugSession.IOptions) {
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;
}
/**
* Sets the API session connection to connect to a debugger to
* the given parameter.
*
* @param connection - The new API session connection.
*/
set connection(connection: Session.ISessionConnection | null) {
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: KernelMessage.IDebugReplyMsg): 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(): void {
if (this._isDisposed) {
return;
}
this._isDisposed = true;
this._disposed.emit();
Signal.clearData(this);
}
/**
* Start a new debug session
*/
async start(): Promise<void> {
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(): Promise<void> {
await this.sendRequest('disconnect', {
restart: false,
terminateDebuggee: true
});
this._isStarted = false;
}
/**
* Restore the state of a debug session.
*/
async restoreState(): Promise<IDebugger.ISession.Response['debugInfo']> {
const message = await this.sendRequest('debugInfo', {});
this._isStarted = message.body.isStarted;
return message;
}
/**
* 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]> {
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];
}
/**
* Handle debug events sent on the 'iopub' channel.
*
* @param sender - the emitter of the event.
* @param message - the event message.
*/
private _handleEvent(
sender: Session.ISessionConnection,
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 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: KernelMessage.IDebugReplyMsg): 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 `DebugSession` statics.
*/
export namespace DebugSession {
/**
* Instantiation options for a `DebugSession`.
*/
export interface IOptions {
/**
* The session connection used by the debug session.
*/
connection: Session.ISessionConnection;
}
}