@jupyterlab/services
Version:
Client APIs for the Jupyter services REST APIs
119 lines • 3.65 kB
JavaScript
"use strict";
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.KernelSpecManager = void 0;
const coreutils_1 = require("@lumino/coreutils");
const polling_1 = require("@lumino/polling");
const signaling_1 = require("@lumino/signaling");
const basemanager_1 = require("../basemanager");
const restapi_1 = require("./restapi");
/**
* An implementation of a kernel spec manager.
*/
class KernelSpecManager extends basemanager_1.BaseManager {
/**
* Construct a new kernel spec manager.
*
* @param options - The default options for kernel.
*/
constructor(options = {}) {
var _a, _b;
super(options);
this._isReady = false;
this._connectionFailure = new signaling_1.Signal(this);
this._specs = null;
this._specsChanged = new signaling_1.Signal(this);
this._kernelSpecAPIClient =
(_a = options.kernelSpecAPIClient) !== null && _a !== void 0 ? _a : new restapi_1.KernelSpecAPIClient({ serverSettings: this.serverSettings });
// Initialize internal data.
this._ready = Promise.all([this.requestSpecs()])
.then(_ => undefined)
.catch(_ => undefined)
.then(() => {
if (this.isDisposed) {
return;
}
this._isReady = true;
});
this._pollSpecs = new polling_1.Poll({
auto: false,
factory: () => this.requestSpecs(),
frequency: {
interval: 61 * 1000,
backoff: true,
max: 300 * 1000
},
name: ` /services:KernelSpecManager#specs`,
standby: (_b = options.standby) !== null && _b !== void 0 ? _b : 'when-hidden'
});
void this.ready.then(() => {
void this._pollSpecs.start();
});
}
/**
* Test whether the manager is ready.
*/
get isReady() {
return this._isReady;
}
/**
* A promise that fulfills when the manager is ready.
*/
get ready() {
return this._ready;
}
/**
* Get the most recently fetched kernel specs.
*/
get specs() {
return this._specs;
}
/**
* A signal emitted when the specs change.
*/
get specsChanged() {
return this._specsChanged;
}
/**
* A signal emitted when there is a connection failure.
*/
get connectionFailure() {
return this._connectionFailure;
}
/**
* Dispose of the resources used by the manager.
*/
dispose() {
this._pollSpecs.dispose();
super.dispose();
}
/**
* Force a refresh of the specs from the server.
*
* @returns A promise that resolves when the specs are fetched.
*
* #### Notes
* This is intended to be called only in response to a user action,
* since the manager maintains its internal state.
*/
async refreshSpecs() {
await this._pollSpecs.refresh();
await this._pollSpecs.tick;
}
/**
* Execute a request to the server to poll specs and update state.
*/
async requestSpecs() {
const specs = await this._kernelSpecAPIClient.get();
if (this.isDisposed) {
return;
}
if (!coreutils_1.JSONExt.deepEqual(specs, this._specs)) {
this._specs = specs;
this._specsChanged.emit(specs);
}
}
}
exports.KernelSpecManager = KernelSpecManager;
//# sourceMappingURL=manager.js.map