Skip to content

Commit

Permalink
Factor out common things from the managers into a base manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongrout committed Oct 3, 2019
1 parent a4ec589 commit f6c25a6
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 110 deletions.
105 changes: 105 additions & 0 deletions packages/services/src/basemanager.ts
@@ -0,0 +1,105 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import { IObservableDisposable } from '@phosphor/disposable';
import { ISignal, Signal } from '@phosphor/signaling';
import { ServerConnection } from './serverconnection';
import { Poll } from '@jupyterlab/coreutils';

/**
* Object which manages kernel instances for a given base url.
*
* #### Notes
* The manager is responsible for maintaining the state of kernel specs.
*/
export interface IManager extends IObservableDisposable {
/**
* A signal emitted when there is a connection failure.
* TODO: figure out the relationship between this and the other connection status signals for kernels.
*/
connectionFailure: ISignal<IManager, ServerConnection.NetworkError>;

/**
* The server settings for the manager.
*/
readonly serverSettings: ServerConnection.ISettings;

/**
* Whether the manager is ready.
*/
readonly isReady: boolean;

/**
* A promise that resolves when the manager is initially ready.
*/
readonly ready: Promise<void>;
}

export abstract class BaseManager implements IManager {
constructor(options: BaseManager.IOptions) {
this.serverSettings =
options.serverSettings || ServerConnection.makeSettings();
}
/**
* A signal emitted when the delegate is disposed.
*/
get disposed(): ISignal<this, void> {
return this._disposed;
}

/**
* A signal emitted when there is a connection failure.
*/
abstract connectionFailure: ISignal<this, Error>;

/**
* Test whether the delegate has been disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}

/**
* Test whether the manager is ready.
*/
abstract isReady: boolean;

/**
* A promise that fulfills when the manager is ready.
*/
abstract ready: Promise<void>;

/**
* Dispose of the delegate and invoke the callback function.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
this._disposed.emit(undefined);
Signal.clearData(this);
}

/**
* The server settings of the manager.
*/
readonly serverSettings: ServerConnection.ISettings;

private _isDisposed = false;
private _disposed = new Signal<this, void>(this);
}

/**
* The namespace for `BaseManager` class statics.
*/
export namespace BaseManager {
/**
* The options used to initialize a SessionManager.
*/
export interface IOptions {
/**
* The server settings for the manager.
*/
serverSettings?: ServerConnection.ISettings;
}
}
8 changes: 2 additions & 6 deletions packages/services/src/kernel/kernel.ts
Expand Up @@ -16,6 +16,7 @@ import { DefaultKernel } from './default';
import { KernelMessage } from './messages';

import { KernelSpec } from '../kernelspec';
import { IManager as IBaseManager } from '../basemanager';

