thebe-core
Version:
Typescript based core functionality for Thebe
103 lines • 3.9 kB
JavaScript
import { __awaiter } from "tslib";
import { ErrorStatusEvent, EventSubject, SessionStatusEvent } from './events';
import { ThebeManager } from './manager';
import { EventEmitter } from './emitter';
class ThebeSession {
constructor(server, connection, rendermime) {
var _a;
this.server = server;
this.connection = connection;
this.events = new EventEmitter(this.connection.id, server.config, EventSubject.session, this);
if (this.connection.kernel == null)
throw Error('ThebeSession - kernel is null');
this.manager = new ThebeManager(this.connection.kernel, rendermime);
this.connection.statusChanged.connect((_, s) => {
// 'unknown' | 'starting' | 'idle' | 'busy' | 'terminating' | 'restarting' | 'autorestarting' | 'dead'
let status;
switch (s) {
case 'starting':
case 'restarting':
case 'autorestarting':
status = SessionStatusEvent.starting;
break;
case 'idle':
case 'busy':
status = SessionStatusEvent.ready;
break;
case 'terminating':
case 'dead':
default:
status = SessionStatusEvent.shutdown;
break;
}
this.events.triggerStatus({
status,
message: `kernel ${this.connection.name} status changed to ${status}[${s}]`,
});
if (s === 'dead') {
this.events.triggerError({
status: ErrorStatusEvent.session,
message: `kernel ${this.connection.name} is dead`,
});
this.dispose();
}
});
this.connection.disposed.connect(() => {
this.events.triggerStatus({
status: SessionStatusEvent.shutdown,
message: `kernel ${this.connection.name} disposed`,
});
});
this.events.triggerStatus({
status: SessionStatusEvent.ready,
message: `ThebeSession created, kernel '${(_a = this.connection.kernel) === null || _a === void 0 ? void 0 : _a.name}' available`,
});
}
get id() {
return this.connection.id;
}
get kernel() {
var _a;
return (_a = this.connection) === null || _a === void 0 ? void 0 : _a.kernel;
}
get path() {
return this.connection.path;
}
get name() {
return this.connection.name;
}
restart() {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
console.debug(`requesting restart for kernel ${this.id}`);
const p = (_a = this.connection.kernel) === null || _a === void 0 ? void 0 : _a.restart();
this.events.triggerStatus({
status: SessionStatusEvent.starting,
message: `Kernel restart requested`,
});
yield p;
this.events.triggerStatus({
status: SessionStatusEvent.ready,
message: `session restarted, kernel '${(_b = this.connection.kernel) === null || _b === void 0 ? void 0 : _b.name}' available`,
});
});
}
shutdown() {
return __awaiter(this, void 0, void 0, function* () {
if (this.connection.isDisposed)
return;
yield this.connection.shutdown();
this.events.triggerStatus({
status: SessionStatusEvent.shutdown,
message: `session ${this.name}`,
});
this.dispose();
});
}
dispose() {
if (!this.connection.isDisposed)
this.connection.dispose();
}
}
export default ThebeSession;
//# sourceMappingURL=session.js.map