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