/**
* A namespace for kernel types, interfaces, and type checker functions.
Expand Down Expand Up @@ -652,7 +653,7 @@ export namespace Kernel {
* through polling the server. Use a manager if you want to be notified of
* changes to kernels.
*/
export interface IManager extends IDisposable {
export interface IManager extends IBaseManager {
/**
* A signal emitted when the running kernels change.
*/
Expand All @@ -664,11 +665,6 @@ export namespace Kernel {
*/
connectionFailure: ISignal<IManager, ServerConnection.NetworkError>;

/**
* The server settings for the manager.
*/
serverSettings?: ServerConnection.ISettings;

/**
* Whether the manager is ready.
*/
Expand Down
29 changes: 6 additions & 23 deletions packages/services/src/kernel/manager.ts
Expand Up @@ -12,6 +12,7 @@ import { ISignal, Signal } from '@phosphor/signaling';
import { ServerConnection } from '..';

import { Kernel } from './kernel';
import { BaseManager } from '../basemanager';

// TODO: Migrate kernel connection status etc. up to session
// TODO: move session management work up to session manager rather than session objects
Expand All @@ -21,15 +22,14 @@ import { Kernel } from './kernel';
/**
* An implementation of a kernel manager.
*/
export class KernelManager implements Kernel.IManager {
export class KernelManager extends BaseManager implements Kernel.IManager {
/**
* Construct a new kernel manager.
*
* @param options - The default options for kernel.
*/
constructor(options: KernelManager.IOptions = {}) {
this.serverSettings =
options.serverSettings || ServerConnection.makeSettings();
super(options);

// Initialize internal data.
this._ready = (async () => {
Expand Down Expand Up @@ -62,13 +62,6 @@ export class KernelManager implements Kernel.IManager {
*/
readonly serverSettings: ServerConnection.ISettings;

/**
* Test whether the kernel manager is disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}

/**
* Test whether the manager is ready.
*/
Expand Down Expand Up @@ -114,13 +107,9 @@ export class KernelManager implements Kernel.IManager {
* Dispose of the resources used by the manager.
*/
dispose(): void {
if (this._isDisposed) {
return;
}
this._isDisposed = true;
this._models.length = 0;
this._pollModels.dispose();
Signal.clearData(this);
super.dispose();
}

/**
Expand Down Expand Up @@ -253,7 +242,7 @@ export class KernelManager implements Kernel.IManager {
}
throw err;
});
if (this._isDisposed) {
if (this.isDisposed) {
return;
}
if (!JSONExt.deepEqual(models, this._models)) {
Expand Down Expand Up @@ -297,7 +286,6 @@ export class KernelManager implements Kernel.IManager {
}
}

private _isDisposed = false;
private _isReady = false;
private _kernels = new Set<Kernel.IKernel>();
private _models: Kernel.IModel[] = [];
Expand All @@ -314,12 +302,7 @@ export namespace KernelManager {
/**
* The options used to initialize a KernelManager.
*/
export interface IOptions {
/**
* The server settings for the manager.
*/
serverSettings?: ServerConnection.ISettings;

export interface IOptions extends BaseManager.IOptions {
/**
* When the manager stops polling the API. Defaults to `when-hidden`.
*/
Expand Down
30 changes: 7 additions & 23 deletions packages/services/src/kernelspec/manager.ts
Expand Up @@ -6,18 +6,19 @@ import { ServerConnection } from '../serverconnection';
import { KernelSpec } from './kernelspec';
import { ISignal, Signal } from '@phosphor/signaling';
import { JSONExt } from '@phosphor/coreutils';
import { BaseManager } from '../basemanager';
/**
* An implementation of a kernel manager.
*/
export class KernelSpecManager implements KernelSpec.IManager {
export class KernelSpecManager extends BaseManager
implements KernelSpec.IManager {
/**
* Construct a new kernel manager.
*
* @param options - The default options for kernel.
*/
constructor(options: KernelSpecManager.IOptions = {}) {
this.serverSettings =
options.serverSettings || ServerConnection.makeSettings();
super(options);

// Initialize internal data.
this._ready = Promise.all([this.requestSpecs()])
Expand Down Expand Up @@ -51,13 +52,6 @@ export class KernelSpecManager implements KernelSpec.IManager {
*/
readonly serverSettings: ServerConnection.ISettings;

/**
* Test whether the kernel manager is disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}

/**
* Test whether the manager is ready.
*/
Expand Down Expand Up @@ -97,12 +91,8 @@ export class KernelSpecManager implements KernelSpec.IManager {
* Dispose of the resources used by the manager.
*/
dispose(): void {
if (this._isDisposed) {
return;
}
this._isDisposed = true;
this._pollSpecs.dispose();
Signal.clearData(this);
super.dispose();
}

/**
Expand All @@ -124,7 +114,7 @@ export class KernelSpecManager implements KernelSpec.IManager {
*/
protected async requestSpecs(): Promise<void> {
const specs = await KernelSpec.getSpecs(this.serverSettings);
if (this._isDisposed) {
if (this.isDisposed) {
return;
}
if (!JSONExt.deepEqual(specs, this._specs)) {
Expand All @@ -133,7 +123,6 @@ export class KernelSpecManager implements KernelSpec.IManager {
}
}

private _isDisposed = false;
private _isReady = false;
private _connectionFailure = new Signal<this, Error>(this);

Expand All @@ -151,12 +140,7 @@ export namespace KernelSpecManager {
/**
* The options used to initialize a KernelManager.
*/
export interface IOptions {
/**
* The server settings for the manager.
*/
serverSettings?: ServerConnection.ISettings;

export interface IOptions extends BaseManager.IOptions {
/**
* When the manager stops polling the API. Defaults to `when-hidden`.
*/
Expand Down
2 changes: 0 additions & 2 deletions packages/services/src/manager.ts
Expand Up @@ -13,8 +13,6 @@ import { NbConvert, NbConvertManager } from './nbconvert';

import { Contents, ContentsManager } from './contents';

import { Kernel } from './kernel';

import { KernelSpec, KernelSpecManager } from './kernelspec';

import { Session, SessionManager } from './session';
Expand Down

0 comments on commit f6c25a6

Please sign in to comment.