UNPKG

17.4 kBTypeScriptView Raw
1import { IChangedArgs } from '@jupyterlab/coreutils';
2import { Kernel, KernelMessage, KernelSpec, Session } from '@jupyterlab/services';
3import { ITranslator } from '@jupyterlab/translation';
4import { IterableOrArrayLike } from '@lumino/algorithm';
5import { IDisposable, IObservableDisposable } from '@lumino/disposable';
6import { ISignal } from '@lumino/signaling';
7/**
8 * A context object to manage a widget's kernel session connection.
9 *
10 * #### Notes
11 * The current session connection is `.session`, the current session's kernel
12 * connection is `.session.kernel`. For convenience, we proxy several kernel
13 * connection and session connection signals up to the session context so
14 * that you do not have to manage slots as sessions and kernels change. For
15 * example, to act on whatever the current kernel's iopubMessage signal is
16 * producing, connect to the session context `.iopubMessage` signal.
17 *
18 */
19export interface ISessionContext extends IObservableDisposable {
20 /**
21 * The current session connection.
22 */
23 session: Session.ISessionConnection | null;
24 /**
25 * Initialize the session context.
26 *
27 * @returns A promise that resolves with whether to ask the user to select a kernel.
28 *
29 * #### Notes
30 * This includes starting up an initial kernel if needed.
31 */
32 initialize(): Promise<boolean>;
33 /**
34 * Whether the session context is ready.
35 */
36 readonly isReady: boolean;
37 /**
38 * Whether the session context is terminating.
39 */
40 readonly isTerminating: boolean;
41 /**
42 * Whether the session context is restarting.
43 */
44 readonly isRestarting: boolean;
45 /**
46 * A promise that is fulfilled when the session context is ready.
47 */
48 readonly ready: Promise<void>;
49 /**
50 * A signal emitted when the session connection changes.
51 */
52 readonly sessionChanged: ISignal<this, IChangedArgs<Session.ISessionConnection | null, Session.ISessionConnection | null, 'session'>>;
53 /**
54 * A signal emitted when the kernel changes, proxied from the session connection.
55 */
56 readonly kernelChanged: ISignal<this, IChangedArgs<Kernel.IKernelConnection | null, Kernel.IKernelConnection | null, 'kernel'>>;
57 /**
58 * A signal emitted when the kernel status changes, proxied from the session connection.
59 */
60 readonly statusChanged: ISignal<this, Kernel.Status>;
61 /**
62 * A signal emitted when the kernel connection status changes, proxied from the session connection.
63 */
64 readonly connectionStatusChanged: ISignal<this, Kernel.ConnectionStatus>;
65 /**
66 * A flag indicating if session is has pending input, proxied from the session connection.
67 */
68 readonly pendingInput: boolean;
69 /**
70 * A signal emitted for a kernel messages, proxied from the session connection.
71 */
72 readonly iopubMessage: ISignal<this, KernelMessage.IMessage>;
73 /**
74 * A signal emitted for an unhandled kernel message, proxied from the session connection.
75 */
76 readonly unhandledMessage: ISignal<this, KernelMessage.IMessage>;
77 /**
78 * A signal emitted when a session property changes, proxied from the session connection.
79 */
80 readonly propertyChanged: ISignal<this, 'path' | 'name' | 'type'>;
81 /**
82 * The kernel preference for starting new kernels.
83 */
84 kernelPreference: ISessionContext.IKernelPreference;
85 /**
86 * Whether the kernel is "No Kernel" or not.
87 *
88 * #### Notes
89 * As the displayed name is translated, this can be used directly.
90 */
91 readonly hasNoKernel: boolean;
92 /**
93 * The sensible display name for the kernel, or translated "No Kernel"
94 *
95 * #### Notes
96 * This is at this level since the underlying kernel connection does not
97 * have access to the kernel spec manager.
98 */
99 readonly kernelDisplayName: string;
100 /**
101 * A sensible status to display
102 *
103 * #### Notes
104 * This combines the status and connection status into a single status for the user.
105 */
106 readonly kernelDisplayStatus: ISessionContext.KernelDisplayStatus;
107 /**
108 * The session path.
109 *
110 * #### Notes
111 * Typically `.session.path` should be used. This attribute is useful if
112 * there is no current session.
113 */
114 readonly path: string;
115 /**
116 * The session type.
117 *
118 * #### Notes
119 * Typically `.session.type` should be used. This attribute is useful if
120 * there is no current session.
121 */
122 readonly type: string;
123 /**
124 * The session name.
125 *
126 * #### Notes
127 * Typically `.session.name` should be used. This attribute is useful if
128 * there is no current session.
129 */
130 readonly name: string;
131 /**
132 * The previous kernel name.
133 */
134 readonly prevKernelName: string;
135 /**
136 * The session manager used by the session.
137 */
138 readonly sessionManager: Session.IManager;
139 /**
140 * The kernel spec manager
141 */
142 readonly specsManager: KernelSpec.IManager;
143 /**
144 * Restart the current Kernel.
145 *
146 * @returns A promise that resolves when the kernel is restarted.
147 */
148 restartKernel(): Promise<void>;
149 /**
150 * Kill the kernel and shutdown the session.
151 *
152 * @returns A promise that resolves when the session is shut down.
153 */
154 shutdown(): Promise<void>;
155 /**
156 * Change the kernel associated with the session.
157 *
158 * @param options The optional kernel model parameters to use for the new kernel.
159 *
160 * @returns A promise that resolves with the new kernel connection.
161 */
162 changeKernel(options?: Partial<Kernel.IModel>): Promise<Kernel.IKernelConnection | null>;
163}
164/**
165 * The namespace for session context related interfaces.
166 */
167export declare namespace ISessionContext {
168 /**
169 * A kernel preference.
170 *
171 * #### Notes
172 * Preferences for a kernel are considered in the order `id`, `name`,
173 * `language`. If no matching kernels can be found and `autoStartDefault` is
174 * `true`, then the default kernel for the server is preferred.
175 */
176 interface IKernelPreference {
177 /**
178 * The name of the kernel.
179 */
180 readonly name?: string;
181 /**
182 * The preferred kernel language.
183 */
184 readonly language?: string;
185 /**
186 * The id of an existing kernel.
187 */
188 readonly id?: string;
189 /**
190 * A kernel should be started automatically (default `true`).
191 */
192 readonly shouldStart?: boolean;
193 /**
194 * A kernel can be started (default `true`).
195 */
196 readonly canStart?: boolean;
197 /**
198 * Shut down the session when session context is disposed (default `false`).
199 */
200 readonly shutdownOnDispose?: boolean;
201 /**
202 * Automatically start the default kernel if no other matching kernel is
203 * found (default `true`).
204 */
205 readonly autoStartDefault?: boolean;
206 }
207 type KernelDisplayStatus = Kernel.Status | Kernel.ConnectionStatus | 'initializing' | '';
208 /**
209 * An interface for a session context dialog provider.
210 */
211 interface IDialogs {
212 /**
213 * Select a kernel for the session.
214 */
215 selectKernel(session: ISessionContext, translator?: ITranslator): Promise<void>;
216 /**
217 * Restart the session context.
218 *
219 * @returns A promise that resolves with whether the kernel has restarted.
220 *
221 * #### Notes
222 * If there is a running kernel, present a dialog.
223 * If there is no kernel, we start a kernel with the last run
224 * kernel name and resolves with `true`. If no kernel has been started,
225 * this is a no-op, and resolves with `false`.
226 */
227 restart(session: ISessionContext, translator?: ITranslator): Promise<boolean>;
228 }
229}
230/**
231 * The default implementation for a session context object.
232 */
233export declare class SessionContext implements ISessionContext {
234 /**
235 * Construct a new session context.
236 */
237 constructor(options: SessionContext.IOptions);
238 /**
239 * The current session connection.
240 */
241 get session(): Session.ISessionConnection | null;
242 /**
243 * The session path.
244 *
245 * #### Notes
246 * Typically `.session.path` should be used. This attribute is useful if
247 * there is no current session.
248 */
249 get path(): string;
250 /**
251 * The session type.
252 *
253 * #### Notes
254 * Typically `.session.type` should be used. This attribute is useful if
255 * there is no current session.
256 */
257 get type(): string;
258 /**
259 * The session name.
260 *
261 * #### Notes
262 * Typically `.session.name` should be used. This attribute is useful if
263 * there is no current session.
264 */
265 get name(): string;
266 /**
267 * A signal emitted when the kernel connection changes, proxied from the session connection.
268 */
269 get kernelChanged(): ISignal<this, Session.ISessionConnection.IKernelChangedArgs>;
270 /**
271 * A signal emitted when the session connection changes.
272 */
273 get sessionChanged(): ISignal<this, IChangedArgs<Session.ISessionConnection | null, Session.ISessionConnection | null, 'session'>>;
274 /**
275 * A signal emitted when the kernel status changes, proxied from the kernel.
276 */
277 get statusChanged(): ISignal<this, Kernel.Status>;
278 /**
279 * A flag indicating if the session has ending input, proxied from the kernel.
280 */
281 get pendingInput(): boolean;
282 /**
283 * A signal emitted when the kernel status changes, proxied from the kernel.
284 */
285 get connectionStatusChanged(): ISignal<this, Kernel.ConnectionStatus>;
286 /**
287 * A signal emitted for iopub kernel messages, proxied from the kernel.
288 */
289 get iopubMessage(): ISignal<this, KernelMessage.IIOPubMessage>;
290 /**
291 * A signal emitted for an unhandled kernel message, proxied from the kernel.
292 */
293 get unhandledMessage(): ISignal<this, KernelMessage.IMessage>;
294 /**
295 * A signal emitted when a session property changes, proxied from the current session.
296 */
297 get propertyChanged(): ISignal<this, 'path' | 'name' | 'type'>;
298 /**
299 * The kernel preference of this client session.
300 *
301 * This is used when selecting a new kernel, and should reflect the sort of
302 * kernel the activity prefers.
303 */
304 get kernelPreference(): ISessionContext.IKernelPreference;
305 set kernelPreference(value: ISessionContext.IKernelPreference);
306 /**
307 * Whether the context is ready.
308 */
309 get isReady(): boolean;
310 /**
311 * A promise that is fulfilled when the context is ready.
312 */
313 get ready(): Promise<void>;
314 /**
315 * Whether the context is terminating.
316 */
317 get isTerminating(): boolean;
318 /**
319 * Whether the context is restarting.
320 */
321 get isRestarting(): boolean;
322 /**
323 * The session manager used by the session.
324 */
325 readonly sessionManager: Session.IManager;
326 /**
327 * The kernel spec manager
328 */
329 readonly specsManager: KernelSpec.IManager;
330 /**
331 * Whether the kernel is "No Kernel" or not.
332 *
333 * #### Notes
334 * As the displayed name is translated, this can be used directly.
335 */
336 get hasNoKernel(): boolean;
337 /**
338 * The display name of the current kernel, or a sensible alternative.
339 *
340 * #### Notes
341 * This is a convenience function to have a consistent sensible name for the
342 * kernel.
343 */
344 get kernelDisplayName(): string;
345 /**
346 * A sensible status to display
347 *
348 * #### Notes
349 * This combines the status and connection status into a single status for
350 * the user.
351 */
352 get kernelDisplayStatus(): ISessionContext.KernelDisplayStatus;
353 /**
354 * The name of the previously started kernel.
355 */
356 get prevKernelName(): string;
357 /**
358 * Test whether the context is disposed.
359 */
360 get isDisposed(): boolean;
361 /**
362 * A signal emitted when the poll is disposed.
363 */
364 get disposed(): ISignal<this, void>;
365 /**
366 * Get the constant displayed name for "No Kernel"
367 */
368 protected get noKernelName(): string;
369 /**
370 * Dispose of the resources held by the context.
371 */
372 dispose(): void;
373 /**
374 * Restart the current Kernel.
375 *
376 * @returns A promise that resolves when the kernel is restarted.
377 */
378 restartKernel(): Promise<void>;
379 /**
380 * Change the current kernel associated with the session.
381 */
382 changeKernel(options?: Partial<Kernel.IModel>): Promise<Kernel.IKernelConnection | null>;
383 /**
384 * Kill the kernel and shutdown the session.
385 *
386 * @returns A promise that resolves when the session is shut down.
387 */
388 shutdown(): Promise<void>;
389 /**
390 * Initialize the session context
391 *
392 * @returns A promise that resolves with whether to ask the user to select a kernel.
393 *
394 * #### Notes
395 * If a server session exists on the current path, we will connect to it.
396 * If preferences include disabling `canStart` or `shouldStart`, no
397 * server session will be started.
398 * If a kernel id is given, we attempt to start a session with that id.
399 * If a default kernel is available, we connect to it.
400 * Otherwise we ask the user to select a kernel.
401 */
402 initialize(): Promise<boolean>;
403 /**
404 * Inner initialize function that doesn't handle promises.
405 * This makes it easier to consolidate promise handling logic.
406 */
407 _initialize(): Promise<boolean>;
408 /**
409 * Shut down the current session.
410 */
411 private _shutdownSession;
412 /**
413 * Start the session if necessary.
414 *
415 * @returns Whether to ask the user to pick a kernel.
416 */
417 private _startIfNecessary;
418 /**
419 * Change the kernel.
420 */
421 private _changeKernel;
422 /**
423 * Handle a new session object.
424 */
425 private _handleNewSession;
426 /**
427 * Handle an error in session startup.
428 */
429 private _handleSessionError;
430 /**
431 * Display kernel error
432 */
433 private _displayKernelError;
434 /**
435 * Handle a session termination.
436 */
437 private _onSessionDisposed;
438 /**
439 * Handle a change to a session property.
440 */
441 private _onPropertyChanged;
442 /**
443 * Handle a change to the kernel.
444 */
445 private _onKernelChanged;
446 /**
447 * Handle a change to the session status.
448 */
449 private _onStatusChanged;
450 /**
451 * Handle a change to the session status.
452 */
453 private _onConnectionStatusChanged;
454 /**
455 * Handle a change to the pending input.
456 */
457 private _onPendingInput;
458 /**
459 * Handle an iopub message.
460 */
461 private _onIopubMessage;
462 /**
463 * Handle an unhandled message.
464 */
465 private _onUnhandledMessage;
466 private _path;
467 private _name;
468 private _type;
469 private _prevKernelName;
470 private _kernelPreference;
471 private _isDisposed;
472 private _disposed;
473 private _session;
474 private _ready;
475 private _initializing;
476 private _initStarted;
477 private _initPromise;
478 private _isReady;
479 private _isTerminating;
480 private _isRestarting;
481 private _kernelChanged;
482 private _sessionChanged;
483 private _statusChanged;
484 private _connectionStatusChanged;
485 private translator;
486 private _trans;
487 private _pendingInput;
488 private _iopubMessage;
489 private _unhandledMessage;
490 private _propertyChanged;
491 private _dialog;
492 private _setBusy;
493 private _busyDisposable;
494 private _pendingKernelName;
495 private _pendingSessionRequest;
496}
497/**
498 * A namespace for `SessionContext` statics.
499 */
500export declare namespace SessionContext {
501 /**
502 * The options used to initialize a context.
503 */
504 interface IOptions {
505 /**
506 * A session manager instance.
507 */
508 sessionManager: Session.IManager;
509 /**
510 * A kernel spec manager instance.
511 */
512 specsManager: KernelSpec.IManager;
513 /**
514 * The initial path of the file.
515 */
516 path?: string;
517 /**
518 * The name of the session.
519 */
520 name?: string;
521 /**
522 * The type of the session.
523 */
524 type?: string;
525 /**
526 * A kernel preference.
527 */
528 kernelPreference?: ISessionContext.IKernelPreference;
529 /**
530 * The application language translator.
531 */
532 translator?: ITranslator;
533 /**
534 * A function to call when the session becomes busy.
535 */
536 setBusy?: () => IDisposable;
537 }
538 /**
539 * An interface for populating a kernel selector.
540 */
541 interface IKernelSearch {
542 /**
543 * The Kernel specs.
544 */
545 specs: KernelSpec.ISpecModels | null;
546 /**
547 * The kernel preference.
548 */
549 preference: ISessionContext.IKernelPreference;
550 /**
551 * The current running sessions.
552 */
553 sessions?: IterableOrArrayLike<Session.IModel>;
554 }
555 /**
556 * Get the default kernel name given select options.
557 */
558 function getDefaultKernel(options: IKernelSearch): string | null;
559}
560/**
561 * The default implementation of the client session dialog provider.
562 */
563export declare const sessionContextDialogs: ISessionContext.IDialogs;