manager.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { IDisposable } from '@lumino/disposable';
  4. import { Poll } from '@lumino/polling';
  5. import { ISignal, Signal } from '@lumino/signaling';
  6. import { Builder, BuildManager } from './builder';
  7. import { NbConvert, NbConvertManager } from './nbconvert';
  8. import { Contents, ContentsManager } from './contents';
  9. import { KernelSpec, KernelSpecManager } from './kernelspec';
  10. import { Session, SessionManager } from './session';
  11. import { Setting, SettingManager } from './setting';
  12. import { Terminal, TerminalManager } from './terminal';
  13. import { ServerConnection } from './serverconnection';
  14. import { Workspace, WorkspaceManager } from './workspace';
  15. import { KernelManager } from './kernel';
  16. /**
  17. * A Jupyter services manager.
  18. */
  19. export class ServiceManager implements ServiceManager.IManager {
  20. /**
  21. * Construct a new services provider.
  22. */
  23. constructor(options: ServiceManager.IOptions = {}) {
  24. const defaultDrive = options.defaultDrive;
  25. const serverSettings =
  26. options.serverSettings ?? ServerConnection.makeSettings();
  27. const standby = options.standby ?? 'when-hidden';
  28. const normalized = { defaultDrive, serverSettings, standby };
  29. const kernelManager = new KernelManager(normalized);
  30. this.serverSettings = serverSettings;
  31. this.contents = new ContentsManager(normalized);
  32. this.sessions = new SessionManager({
  33. ...normalized,
  34. kernelManager: kernelManager
  35. });
  36. this.settings = new SettingManager(normalized);
  37. this.terminals = new TerminalManager(normalized);
  38. this.builder = new BuildManager(normalized);
  39. this.workspaces = new WorkspaceManager(normalized);
  40. this.nbconvert = new NbConvertManager(normalized);
  41. this.kernelspecs = new KernelSpecManager(normalized);
  42. // Relay connection failures from the service managers that poll
  43. // the server for current information.
  44. this.kernelspecs.connectionFailure.connect(this._onConnectionFailure, this);
  45. this.sessions.connectionFailure.connect(this._onConnectionFailure, this);
  46. this.terminals.connectionFailure.connect(this._onConnectionFailure, this);
  47. const readyList = [this.sessions.ready, this.kernelspecs.ready];
  48. if (this.terminals.isAvailable()) {
  49. readyList.push(this.terminals.ready);
  50. }
  51. this._readyPromise = Promise.all(readyList).then(() => {
  52. this._isReady = true;
  53. });
  54. }
  55. /**
  56. * A signal emitted when there is a connection failure with the kernel.
  57. */
  58. get connectionFailure(): ISignal<this, Error> {
  59. return this._connectionFailure;
  60. }
  61. /**
  62. * Test whether the service manager is disposed.
  63. */
  64. get isDisposed(): boolean {
  65. return this._isDisposed;
  66. }
  67. /**
  68. * Dispose of the resources used by the manager.
  69. */
  70. dispose(): void {
  71. if (this.isDisposed) {
  72. return;
  73. }
  74. this._isDisposed = true;
  75. Signal.clearData(this);
  76. this.contents.dispose();
  77. this.sessions.dispose();
  78. this.terminals.dispose();
  79. }
  80. /**
  81. * The server settings of the manager.
  82. */
  83. readonly serverSettings: ServerConnection.ISettings;
  84. /**
  85. * Get the session manager instance.
  86. */
  87. readonly sessions: SessionManager;
  88. /**
  89. * Get the session manager instance.
  90. */
  91. readonly kernelspecs: KernelSpecManager;
  92. /**
  93. * Get the setting manager instance.
  94. */
  95. readonly settings: SettingManager;
  96. /**
  97. * The builder for the manager.
  98. */
  99. readonly builder: BuildManager;
  100. /**
  101. * Get the contents manager instance.
  102. */
  103. readonly contents: ContentsManager;
  104. /**
  105. * Get the terminal manager instance.
  106. */
  107. readonly terminals: TerminalManager;
  108. /**
  109. * Get the workspace manager instance.
  110. */
  111. readonly workspaces: WorkspaceManager;
  112. /**
  113. * Get the nbconvert manager instance.
  114. */
  115. readonly nbconvert: NbConvertManager;
  116. /**
  117. * Test whether the manager is ready.
  118. */
  119. get isReady(): boolean {
  120. return this._isReady;
  121. }
  122. /**
  123. * A promise that fulfills when the manager is ready.
  124. */
  125. get ready(): Promise<void> {
  126. return this._readyPromise;
  127. }
  128. private _onConnectionFailure(sender: any, err: Error): void {
  129. this._connectionFailure.emit(err);
  130. }
  131. private _isDisposed = false;
  132. private _readyPromise: Promise<void>;
  133. private _connectionFailure = new Signal<this, Error>(this);
  134. private _isReady = false;
  135. }
  136. /**
  137. * The namespace for `ServiceManager` statics.
  138. */
  139. export namespace ServiceManager {
  140. /**
  141. * A service manager interface.
  142. */
  143. export interface IManager extends IDisposable {
  144. /**
  145. * The builder for the manager.
  146. */
  147. readonly builder: Builder.IManager;
  148. /**
  149. * The contents manager for the manager.
  150. */
  151. readonly contents: Contents.IManager;
  152. /**
  153. * Test whether the manager is ready.
  154. */
  155. readonly isReady: boolean;
  156. /**
  157. * A promise that fulfills when the manager is initially ready.
  158. */
  159. readonly ready: Promise<void>;
  160. /**
  161. * The server settings of the manager.
  162. */
  163. readonly serverSettings: ServerConnection.ISettings;
  164. /**
  165. * The session manager for the manager.
  166. */
  167. readonly sessions: Session.IManager;
  168. /**
  169. * The session manager for the manager.
  170. */
  171. readonly kernelspecs: KernelSpec.IManager;
  172. /**
  173. * The setting manager for the manager.
  174. */
  175. readonly settings: Setting.IManager;
  176. /**
  177. * The terminals manager for the manager.
  178. */
  179. readonly terminals: Terminal.IManager;
  180. /**
  181. * The workspace manager for the manager.
  182. */
  183. readonly workspaces: Workspace.IManager;
  184. /**
  185. * The nbconvert manager for the manager.
  186. */
  187. readonly nbconvert: NbConvert.IManager;
  188. /**
  189. * A signal emitted when there is a connection failure with the server.
  190. */
  191. readonly connectionFailure: ISignal<IManager, Error>;
  192. }
  193. /**
  194. * The options used to create a service manager.
  195. */
  196. export interface IOptions {
  197. /**
  198. * The server settings of the manager.
  199. */
  200. readonly serverSettings?: ServerConnection.ISettings;
  201. /**
  202. * The default drive for the contents manager.
  203. */
  204. readonly defaultDrive?: Contents.IDrive;
  205. /**
  206. * When the manager stops polling the API. Defaults to `when-hidden`.
  207. */
  208. standby?: Poll.Standby;
  209. }
  210. }