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 = (_d = options.serverSettings) !== null && _d !== void 0 ? _d : __1.ServerConnection.makeSettings();
|
45 | this.setupKernel(options.model.kernel);
|
46 | }
|
47 | |
48 |
|
49 |
|
50 | get disposed() {
|
51 | return this._disposed;
|
52 | }
|
53 | |
54 |
|
55 |
|
56 | get kernelChanged() {
|
57 | return this._kernelChanged;
|
58 | }
|
59 | |
60 |
|
61 |
|
62 | get statusChanged() {
|
63 | return this._statusChanged;
|
64 | }
|
65 | |
66 |
|
67 |
|
68 | get connectionStatusChanged() {
|
69 | return this._connectionStatusChanged;
|
70 | }
|
71 | |
72 |
|
73 |
|
74 | get pendingInput() {
|
75 | return this._pendingInput;
|
76 | }
|
77 | |
78 |
|
79 |
|
80 | get iopubMessage() {
|
81 | return this._iopubMessage;
|
82 | }
|
83 | |
84 |
|
85 |
|
86 | get unhandledMessage() {
|
87 | return this._unhandledMessage;
|
88 | }
|
89 | |
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | get anyMessage() {
|
97 | return this._anyMessage;
|
98 | }
|
99 | |
100 |
|
101 |
|
102 | get propertyChanged() {
|
103 | return this._propertyChanged;
|
104 | }
|
105 | |
106 |
|
107 |
|
108 | get id() {
|
109 | return this._id;
|
110 | }
|
111 | |
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | get kernel() {
|
118 | return this._kernel;
|
119 | }
|
120 | |
121 |
|
122 |
|
123 | get path() {
|
124 | return this._path;
|
125 | }
|
126 | |
127 |
|
128 |
|
129 | get type() {
|
130 | return this._type;
|
131 | }
|
132 | |
133 |
|
134 |
|
135 | get name() {
|
136 | return this._name;
|
137 | }
|
138 | |
139 |
|
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 |
|
152 |
|
153 | get isDisposed() {
|
154 | return this._isDisposed;
|
155 | }
|
156 | |
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
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 |
|
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 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
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 |
|
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 |
|
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 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
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 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
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 |
|
271 |
|
272 |
|
273 |
|
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 |
|
291 |
|
292 | onKernelStatus(sender, state) {
|
293 | this._statusChanged.emit(state);
|
294 | }
|
295 | |
296 |
|
297 |
|
298 | onKernelConnectionStatus(sender, state) {
|
299 | this._connectionStatusChanged.emit(state);
|
300 | }
|
301 | |
302 |
|
303 |
|
304 | onPendingInput(sender, state) {
|
305 | this._pendingInput.emit(state);
|
306 | }
|
307 | |
308 |
|
309 |
|
310 | onIOPubMessage(sender, msg) {
|
311 | this._iopubMessage.emit(msg);
|
312 | }
|
313 | |
314 |
|
315 |
|
316 | onUnhandledMessage(sender, msg) {
|
317 | this._unhandledMessage.emit(msg);
|
318 | }
|
319 | |
320 |
|
321 |
|
322 | onAnyMessage(sender, args) {
|
323 | this._anyMessage.emit(args);
|
324 | }
|
325 | |
326 |
|
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 |
|
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 | }
|
348 | exports.SessionConnection = SessionConnection;
|
349 |
|
\ | No newline at end of file |