UNPKG

10.9 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Jupyter Development Team.
3// Distributed under the terms of the Modified BSD License.
4Object.defineProperty(exports, "__esModule", { value: true });
5exports.SessionConnection = void 0;
6const signaling_1 = require("@lumino/signaling");
7const __1 = require("..");
8const restapi_1 = require("./restapi");
9const coreutils_1 = require("@lumino/coreutils");
10/**
11 * Session object for accessing the session REST api. The session
12 * should be used to start kernels and then shut them down -- for
13 * all other kernel operations, the kernel object should be used.
14 */
15class SessionConnection {
16 /**
17 * Construct a new session.
18 */
19 constructor(options) {
20 var _a, _b, _c, _d;
21 this._id = '';
22 this._path = '';
23 this._name = '';
24 this._type = '';
25 this._kernel = null;
26 this._isDisposed = false;
27 this._disposed = new signaling_1.Signal(this);
28 this._kernelChanged = new signaling_1.Signal(this);
29 this._statusChanged = new signaling_1.Signal(this);
30 this._connectionStatusChanged = new signaling_1.Signal(this);
31 this._pendingInput = new signaling_1.Signal(this);
32 this._iopubMessage = new signaling_1.Signal(this);
33 this._unhandledMessage = new signaling_1.Signal(this);
34 this._anyMessage = new signaling_1.Signal(this);
35 this._propertyChanged = new signaling_1.Signal(this);
36 this._id = options.model.id;
37 this._name = options.model.name;
38 this._path = options.model.path;
39 this._type = options.model.type;
40 this._username = (_a = options.username) !== null && _a !== void 0 ? _a : '';
41 this._clientId = (_b = options.clientId) !== null && _b !== void 0 ? _b : coreutils_1.UUID.uuid4();
42 this._connectToKernel = options.connectToKernel;
43 this._kernelConnectionOptions = (_c = options.kernelConnectionOptions) !== null && _c !== void 0 ? _c : {};
44 this.serverSettings =
45 (_d = options.serverSettings) !== null && _d !== void 0 ? _d : __1.ServerConnection.makeSettings();
46 this.setupKernel(options.model.kernel);
47 }
48 /**
49 * A signal emitted when the session is disposed.
50 */
51 get disposed() {
52 return this._disposed;
53 }
54 /**
55 * A signal emitted when the kernel changes.
56 */
57 get kernelChanged() {
58 return this._kernelChanged;
59 }
60 /**
61 * A signal proxied from the connection about the kernel status.
62 */
63 get statusChanged() {
64 return this._statusChanged;
65 }
66 /**
67 * A signal proxied from the kernel about the connection status.
68 */
69 get connectionStatusChanged() {
70 return this._connectionStatusChanged;
71 }
72 /**
73 * A signal proxied from the kernel pending input.
74 */
75 get pendingInput() {
76 return this._pendingInput;
77 }
78 /**
79 * A signal proxied from the kernel about iopub kernel messages.
80 */
81 get iopubMessage() {
82 return this._iopubMessage;
83 }
84 /**
85 * A signal proxied from the kernel for an unhandled kernel message.
86 */
87 get unhandledMessage() {
88 return this._unhandledMessage;
89 }
90 /**
91 * A signal proxied from the kernel emitted for any kernel message.
92 *
93 * #### Notes
94 * The behavior is undefined if the message is modified during message
95 * handling. As such, it should be treated as read-only.
96 */
97 get anyMessage() {
98 return this._anyMessage;
99 }
100 /**
101 * A signal emitted when a session property changes.
102 */
103 get propertyChanged() {
104 return this._propertyChanged;
105 }
106 /**
107 * Get the session id.
108 */
109 get id() {
110 return this._id;
111 }
112 /**
113 * Get the session kernel connection object.
114 *
115 * #### Notes
116 * This is a read-only property, and can be altered by [changeKernel].
117 */
118 get kernel() {
119 return this._kernel;
120 }
121 /**
122 * Get the session path.
123 */
124 get path() {
125 return this._path;
126 }
127 /**
128 * Get the session type.
129 */
130 get type() {
131 return this._type;
132 }
133 /**
134 * Get the session name.
135 */
136 get name() {
137 return this._name;
138 }
139 /**
140 * Get the model associated with the session.
141 */
142 get model() {
143 return {
144 id: this.id,
145 kernel: this.kernel && { id: this.kernel.id, name: this.kernel.name },
146 path: this._path,
147 type: this._type,
148 name: this._name
149 };
150 }
151 /**
152 * Test whether the session has been disposed.
153 */
154 get isDisposed() {
155 return this._isDisposed;
156 }
157 /**
158 * Update the session based on a session model from the server.
159 *
160 * #### Notes
161 * This only updates this session connection instance. Use `setPath`,
162 * `setName`, `setType`, and `changeKernel` to change the session values on
163 * the server.
164 */
165 update(model) {
166 const oldModel = this.model;
167 this._path = model.path;
168 this._name = model.name;
169 this._type = model.type;
170 if ((this._kernel === null && model.kernel !== null) ||
171 (this._kernel !== null && model.kernel === null) ||
172 (this._kernel !== null &&
173 model.kernel !== null &&
174 this._kernel.id !== model.kernel.id)) {
175 if (this._kernel !== null) {
176 this._kernel.dispose();
177 }
178 const oldValue = this._kernel || null;
179 this.setupKernel(model.kernel);
180 const newValue = this._kernel || null;
181 this._kernelChanged.emit({ name: 'kernel', oldValue, newValue });
182 }
183 this._handleModelChange(oldModel);
184 }
185 /**
186 * Dispose of the resources held by the session.
187 */
188 dispose() {
189 if (this.isDisposed) {
190 return;
191 }
192 this._isDisposed = true;
193 this._disposed.emit();
194 if (this._kernel) {
195 this._kernel.dispose();
196 const oldValue = this._kernel;
197 this._kernel = null;
198 const newValue = this._kernel;
199 this._kernelChanged.emit({ name: 'kernel', oldValue, newValue });
200 }
201 signaling_1.Signal.clearData(this);
202 }
203 /**
204 * Change the session path.
205 *
206 * @param path - The new session path.
207 *
208 * @returns A promise that resolves when the session has renamed.
209 *
210 * #### Notes
211 * This uses the Jupyter REST API, and the response is validated.
212 * The promise is fulfilled on a valid response and rejected otherwise.
213 */
214 async setPath(path) {
215 if (this.isDisposed) {
216 throw new Error('Session is disposed');
217 }
218 await this._patch({ path });
219 }
220 /**
221 * Change the session name.
222 */
223 async setName(name) {
224 if (this.isDisposed) {
225 throw new Error('Session is disposed');
226 }
227 await this._patch({ name });
228 }
229 /**
230 * Change the session type.
231 */
232 async setType(type) {
233 if (this.isDisposed) {
234 throw new Error('Session is disposed');
235 }
236 await this._patch({ type });
237 }
238 /**
239 * Change the kernel.
240 *
241 * @param options - The name or id of the new kernel.
242 *
243 * #### Notes
244 * This shuts down the existing kernel and creates a new kernel,
245 * keeping the existing session ID and session path.
246 */
247 async changeKernel(options) {
248 if (this.isDisposed) {
249 throw new Error('Session is disposed');
250 }
251 await this._patch({ kernel: options });
252 return this.kernel;
253 }
254 /**
255 * Kill the kernel and shutdown the session.
256 *
257 * @returns - The promise fulfilled on a valid response from the server.
258 *
259 * #### Notes
260 * Uses the [Jupyter Server API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/sessions), and validates the response.
261 * Disposes of the session and emits a [sessionDied] signal on success.
262 */
263 async shutdown() {
264 if (this.isDisposed) {
265 throw new Error('Session is disposed');
266 }
267 await (0, restapi_1.shutdownSession)(this.id, this.serverSettings);
268 this.dispose();
269 }
270 /**
271 * Create a new kernel connection and connect to its signals.
272 *
273 * #### Notes
274 * This method is not meant to be subclassed.
275 */
276 setupKernel(model) {
277 if (model === null) {
278 this._kernel = null;
279 return;
280 }
281 const kc = this._connectToKernel({
282 ...this._kernelConnectionOptions,
283 model,
284 username: this._username,
285 clientId: this._clientId,
286 serverSettings: this.serverSettings
287 });
288 this._kernel = kc;
289 kc.statusChanged.connect(this.onKernelStatus, this);
290 kc.connectionStatusChanged.connect(this.onKernelConnectionStatus, this);
291 kc.pendingInput.connect(this.onPendingInput, this);
292 kc.unhandledMessage.connect(this.onUnhandledMessage, this);
293 kc.iopubMessage.connect(this.onIOPubMessage, this);
294 kc.anyMessage.connect(this.onAnyMessage, this);
295 }
296 /**
297 * Handle to changes in the Kernel status.
298 */
299 onKernelStatus(sender, state) {
300 this._statusChanged.emit(state);
301 }
302 /**
303 * Handle to changes in the Kernel status.
304 */
305 onKernelConnectionStatus(sender, state) {
306 this._connectionStatusChanged.emit(state);
307 }
308 /**
309 * Handle a change in the pendingInput.
310 */
311 onPendingInput(sender, state) {
312 this._pendingInput.emit(state);
313 }
314 /**
315 * Handle iopub kernel messages.
316 */
317 onIOPubMessage(sender, msg) {
318 this._iopubMessage.emit(msg);
319 }
320 /**
321 * Handle unhandled kernel messages.
322 */
323 onUnhandledMessage(sender, msg) {
324 this._unhandledMessage.emit(msg);
325 }
326 /**
327 * Handle any kernel messages.
328 */
329 onAnyMessage(sender, args) {
330 this._anyMessage.emit(args);
331 }
332 /**
333 * Send a PATCH to the server, updating the session path or the kernel.
334 */
335 async _patch(body) {
336 const model = await (0, restapi_1.updateSession)({ ...body, id: this._id }, this.serverSettings);
337 this.update(model);
338 return model;
339 }
340 /**
341 * Handle a change to the model.
342 */
343 _handleModelChange(oldModel) {
344 if (oldModel.name !== this._name) {
345 this._propertyChanged.emit('name');
346 }
347 if (oldModel.type !== this._type) {
348 this._propertyChanged.emit('type');
349 }
350 if (oldModel.path !== this._path) {
351 this._propertyChanged.emit('path');
352 }
353 }
354}
355exports.SessionConnection = SessionConnection;
356//# sourceMappingURL=default.js.map
\No newline at end of file