|
| 1 | +// SessionManager exposes `JupyterLab.serviceManager.sessions` to user python kernel |
| 2 | + |
| 3 | +import { SessionManager } from '@jupyterlab/services'; |
| 4 | +import { ISerializers, WidgetModel } from '@jupyter-widgets/base'; |
| 5 | +import { toArray } from '@lumino/algorithm'; |
| 6 | +import { MODULE_NAME, MODULE_VERSION } from '../version'; |
| 7 | +import { Session } from '@jupyterlab/services'; |
| 8 | +import { ILabShell } from '@jupyterlab/application'; |
| 9 | + |
| 10 | +/** |
| 11 | + * The model for a Session Manager |
| 12 | + */ |
| 13 | +export class SessionManagerModel extends WidgetModel { |
| 14 | + /** |
| 15 | + * The default attributes. |
| 16 | + */ |
| 17 | + defaults(): any { |
| 18 | + return { |
| 19 | + ...super.defaults(), |
| 20 | + _model_name: SessionManagerModel.model_name, |
| 21 | + _model_module: SessionManagerModel.model_module, |
| 22 | + _model_module_version: SessionManagerModel.model_module_version, |
| 23 | + current_session: null, |
| 24 | + sessions: [], |
| 25 | + }; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Initialize a SessionManagerModel instance. |
| 30 | + * |
| 31 | + * @param attributes The base attributes. |
| 32 | + * @param options The initialization options. |
| 33 | + */ |
| 34 | + initialize(attributes: any, options: any): void { |
| 35 | + const { sessions, shell } = SessionManagerModel; |
| 36 | + this._sessions = sessions; |
| 37 | + this._shell = shell; |
| 38 | + sessions.runningChanged.connect(this._sendSessions, this); |
| 39 | + shell.currentChanged.connect(this._currentChanged, this); |
| 40 | + |
| 41 | + super.initialize(attributes, options); |
| 42 | + this.on('msg:custom', this._onMessage.bind(this)); |
| 43 | + this._shell.activeChanged.connect(this._currentChanged, this); |
| 44 | + this._sendSessions(); |
| 45 | + this._sendCurrent(); |
| 46 | + this.send({ event: 'sessions_initialized' }, {}); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Handle a custom message from the backend. |
| 51 | + * |
| 52 | + * @param msg The message to handle. |
| 53 | + */ |
| 54 | + private _onMessage(msg: any): void { |
| 55 | + switch (msg.func) { |
| 56 | + case 'refreshRunning': |
| 57 | + this._sessions.refreshRunning().then(() => { |
| 58 | + this.send({ event: 'sessions_refreshed' }, {}); |
| 59 | + }); |
| 60 | + break; |
| 61 | + default: |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * get sessionContext from a given widget instance |
| 68 | + * |
| 69 | + * @param widget widget tracked by app.shell._track (FocusTracker) |
| 70 | + */ |
| 71 | + private _getSessionContext(widget: any): Session.IModel | {} { |
| 72 | + return widget?.sessionContext?.session?.model ?? {}; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Handle focus change in JLab |
| 77 | + * |
| 78 | + * NOTE: currentChange fires on two situations that we are concerned about here: |
| 79 | + * 1. when user focuses on a widget in browser, which the `change.newValue` will |
| 80 | + * be the current Widget |
| 81 | + * 2. when user executes a code in console/notebook, where the `changed.newValue` will be null since |
| 82 | + * we lost focus due to execution. |
| 83 | + * To solve this problem, we interrogate `this._tracker.currentWidget` directly. |
| 84 | + * We also added a simple fencing to reduce the number of Comm sync calls between Python/JS |
| 85 | + */ |
| 86 | + private _currentChanged(): void { |
| 87 | + this._current_session = this._getSessionContext(this._shell.currentWidget); |
| 88 | + this.set('current_session', this._current_session); |
| 89 | + this.set('sessions', toArray(this._sessions.running())); |
| 90 | + this.save_changes(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Send the list of sessions to the backend. |
| 95 | + */ |
| 96 | + private _sendSessions(): void { |
| 97 | + this.set('sessions', toArray(this._sessions.running())); |
| 98 | + this.save_changes(); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * send current session to backend |
| 103 | + */ |
| 104 | + private _sendCurrent(): void { |
| 105 | + this._current_session = this._getSessionContext(this._shell.currentWidget); |
| 106 | + this.set('current_session', this._current_session); |
| 107 | + this.save_changes(); |
| 108 | + } |
| 109 | + |
| 110 | + static serializers: ISerializers = { |
| 111 | + ...WidgetModel.serializers, |
| 112 | + }; |
| 113 | + |
| 114 | + static model_name = 'SessionManagerModel'; |
| 115 | + static model_module = MODULE_NAME; |
| 116 | + static model_module_version = MODULE_VERSION; |
| 117 | + static view_name: string = null; |
| 118 | + static view_module: string = null; |
| 119 | + static view_module_version = MODULE_VERSION; |
| 120 | + |
| 121 | + private _current_session: Session.IModel | {}; |
| 122 | + private _sessions: SessionManager; |
| 123 | + static sessions: SessionManager; |
| 124 | + private _shell: ILabShell; |
| 125 | + static shell: ILabShell; |
| 126 | +} |
0 commit comments