1 | "use strict";
|
2 |
|
3 |
|
4 | Object.defineProperty(exports, "__esModule", { value: true });
|
5 | exports.SessionConnection = void 0;
|
6 | const signaling_1 = require("@lumino/signaling");
|
7 | const __1 = require("..");
|
8 | const restapi_1 = require("./restapi");
|
9 | const coreutils_1 = require("@lumino/coreutils");
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | class SessionConnection {
|
16 | |
17 |
|
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 |
|
50 |
|
51 | get disposed() {
|
52 | return this._disposed;
|
53 | }
|
54 | |
55 |
|
56 |
|
57 | get kernelChanged() {
|
58 | return this._kernelChanged;
|
59 | }
|
60 | |
61 |
|
62 |
|
63 | get statusChanged() {
|
64 | return this._statusChanged;
|
65 | }
|
66 | |
67 |
|
68 |
|
69 | get connectionStatusChanged() {
|
70 | return this._connectionStatusChanged;
|
71 | }
|
72 | |
73 |
|
74 |
|
75 | get pendingInput() {
|
76 | return this._pendingInput;
|
77 | }
|
78 | |
79 |
|
80 |
|
81 | get iopubMessage() {
|
82 | return this._iopubMessage;
|
83 | }
|
84 | |
85 |
|
86 |
|
87 | get unhandledMessage() {
|
88 | return this._unhandledMessage;
|
89 | }
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | get anyMessage() {
|
98 | return this._anyMessage;
|
99 | }
|
100 | |
101 |
|
102 |
|
103 | get propertyChanged() {
|
104 | return this._propertyChanged;
|
105 | }
|
106 | |
107 |
|
108 |
|
109 | get id() {
|
110 | return this._id;
|
111 | }
|
112 | |
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | get kernel() {
|
119 | return this._kernel;
|
120 | }
|
121 | |
122 |
|
123 |
|
124 | get path() {
|
125 | return this._path;
|
126 | }
|
127 | |
128 |
|
129 |
|
130 | get type() {
|
131 | return this._type;
|
132 | }
|
133 | |
134 |
|
135 |
|
136 | get name() {
|
137 | return this._name;
|
138 | }
|
139 | |
140 |
|
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 |
|
153 |
|
154 | get isDisposed() {
|
155 | return this._isDisposed;
|
156 | }
|
157 | |
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
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 |
|
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 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
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 |
|
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 |
|
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 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
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 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
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 |
|
272 |
|
273 |
|
274 |
|
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 |
|
298 |
|
299 | onKernelStatus(sender, state) {
|
300 | this._statusChanged.emit(state);
|
301 | }
|
302 | |
303 |
|
304 |
|
305 | onKernelConnectionStatus(sender, state) {
|
306 | this._connectionStatusChanged.emit(state);
|
307 | }
|
308 | |
309 |
|
310 |
|
311 | onPendingInput(sender, state) {
|
312 | this._pendingInput.emit(state);
|
313 | }
|
314 | |
315 |
|
316 |
|
317 | onIOPubMessage(sender, msg) {
|
318 | this._iopubMessage.emit(msg);
|
319 | }
|
320 | |
321 |
|
322 |
|
323 | onUnhandledMessage(sender, msg) {
|
324 | this._unhandledMessage.emit(msg);
|
325 | }
|
326 | |
327 |
|
328 |
|
329 | onAnyMessage(sender, args) {
|
330 | this._anyMessage.emit(args);
|
331 | }
|
332 | |
333 |
|
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 |
|
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 | }
|
355 | exports.SessionConnection = SessionConnection;
|
356 |
|
\ | No newline at end of file |